Goulib.colors

color toolbox


In [1]:
%matplotlib notebook 
import matplotlib.pyplot as plt

In [2]:
from Goulib.notebook import *
from Goulib.colors import *

Color objects


In [3]:
Color('#3C14DC')


Out[3]:
~blue

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


red lime blue ~yellowgreen ~crimson

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


~palevioletred ~seagreen ~mediumorchid ~palevioletred ~midnightblue ~royalblue ~purple ~blue ~thistle ~mediumvioletred

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 !


cyan magenta yellow

In [8]:
magenta-cyan-yellow #colors can be substracted too


Out[8]:
black

In [9]:
-cmyk # unary minus gives the complementary color


Out[9]:
~darkviolet

In [10]:
c= Color('gray')-red # colors can be substracted too. 
h(c,c.hex) # notice RGB values were bounded


teal #008080

Colorspaces and conversion


In [11]:
# Colors can be initialized and converted to-from any of the following colorspaces
colorspaces


Out[11]:
('CMYK', 'XYZ', 'xyY', 'Lab', 'Luv', 'HSV', 'RGB', 'HEX')

In [12]:
#each Color keeps track of the colorspace used at construction
print(blue.space,red.space,cmyk.space,lab.space)


rgb hex cmyk lab

In [13]:
for s in colorspaces:
    print(s,'=',red.convert(s))


CMYK = (0.0, 1.0, 1.0, 0.0)
XYZ = (0.412453, 0.212671, 0.019334)
xyY = (0.6399998137970202, 0.32999978276319014, 0.212671)
Lab = (53.2405879437449, 80.0923082256922, 67.2027510444287)
Luv = (53.2405879437449, 175.0144735628877, 37.75617373935089)
HSV = (0.0, 1.0, 1.0)
RGB = (1.0, 0.0, 0.0)
HEX = #ff0000

In [14]:
c=Color(cmyk.lab,'lab')
print(cmyk.lab,c.lab)
print(cmyk.cmyk,c.cmyk)


(78.0294899763116, -46.93394514696031, 56.55175935198822) (78.0294899763116, -46.93394514696031, 56.55175935198822)
(0.45, 0.12, 0.67, 0.05) (0.37500000000000044, 0.0, 0.6250000000000002, 0.1639999999999998)

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


[red,~darkorange,~yellow,~lime,~lime,~aquamarine,~dodgerblue,blue]

In [17]:
h(color_range(8,'red','blue','xyz')) # but another colorspace can be specified


[red,~deeppink,~mediumvioletred,~mediumvioletred,~darkorchid,~darkviolet,~blue,blue]

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)


Out[18]:
 
 
 
 
 
 
 
 
 
 
 
 

In [19]:
#Palettes can also be inited from Matplotlib colormaps
from matplotlib import cm
Palette(cm.nipy_spectral) # discretized to 256 levels by default


Out[19]:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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'])


color contains 144 web colors indexed by name : blue
color_lookup contains 138 web colors indexed by hex : olive
acadcolors contains 256 Autocad ACI colors indexed by int : [black,red,yellow,lime,cyan,blue,magenta,white,8,gray,red,11,12,13,14,15]
pantone contains 907 Pantone colors indexed by name : 1795C

In [21]:
pantone #responsive multiline, with popups


Out[21]:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

In [22]:
ColorTable(pantone,lambda c:int(c.name[:-1]),15) #another representation is available


Out[22]:
100C101C102C103C104C105C106C107C108C109C110C111C112C113C114C
115C116C117C118C119C120C121C122C123C124C125C126C127C128C129C
130C131C132C133C134C135C136C137C138C139C140C141C142C143C144C
145C146C147C148C149C150C151C152C153C154C155C156C157C158C159C
160C161C162C163C164C165C166C167C168C169C170C171C172C173C174C
175C176C177C178C179C180C181C182C183C184C185C186C187C188C189C
190C191C192C193C194C196C197C198C199C200C201C202C203C204C205C
206C207C208C209C210C211C212C213C214C215C216C217C218C219C220C
221C222C223C224C225C226C227C228C229C230C231C232C233C234C235C
236C237C238C239C240C241C242C243C244C245C246C247C248C249C250C
251C252C253C254C255C256C257C258C259C260C261C262C263C264C265C
266C267C268C269C270C271C272C273C274C275C276C277C278C279C280C
281C282C283C284C285C286C287C288C289C290C291C292C293C294C295C
296C297C298C299C300C301C302C303C304C305C306C307C308C309C310C
311C312C313C314C315C316C317C318C319C320C321C322C323C324C325C
326C327C328C329C330C331C332C333C334C335C336C337C338C339C340C
341C342C343C344C345C346C347C348C349C350C351C352C353C354C355C
356C357C358C359C360C361C362C363C364C365C366C367C368C369C370C
371C372C373C374C375C376C377C378C379C380C381C382C383C384C385C
386C387C388C389C390C391C392C393C394C395C396C397C398C399C400C
401C402C403C404C405C406C408C409C410C411C412C413C414C415C416C
417C418C419C420C421C422C423C424C425C426C427C428C429C430C431C
432C433C434C435C436C437C438C439C440C441C442C443C444C445C446C
447C448C449C450C451C452C453C454C455C456C457C458C459C460C461C
462C463C464C465C466C467C468C469C470C471C472C473C474C475C476C
477C478C479C480C481C482C483C484C485C486C487C488C489C490C491C
492C494C495C496C497C498C499C500C501C502C503C504C505C506C507C
508C509C510C511C512C513C514C515C516C517C518C519C520C521C522C
523C524C525C526C527C528C529C530C531C532C533C534C535C536C537C
538C539C540C541C542C543C544C545C546C547C548C549C550C551C552C
553C554C555C556C557C558C559C560C561C562C563C564C565C566C567C
569C570C571C572C573C574C575C576C577C578C579C580C581C582C583C
584C585C586C587C600C601C602C603C604C605C606C607C608C609C610C
611C612C613C614C615C616C617C618C619C620C621C622C623C624C625C
626C627C628C629C630C631C632C633C634C635C636C637C638C639C640C
641C642C643C644C645C646C647C648C649C650C651C652C653C654C655C
656C657C658C659C660C661C662C663C664C665C666C667C668C669C670C
671C672C673C674C675C676C677C678C679C680C681C682C683C684C685C
686C687C688C689C690C691C692C693C694C695C696C697C698C699C700C
701C702C703C704C705C706C707C708C709C710C711C712C713C714C715C
716C717C718C719C720C721C722C723C724C725C726C727C728C729C730C
731C732C801C802C803C804C805C806C807C808C809C810C811C812C813C
814C1205C1215C1225C1235C1245C1255C1265C1345C1355C1365C1375C1385C1395C1405C
1485C1495C1505C1525C1535C1545C1555C1565C1575C1585C1595C1605C1615C1625C1635C
1645C1655C1665C1675C1685C1765C1767C1775C1777C1785C1787C1788C1795C1797C1805C
1807C1815C1817C1895C1905C1915C1925C1935C1945C1955C2365C2375C2385C2395C2405C
2415C2425C2562C2563C2567C2572C2573C2577C2582C2583C2587C2592C2593C2597C2602C
2603C2607C2612C2613C2617C2622C2623C2627C2635C2645C2655C2665C2685C2695C2705C
2706C2707C2708C2715C2716C2717C2718C2725C2726C2727C2728C2735C2736C2738C2745C
2746C2747C2748C2755C2756C2757C2758C2765C2766C2767C2768C2905C2915C2925C2935C
2945C2955C2965C2975C2985C2995C3005C3015C3025C3035C3105C3115C3125C3135C3145C
3155C3165C3242C3245C3248C3252C3255C3258C3262C3265C3268C3272C3275C3278C3282C
3285C3288C3292C3295C3298C3302C3305C3308C3375C3385C3395C3405C3415C3425C3435C
3935C3945C3955C3965C3975C3985C3995C4485C4495C4505C4515C4525C4535C4545C4625C
4635C4645C4655C4665C4675C4685C4695C4705C4715C4725C4735C4745C4755C4975C4985C
4995C5005C5015C5025C5035C5115C5125C5135C5145C5155C5165C5175C5185C5195C5205C
5215C5225C5235C5245C5255C5265C5275C5285C5295C5305C5315C5395C5405C5415C5425C
5435C5445C5455C5463C5467C5473C5477C5483C5487C5493C5497C5503C5507C5513C5517C
5523C5527C5535C5545C5555C5565C5575C5585C5595C5605C5615C5625C5635C5645C5655C
5665C5743C5747C5753C5757C5763C5767C5773C5777C5783C5787C5793C5797C5803C5807C
5815C5825C5835C5845C5855C5865C5875C

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


1795C ~crimson 2.0831923277040305
Out[25]:
(47.0, 68.0, 45.0)

In [26]:
p=nearest_color(cmyk,pantone) # find best matching Pantone color
h(p,cmyk,deltaE(p,cmyk))


802C ~yellowgreen 3.2861652596045845

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)


Out[28]:
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x1d09bf5af60>

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()


---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-29-9468ea357f15> in <module>
----> 1 import mpld3 # looks much cooler ! seen on https://mpld3.github.io/examples/scatter_tooltip.html
      2 mpld3.enable_notebook()
      3 fig = plt.figure()
      4 ax = fig.gca(projection='3d')
      5 scatter = ax.scatter(x,y,c=c,lw=10,s=z)

ModuleNotFoundError: No module named 'mpld3'