from colormath.color_objects import sRGBColor, LabColor, HSVColor
from colormath.color_diff import *
from colormath.color_conversions import convert_color
import numpy
from IPython.display import HTML, display
import json
import palettable
def swatches(colors):
tmpl = "<div style='float: left; width: 20px;height: 20px; background:{0}'></div>"
hexes = (sRGBColor(*c, is_upscaled=True).get_rgb_hex() for c in colors)
html = "<div style='width: 400px; overflow:auto;'>" + "".join(map(tmpl.format, hexes)) + "</div>"
return HTML(html)
def gen_hsv_pal(n, s=1.0, v=1.0):
hsv_pal = [HSVColor((360.0 * hue) / n, s, v) for hue in range(n)]
return [convert_color(hsv, sRGBColor).get_upscaled_value_tuple() for hsv in hsv_pal]
palettes = json.load(open("palettes_sorted.json"))
palettes_cb_12 = map(palettable.order_palette, json.load(open("cb_12.json")))
# "optimal" palettes
for palette in sorted(palettes, key=len):
if len(palette) == 12:
print(palettable.palette_min_diff(palette))
display(swatches(palette))
# color brewer palettes
for palette in palettes_cb_12:
print(palettable.palette_min_diff(palette))
display(swatches(palette))
hsv_pal = gen_hsv_pal(12)
hsv_pal_ord = palettable.order_palette(hsv_pal)
print(palettable.palette_min_diff(hsv_pal))
display(swatches(hsv_pal))