def colors_as_image(colors): "Save palette as a PNG with labeled, colored blocks" colors = [hex_to_rgb("#%s" % c) for c in colors] size = (80 * len(colors), 80) im = Image.new('RGB', size) draw = ImageDraw.Draw(im) for i, c in enumerate(colors): v = colorsys.rgb_to_hsv(*norm_color(c))[2] (x1, y1) = (i * 80, 0) (x2, y2) = ((i + 1) * 80 - 1, 79) draw.rectangle([(x1, y1), (x2, y2)], fill=c) if v < 0.6: # white with shadow draw.text((x1 + 4, y1 + 4), rgb_to_hex(c), (90, 90, 90)) draw.text((x1 + 3, y1 + 3), rgb_to_hex(c)) else: # dark with bright "shadow" draw.text((x1 + 4, y1 + 4), rgb_to_hex(c), (230, 230, 230)) draw.text((x1 + 3, y1 + 3), rgb_to_hex(c), (0, 0, 0)) return im
def distance(self, c1, c2): return colorific.distance( colorific.hex_to_rgb(c1), colorific.hex_to_rgb(c2), )
def test_hex_to_rgb(self): for rgb, hexval in self.pairs: self.assertEqual(colorific.hex_to_rgb(hexval), rgb)