Created
June 22, 2021 11:06
-
-
Save martenlienen/dbf67dd70946a9a0c55ff87c2b7a342d to your computer and use it in GitHub Desktop.
Generate inkscape / gimp palettes from seaborn and matplotlib color maps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GIMP Palette | |
Name: colorblind (seaborn) | |
Columns: 0 | |
# | |
1 115 178 | |
35 176 253 | |
145 215 254 | |
222 143 5 | |
250 185 70 | |
252 220 162 | |
2 158 115 | |
24 252 189 | |
139 253 222 | |
213 94 0 | |
255 144 57 | |
255 199 156 | |
204 120 188 | |
221 164 210 | |
238 210 232 | |
202 145 97 | |
219 181 149 | |
237 218 202 | |
251 175 228 | |
252 201 237 | |
253 228 246 | |
148 148 148 | |
183 183 183 | |
219 219 219 | |
236 225 51 | |
242 235 119 | |
248 244 187 | |
86 180 233 | |
142 205 240 | |
198 230 247 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import sys | |
from colorsys import hls_to_rgb, rgb_to_hls | |
from pathlib import Path | |
import numpy as np | |
import seaborn as sns | |
from matplotlib.colors import ListedColormap | |
from webcolors import hex_to_rgb | |
def to_hsl(rgb): | |
rgb = [c / 255 for c in rgb] | |
h, l, s = rgb_to_hls(*rgb) | |
return h, s, l | |
def to_rgb(hsl): | |
h, s, l = hsl | |
return [int(c * 255) for c in hls_to_rgb(h, l, s)] | |
def write_rgb(f, rgb, name=None): | |
f.write(" ".join(map(str, rgb))) | |
if name is not None: | |
f.write(" ") | |
f.write(name) | |
f.write("\n") | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-o", "--out", help="Palette file") | |
parser.add_argument( | |
"-p", "--palette", default="colorblind", help="Seaborn palette name" | |
) | |
parser.add_argument("-n", "--name", help="Inkscape palette name") | |
args = parser.parse_args() | |
out = args.out | |
palette_name = args.palette | |
out_name = args.name | |
if out is None: | |
out = f"~/.config/inkscape/palettes/{palette_name}.gpl" | |
out = Path(out).expanduser() | |
out.parent.mkdir(parents=True, exist_ok=True) | |
if out_name is None: | |
out_name = f"{palette_name} (seaborn)" | |
cmap = sns.color_palette(palette_name, as_cmap=True) | |
if isinstance(cmap, list): | |
cmap = ListedColormap(cmap) | |
if not isinstance(cmap, ListedColormap): | |
sys.exit("Color map needs to be qualitative") | |
with out.open("w") as f: | |
f.write(f"GIMP Palette\nName: {out_name}\nColumns: 0\n#\n") | |
for hexcolor in cmap.colors: | |
rgb = hex_to_rgb(hexcolor) | |
write_rgb(f, rgb) | |
h, s, l = to_hsl(rgb) | |
# Exclude the original luminance and full white | |
for l_ in np.linspace(l, 1.0, num=4)[1:-1]: | |
write_rgb(f, to_rgb((h, s, l_))) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment