import palettable
def get_colormaps(collection, suffix):
for palette in collection.__dict__:
if palette.endswith(suffix):
yield getattr(collection, palette)
div = palettable.colorbrewer.diverging
for palette in get_colormaps(div, '11'):
print(palette.name)
BrBG PiYG PRGn PuOr RdBu RdGy RdYlBu RdYlGn Spectral
qual = palettable.colorbrewer.qualitative
for palette in get_colormaps(qual, '8'):
print(palette.name)
Accent Dark2 Paired Pastel1 Pastel2 Set1 Set2 Set3
seq = palettable.colorbrewer.sequential
for palette in get_colormaps(seq, '9'):
print(palette.name)
Blues BuGn BuPu GnBu Greens Greys Oranges OrRd PuBu PuBuGn PuRd Purples RdPu Reds YlGn YlGnBu YlOrBr YlOrRd
head = """<?xml version="1.0" encoding="UTF-8"?>
<office:color-table xmlns:office="http://openoffice.org/2000/office" xmlns:style="http://openoffice.org/2000/style" xmlns:text="http://openoffice.org/2000/text" xmlns:table="http://openoffice.org/2000/table" xmlns:draw="http://openoffice.org/2000/drawing" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="http://openoffice.org/2000/meta" xmlns:number="http://openoffice.org/2000/datastyle" xmlns:svg="http://www.w3.org/2000/svg" xmlns:chart="http://openoffice.org/2000/chart" xmlns:dr3d="http://openoffice.org/2000/dr3d" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="http://openoffice.org/2000/form" xmlns:script="http://openoffice.org/2000/script">
"""
template = '<draw:color draw:name="%s" draw:color="%s"/>\n'
tail = "</office:color-table>"
def start_table():
f = open('allen.soc', 'w')
f.write(head)
return f
def write_colormap(f, colormap):
for i, color in enumerate(colormap.hex_colors):
name = colormap.name + str(i+1)
line = template % (name, color)
f.write(line)
next_level = 255
def write_line(name, color):
line = template % (name, color)
f.write(line)
def write_grays(f, gray, n):
hex_gray = '%0.2x' % gray
name = 'gray%d' % gray
color = '#%s%s%s' % (hex_gray, hex_gray, hex_gray)
write_line(name, color)
for i in range(1, n):
name = 'blank%d' % (gray-i)
color = '#ffffff'
write_line(name, color)
def end_table(f):
f.write(tail)
f.close()
import numpy as np
def gen_grays(num):
grays = np.linspace(255, 0, num).astype(int)
for gray in grays:
yield gray
seq_maps = list(get_colormaps(seq, '9'))
qual_maps = list(get_colormaps(qual, '8'))
div_maps = list(get_colormaps(div, '11'))
num = len(seq_maps + qual_maps + div_maps)
len(qual_maps)
8
f = start_table()
grays = gen_grays(num)
def write_maps(f, maps, grays, n):
for colormap in maps:
gray = next(grays)
write_grays(f, gray, n)
write_colormap(f, colormap)
write_maps(f, seq_maps, grays, 3)
write_maps(f, qual_maps, grays, 4)
write_maps(f, div_maps, grays, 1)
end_table(f)