home *** CD-ROM | disk | FTP | other *** search
- class Palette:
- def __init__(self):
- self.__dict__['palette'] = []
-
- def __call__(self, rgb, g=None, b=None):
- if (g,b) != (None,None):
- rgb = (rgb,g,b)
- # map rgb tuple to colour index
- try:
- return self.palette.index(rgb)
- except:
- i = len(self.palette)
- if i >= 256:
- raise RuntimeError, "all palette entries are used"
- self.palette.append(rgb)
- return i
-
- def __setattr__(self, name, value):
- ' assign an attribute of a given colour to the palette entry '
- self.__dict__[name] = self.__call__(value)
-
- def __getitem__(self, name):
- ' allow dict lookups using colour names '
- return self.__dict__[name]
-
- def getpalette(self):
- # return flattened palette
- palette = []
- for r, g, b in self.palette:
- palette = palette + [r, g, b]
- return palette
-
- def simple_palette(self):
- self.white = (255, 255, 255)
- self.lightgrey = (200, 200, 200)
- self.lightmediumgrey = (150, 150, 150)
- self.mediumgrey = (100, 100, 100)
- self.darkgrey = (50, 50, 50)
- self.black = (0, 0, 0)
-
- self.blue = (0, 0, 255)
- self.lightblue = (128, 128, 255)
- self.green = (0, 255, 0)
-
- self.lightgreen = (128, 255, 128)
- self.red = (255, 0, 0)
- self.lightred = (255, 128, 128)
-
- self.cyan = (0, 255, 255)
- self.purple = (255, 0, 255)
- self.yellow = (255, 255, 0)
-
-