Example #1
0
    def __init__(self, col, format="hex", bw=None, bwrgb=None):

        self.alpha = 1
        self.bwlevel = bw or BWLEVEL
        self.bwrgb = bwrgb or BWRGBWEIGHT

        if isinstance(col, (long, float, int)):
            col = hex(col)
        elif isinstance(col, Color):
            col = col.hex
        if format in ('cmyk', 'rgb', 'hex', 'uhex'):
            self.format = format
        else:
            raise ValueError('[Color] Wrong format "%s". Should be in %s' % (format, repr(('cmyk', 'rgb', 'hex', 'uhex'))))
        try:
            if isinstance(col, basestring):
                # Convert unreliable HTML/CSS color name to hex value and process from there.
                col = COLOR_NAMES.get(col, col)

                if ',' in col:
                    self.c = TX.list2IntFloatList(col)
                elif HEXTUPLE.match(col):
                    if len(col) == 4:
                        col = '#%s%s%s' % (col[1]*2, col[2]*2, col[3]*2)
                    self.c = col
                elif UHEXTUPLE.match(col):
                    col = col[2:]
                    if len(col) == 4:
                        col = '#%s%s%s' % (col[1]*2, col[2]*2, col[3]*2)
                    self.c = col
                elif RGBTUPLE.match(col):
                    mrgb = RGBTUPLE.match(col)
                    self.c = (int(mrgb.group(1)), int(mrgb.group(2)), int(mrgb.group(3)))
                elif RGBATUPLE.match(col):
                    mrgb = RGBTUPLE.match(col)
                    self.c = (int(mrgb.group(1)), int(mrgb.group(2)), int(mrgb.group(3)))
                    self.alpha = float(mrgb.group(4))
                elif CMYKTUPLE.match(col):
                    mcmyk = CMYKTUPLE.match(col)
                    self.c = (int(mcmyk.group(1)), int(mcmyk.group(2)), int(mcmyk.group(3)), int(mcmyk.group(4)))
                else:
                    ValueError('[Color] Unknown color format "%s"' % color)
            else:
                self.c = col
                if len(self.c) == 3 and sum(self.c) <= 3:
                    # Test for 0-1 values - RGB values should be 0-255
                    self.c = list(self.c)
                    for i in range(3):
                        if self.c[i] <= 1 and self.c[i] / 2 == float(self.c[i]) / 2:
                            self.c[i] *= 255
                            self.c[i] = int(self.c[i])
                    self.c = tuple(self.c)
        except:
            raise ValueError('[Color] Wrong color format "%s"' % color)

        rgbint = color.rgb(self.c)
        self.r, self.g, self.b = color._int2rgbtuple(rgbint)
        self.h, self.l, self.s = _rgb2hls(self.r, self.g, self.b)
Example #2
0
    def __init__(self, col, format="hex", bw=None, bwrgb=None):

        self.alpha = 1
        self.bwlevel = bw or BWLEVEL
        self.bwrgb = bwrgb or BWRGBWEIGHT

        if isinstance(col, Color):
            col = col.hex
        if format in ('cmyk', 'rgb', 'hex', 'uhex'):
            self.format = format
        else:
            raise ValueError('[Color] Wrong format "%s". Should be in %s' %
                             (format, repr(('cmyk', 'rgb', 'hex', 'uhex'))))
        try:
            if isinstance(col, basestring):
                if ',' in col:
                    self.c = TX.list2IntFloatList(col)
                elif HEXTUPLE.match(col):
                    self.c = col
                elif UHEXTUPLE.match(col):
                    self.c = col[2:]
                elif RGBTUPLE.match(col):
                    mrgb = RGBTUPLE.match(col)
                    self.c = (int(mrgb.group(1)), int(mrgb.group(2)),
                              int(mrgb.group(3)))
                elif RGBATUPLE.match(col):
                    mrgb = RGBTUPLE.match(col)
                    self.c = (int(mrgb.group(1)), int(mrgb.group(2)),
                              int(mrgb.group(3)))
                    self.alpha = float(mrgb.group(4))
                elif CMYKTUPLE.match(col):
                    mcmyk = CMYKTUPLE.match(col)
                    self.c = (int(mcmyk.group(1)), int(mcmyk.group(2)),
                              int(mcmyk.group(3)), int(mcmyk.group(4)))
                else:
                    ValueError('[Color] Unknown color format "%s"' % color)
            else:
                self.c = col
                if len(self.c) == 3 and sum(self.c) <= 3:
                    # Test for 0-1 values - RGB values should be 0-255
                    self.c = list(self.c)
                    for i in range(3):
                        if self.c[i] <= 1 and self.c[i] / 2 == float(
                                self.c[i]) / 2:
                            self.c[i] *= 255
                            self.c[i] = int(self.c[i])
                    self.c = tuple(self.c)
        except:
            raise ValueError('[Color] Wrong color format "%s"' % color)

        rgbint = color.rgb(self.c)
        self.r, self.g, self.b = color._int2rgbtuple(rgbint)
        self.h, self.l, self.s = _rgb2hls(self.r, self.g, self.b)