Skip to content

Commit

Permalink
bug fixes and tested examples
Browse files Browse the repository at this point in the history
  • Loading branch information
BrechtBa committed Jul 8, 2016
1 parent 7ced017 commit 91c103b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 41 deletions.
2 changes: 1 addition & 1 deletion examples/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# some lines
plt.figure()
x = np.linspace(0,2*np.pi,100)
plt.plot(x,np.sin(x- 0*2*np.pi/12),color=pt.color['m'],label='m')
plt.plot(x,np.sin(x- 0*2*np.pi/12),color=pt.color['l'],label='l')
plt.plot(x,np.sin(x- 2*2*np.pi/12),color=pt.color['b'],label='b')
plt.plot(x,np.sin(x- 4*2*np.pi/12),color=pt.color['r'],label='r')
plt.plot(x,np.sin(x- 6*2*np.pi/12),color=pt.color['y'],label='y')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ def plot_colors(ax,coldict,order={}):
plt.ylim([0,5])


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


def change_lightness_to_match_greyscale(col,grey):
def change_lightness_to_match_grayscale(col,gray):
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))
gs.append(rgb_to_gray(ri,gi,bi))


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

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

Expand All @@ -45,59 +45,59 @@ def change_lightness_to_match_greyscale(col,grey):



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 = []
def linearize_grayscale(colors_rgb,gray_min,gray_max,order={},plot=False):
# convert all colors ro grayscale and determine the order
colors_gray_rgb = {}
gray = []
keys = []
for k,c in colors_rgb.items():
g = rgb_to_grey(c[0],c[1],c[2])
g = rgb_to_gray(c[0],c[1],c[2])
keys.append(k)
grey.append(g)
colors_grey_rgb[k] = (g,g,g)
gray.append(g)
colors_gray_rgb[k] = (g,g,g)


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




# rescale to greyscale min-max
grey_edit = np.linspace(grey_min,grey_max,len(order.keys()))
# rescale to grayscale min-max
gray_edit = np.linspace(gray_min,gray_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]])
colors_edit_rgb[k] = change_lightness_to_match_grayscale(c,gray_edit[order[k]])


# convert all colors ro greyscale and determine the order
colors_edit_grey_rgb = {}
# convert all colors ro grayscale and determine the order
colors_edit_gray_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)
g = rgb_to_gray(c[0],c[1],c[2])
colors_edit_gray_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))
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)
plot_colors(ax2,colors_gray_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)
plot_colors(ax4,colors_edit_gray_rgb,order)


return colors_edit_rgb
Expand Down Expand Up @@ -130,10 +130,10 @@ def linearize_greyscale(colors_rgb,grey_min,grey_max,order={},plot=False):
}

print('\nbase colors:')
linearize_greyscale(colors_rgb,0.20,0.90,order=order,plot=True)
linearize_grayscale(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)
linearize_grayscale(colors_rgb,0.40,0.95,order=order,plot=True)


plt.show()
32 changes: 16 additions & 16 deletions plottools/plotcolor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@
################################################################################
# colors that look ok and have considerable contrast in grayscale
basecolors = {
'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)
'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 = {
'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)
'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)
}

Expand Down

0 comments on commit 91c103b

Please sign in to comment.