Skip to content

Instantly share code, notes, and snippets.

@KerryHalupka
Last active February 7, 2024 03:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save KerryHalupka/df046b971136152b526ffd4be2872b9d to your computer and use it in GitHub Desktop.
Save KerryHalupka/df046b971136152b526ffd4be2872b9d to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
def hex_to_rgb(value):
'''
Converts hex to rgb colours
value: string of 6 characters representing a hex colour.
Returns: list length 3 of RGB values'''
value = value.strip("#") # removes hash symbol if present
lv = len(value)
return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
def rgb_to_dec(value):
'''
Converts rgb to decimal colours (i.e. divides each value by 256)
value: list (length 3) of RGB values
Returns: list (length 3) of decimal values'''
return [v/256 for v in value]
def plot_colortable(hex_list):
cell_width = 150
cell_height = 50
swatch_width = 48
margin = 12
topmargin = 40
rgb_list = [hex_to_rgb(value) for value in hex_list]
dec_list = [rgb_to_dec(value) for value in rgb_list]
names = [f'HEX: {col[0]}\nRGB: {col[1]}' for col in zip(hex_list, rgb_list, dec_list)]
n = len(names)
ncols = 3
nrows = n // ncols + int(n % ncols > 0)
width = cell_width * 8 + 2 * margin
height = cell_height * nrows + margin + topmargin
dpi = 72
fig, ax = plt.subplots(figsize=(width / dpi, height / dpi), dpi=dpi)
fig.subplots_adjust(margin/width, margin/height,
(width-margin)/width, (height-topmargin)/height)
ax.set_xlim(0, cell_width * 4)
ax.set_ylim(cell_height * (nrows-0.5), -cell_height/2.)
ax.yaxis.set_visible(False)
ax.xaxis.set_visible(False)
ax.set_axis_off()
for i, name in enumerate(names):
row = i % nrows
col = i // nrows
y = row * cell_height
swatch_start_x = cell_width * col
swatch_end_x = cell_width * col + swatch_width
text_pos_x = cell_width * col + swatch_width + 7
ax.text(text_pos_x, y, name, fontsize=14,
horizontalalignment='left',
verticalalignment='center')
ax.hlines(y, swatch_start_x, swatch_end_x,
color=dec_list[i], linewidth=18)
return fig
plot_colortable(['#54478c', '#2c699a', '#048ba8', '#0db39e', '#83E377', '#B9E769', '#EFEA5A', '#F1C453', '#F29E4C'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment