#!/usr/bin/env python # coding: utf-8 # # [Goulib](../notebook.ipynb).colors # color toolbox # # In[1]: get_ipython().run_line_magic('matplotlib', 'notebook') import matplotlib.pyplot as plt # In[2]: from Goulib.notebook import * from Goulib.colors import * # ## Color objects # In[3]: Color('#3C14DC') # In[4]: red=Color('red') #Color objects can be init'ed by a name (as in matplotlib or html) green=Color('#00ff00') # by RGB hex string, blue=Color((0,0,1)) # by RGB triplet cmyk=Color((.45,.12,.67,.05),'cmyk') # or by specifying the colorspace used lab=Color((47.0, 68.0, 45.0),'Lab') # In[5]: h(red,green,blue,cmyk,lab) #colors automagically recieve a name and have an HTML representation # In[6]: from random import random r=[Color((random(),random(),random())) for _ in range(10)] h(*r) # unknown colors recieve name of the nearest known color with a tile before # In[7]: #colors can be added cyan=blue+green magenta=blue+red yellow=red+green h(cyan, magenta, yellow) # see ? the names of calculated colors are correct ! # In[8]: magenta-cyan-yellow #colors can be substracted too # In[9]: -cmyk # unary minus gives the complementary color # In[10]: c= Color('gray')-red # colors can be substracted too. h(c,c.hex) # notice RGB values were bounded # ## Colorspaces and conversion # In[11]: # Colors can be initialized and converted to-from any of the following colorspaces colorspaces # In[12]: #each Color keeps track of the colorspace used at construction print(blue.space,red.space,cmyk.space,lab.space) # In[13]: for s in colorspaces: print(s,'=',red.convert(s)) # In[14]: c=Color(cmyk.lab,'lab') print(cmyk.lab,c.lab) print(cmyk.cmyk,c.cmyk) # In[15]: #converters #will produce a nice graph of converters but it's still ugly # ## Color ranges # In[16]: h(color_range(8,'red','blue')) # ranges are generated in by linear interpolation in 'hsv' by default # In[17]: h(color_range(8,'red','blue','xyz')) # but another colorspace can be specified # ## Palettes # In[18]: Palette(color_range(12,'red','blue','lab')) # Palette can be inited from a color list, #palettes have a responsive HTML repr with popups (hover the cursor over the bar to see it) # In[19]: #Palettes can also be inited from Matplotlib colormaps from matplotlib import cm Palette(cm.nipy_spectral) # discretized to 256 levels by default # In[20]: # several Paletets of standard colors are predefined: h('color contains',len(color),'web colors indexed by name :',color['blue']) h('color_lookup contains',len(color_lookup),'web colors indexed by hex :',color_lookup['#808000']) h('acadcolors contains',len(acadcolors),'Autocad ACI colors indexed by int :',acadcolors[0:16]) h('pantone contains',len(pantone),'Pantone colors indexed by name :',pantone['1795C']) # In[21]: pantone #responsive multiline, with popups # In[22]: ColorTable(pantone,lambda c:int(c.name[:-1]),15) #another representation is available # In[23]: def ColorCoords(colors,mode,cmode='rgb'): """coordinates of a color map""" x,y,z,c,l=[],[],[],[],[] for id in colors: p=colors[id] v=p.convert(mode) x.append(v[1]) y.append(v[2]) z.append(v[0]) c.append(p.convert(cmode)) l.append(p.name) return x,y,z,c,l def ColorCube(colors,mode='xyY',s=50): """draw an interactive color cube of colors""" x,y,z,c,l=ColorCoords(colors,mode,'hex') fig = plt.figure() from mpl_toolkits.mplot3d import Axes3D ax = fig.add_subplot(111,projection='3d') ax.scatter(x,y,z,c=c,s=s,lw=0,depthshade=False) ax.set_xlabel(mode[1]) ax.set_ylabel(mode[2]) ax.set_zlabel(mode[0]) # In[24]: ColorCube(pantone) plt.show() # ## Pantone colors # In[25]: # pantone is a dict of Colors initialized from Lab values bred=pantone['1795C'] #BOBST Red bred2=Color(bred.hex) #the corresponding RGB color h(bred,bred2,deltaE(bred,bred2)) bred.lab # In[26]: p=nearest_color(cmyk,pantone) # find best matching Pantone color h(p,cmyk,deltaE(p,cmyk)) # In[27]: x,y,z,c,l=ColorCoords(pantone,'xyz','rgb') plt.scatter(x,y,s=z,c=c) #already cool plt.show() # In[28]: fig = plt.figure() from mpl_toolkits.mplot3d import Axes3D ax = Axes3D(fig) ax.scatter(x,y,z,c=c,s=100,lw=0,depthshade=False) # In[29]: import mpld3 # looks much cooler ! seen on https://mpld3.github.io/examples/scatter_tooltip.html mpld3.enable_notebook() fig = plt.figure() ax = fig.gca(projection='3d') scatter = ax.scatter(x,y,c=c,lw=10,s=z) #tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=l) #mpld3.plugins.connect(fig, tooltip) mpld3.display()