Skip to content

Commit

Permalink
added functions to linearize the grey scales of a color library and c…
Browse files Browse the repository at this point in the history
…hanged the colors
  • Loading branch information
BrechtBa committed Jul 8, 2016
1 parent 808e52b commit 7ced017
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 33 deletions.
139 changes: 139 additions & 0 deletions plottools/linearize_greyscale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import colorsys



def plot_colors(ax,coldict,order={}):
n = len(coldict.keys())

if order == {}:
for i,k in enumerate(coldict.keys()):
order[k] = i

for k,c in coldict.items():
o = order[k]
ax.add_patch( patches.Rectangle( (o, 0), 1.0, 5.0, facecolor=c, edgecolor='none') )
ax.text(o+0.5,2.5,k,ha='center')

plt.xlim([0,n])
plt.ylim([0,5])


def rgb_to_grey(r,g,b):
return 0.21*r + 0.72*g + 0.07*b


def change_lightness_to_match_greyscale(col,grey):
h,l,s = colorsys.rgb_to_hls(col[0],col[1],col[2])

ls = np.linspace(0,1,100)
gs = []

for li in ls:
ri,gi,bi = colorsys.hls_to_rgb(h,li,s)
gs.append(rgb_to_grey(ri,gi,bi))


ll = np.interp(grey,gs,ls)

newcol = colorsys.hls_to_rgb(h,ll,s)

return newcol




def linearize_greyscale(colors_rgb,grey_min,grey_max,order={},plot=False):
# convert all colors ro greyscale and determine the order
colors_grey_rgb = {}
grey = []
keys = []
for k,c in colors_rgb.items():
g = rgb_to_grey(c[0],c[1],c[2])
keys.append(k)
grey.append(g)
colors_grey_rgb[k] = (g,g,g)


# define the order
if order == {}:
for i,o in enumerate(np.argsort(grey)):
order[keys[o]] = i




# rescale to greyscale min-max
grey_edit = np.linspace(grey_min,grey_max,len(order.keys()))
colors_edit_rgb = {}
for k,c in colors_rgb.items():
colors_edit_rgb[k] = change_lightness_to_match_greyscale(c,grey_edit[order[k]])


# convert all colors ro greyscale and determine the order
colors_edit_grey_rgb = {}
for k,c in colors_edit_rgb.items():
g = rgb_to_grey(c[0],c[1],c[2])
colors_edit_grey_rgb[k] = (g,g,g)


# print final colors
if plot:
for k,c in colors_edit_rgb.items():
print( '\'{}\': ({:>3.0f}./255,{:>3.0f}./255,{:>3.0f}./255)'.format(k,c[0]*255,c[1]*255,c[2]*255))

fig = plt.figure()
ax1 = fig.add_subplot(221)
plot_colors(ax1,colors_rgb,order)
plt.title('original')

ax2 = fig.add_subplot(223)
plot_colors(ax2,colors_grey_rgb,order)

ax3 = fig.add_subplot(222)
plot_colors(ax3,colors_edit_rgb,order)
plt.title('edited')

ax4 = fig.add_subplot(224)
plot_colors(ax4,colors_edit_grey_rgb,order)


return colors_edit_rgb


if __name__ == '__main__':

colors_rgb = {
'r': (245./255, 82./255, 45./255),
'o': (244./255,154./255, 26./255),
'y': (242./255,244./255, 66./255),
'g': ( 32./255, 81./255, 37./255),
'l': ( 80./255,180./255, 54./255),
'a': ( 95./255,173./255,209./255),
'b': ( 31./255, 57./255,101./255),
'p': (114./255, 31./255,119./255),
'k': ( 49./255, 49./255, 49./255)
}

order = {
'k': 0,
'p': 1,
'b': 2,
'r': 3,
'g': 4,
'o': 5,
'l': 6,
'a': 7,
'y': 8
}

print('\nbase colors:')
linearize_greyscale(colors_rgb,0.20,0.90,order=order,plot=True)

print('\nlight colors:')
linearize_greyscale(colors_rgb,0.40,0.95,order=order,plot=True)


plt.show()
57 changes: 24 additions & 33 deletions plottools/plotcolor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,51 +25,42 @@
################################################################################
# colors that look ok and have considerable contrast in grayscale
basecolors = {
'k': ( 40./255, 40./255, 40./255),
'n': ( 0./255, 56./255,190./255),
'p': (115./255, 45./255,145./255),
'm': (186./255, 57./255, 0./255),
'b': ( 33./255, 96./255,255./255),
'g': ( 78./255,123./255, 43./255),
'r': (224./255, 95./255, 86./255),
't': ( 53./255,158./255,148./255),
'o': (178./255,152./255, 22./255),
'f': (214./255,144./255,198./255),
'l': ( 99./255,205./255,107./255),
'a': (119./255,211./255,189./255),
'y': (205./255,220./255, 35./255) }
'a': (175./255,214./255,232./255)
'b': ( 54./255,100./255,177./255)
'g': ( 66./255,168./255, 77./255)
'k': ( 51./255, 51./255, 51./255)
'l': (126./255,210./255,104./255)
'o': (244./255,152./255, 23./255)
'p': (153./255, 42./255,160./255)
'r': (245./255, 87./255, 51./255)
'y': (241./255,243./255, 53./255)
}

lightcolors = {
'k': ( 60./255, 60./255, 60./255),
'n': ( 0./255, 68./255,230./255),
'p': (139./255, 54./255,176./255),
'm': (226./255, 69./255, 0./255),
'b': ( 73./255,125./255,255./255),
'g': ( 97./255,153./255, 53./255),
'r': (230./255,127./255,120./255),
't': ( 64./255,188./255,176./255),
'o': (214./255,182./255, 26./255),
'f': (225./255,173./255,213./255),
'l': (129./255,215./255,136./255),
'a': (149./255,221./255,204./255),
'y': (213./255,226./255, 70./255) }
'a': (204./255,229./255,240./255)
'b': (101./255,141./255,208./255)
'g': (107./255,197./255,116./255)
'k': (102./255,102./255,102./255)
'l': (167./255,224./255,152./255)
'o': (247./255,183./255, 91./255)
'p': (204./255, 86./255,211./255)
'r': (248./255,132./255,106./255)
'y': (248./255,249./255,154./255)
}

longnames = {
'black': 'k',
'navy': 'n',
'purple': 'p',
'maroon': 'm',
'blue': 'b',
'green': 'g',
'red': 'r',
'teal': 't',
'olive': 'o',
'fuchsia': 'f',
'orange': 'o',
'lime': 'l',
'aqua': 'a',
'yellow': 'y' }
'yellow': 'y'
}

basecycle = ['b','r','l','y','m','o','a','n','p','g','t','f','k']
basecycle = ['b','r','g','o','y','p','l','a','k']


################################################################################
Expand Down

0 comments on commit 7ced017

Please sign in to comment.