In [17]:
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
In [10]:
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)
In [32]:
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]
In [ ]:
palettes = json.load(open("palettes_sorted.json"))
palettes_cb_12 = map(palettable.order_palette, json.load(open("cb_12.json")))
In [12]:
# "optimal" palettes
for palette in sorted(palettes, key=len):
    if len(palette) == 12:
        print(palettable.palette_min_diff(palette))
        display(swatches(palette))
32.5417664158
28.5584300371
30.0827146609
32.1399036258
31.2992794592
30.2147664885
26.8698665958
30.3647560674
28.5885199271
30.225886118
In [13]:
# color brewer palettes
for palette in palettes_cb_12:
    print(palettable.palette_min_diff(palette))
    display(swatches(palette))
13.7903684979
9.52235028493
In [ ]:
hsv_pal = gen_hsv_pal(12)
hsv_pal_ord = palettable.order_palette(hsv_pal)
In [43]:
print(palettable.palette_min_diff(hsv_pal))
display(swatches(hsv_pal))
5.23145251575
In [ ]: