%matplotlib notebook
import matplotlib.pyplot as plt
from Goulib.notebook import *
from Goulib.colors import *
Color('#3C14DC')
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')
h(red,green,blue,cmyk,lab) #colors automagically recieve a name and have an HTML representation
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
#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 !
magenta-cyan-yellow #colors can be substracted too
-cmyk # unary minus gives the complementary color
c= Color('gray')-red # colors can be substracted too.
h(c,c.hex) # notice RGB values were bounded
# Colors can be initialized and converted to-from any of the following colorspaces
colorspaces
('CMYK', 'XYZ', 'xyY', 'Lab', 'Luv', 'HSV', 'RGB', 'HEX')
#each Color keeps track of the colorspace used at construction
print(blue.space,red.space,cmyk.space,lab.space)
rgb hex cmyk lab
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
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)
#converters #will produce a nice graph of converters but it's still ugly
h(color_range(8,'red','blue')) # ranges are generated in by linear interpolation in 'hsv' by default
h(color_range(8,'red','blue','xyz')) # but another colorspace can be specified
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)
#Palettes can also be inited from Matplotlib colormaps
from matplotlib import cm
Palette(cm.nipy_spectral) # discretized to 256 levels by default
# 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'])
pantone #responsive multiline, with popups
ColorTable(pantone,lambda c:int(c.name[:-1]),15) #another representation is available
100C | 101C | 102C | 103C | 104C | 105C | 106C | 107C | 108C | 109C | 110C | 111C | 112C | 113C | 114C |
115C | 116C | 117C | 118C | 119C | 120C | 121C | 122C | 123C | 124C | 125C | 126C | 127C | 128C | 129C |
130C | 131C | 132C | 133C | 134C | 135C | 136C | 137C | 138C | 139C | 140C | 141C | 142C | 143C | 144C |
145C | 146C | 147C | 148C | 149C | 150C | 151C | 152C | 153C | 154C | 155C | 156C | 157C | 158C | 159C |
160C | 161C | 162C | 163C | 164C | 165C | 166C | 167C | 168C | 169C | 170C | 171C | 172C | 173C | 174C |
175C | 176C | 177C | 178C | 179C | 180C | 181C | 182C | 183C | 184C | 185C | 186C | 187C | 188C | 189C |
190C | 191C | 192C | 193C | 194C | 196C | 197C | 198C | 199C | 200C | 201C | 202C | 203C | 204C | 205C |
206C | 207C | 208C | 209C | 210C | 211C | 212C | 213C | 214C | 215C | 216C | 217C | 218C | 219C | 220C |
221C | 222C | 223C | 224C | 225C | 226C | 227C | 228C | 229C | 230C | 231C | 232C | 233C | 234C | 235C |
236C | 237C | 238C | 239C | 240C | 241C | 242C | 243C | 244C | 245C | 246C | 247C | 248C | 249C | 250C |
251C | 252C | 253C | 254C | 255C | 256C | 257C | 258C | 259C | 260C | 261C | 262C | 263C | 264C | 265C |
266C | 267C | 268C | 269C | 270C | 271C | 272C | 273C | 274C | 275C | 276C | 277C | 278C | 279C | 280C |
281C | 282C | 283C | 284C | 285C | 286C | 287C | 288C | 289C | 290C | 291C | 292C | 293C | 294C | 295C |
296C | 297C | 298C | 299C | 300C | 301C | 302C | 303C | 304C | 305C | 306C | 307C | 308C | 309C | 310C |
311C | 312C | 313C | 314C | 315C | 316C | 317C | 318C | 319C | 320C | 321C | 322C | 323C | 324C | 325C |
326C | 327C | 328C | 329C | 330C | 331C | 332C | 333C | 334C | 335C | 336C | 337C | 338C | 339C | 340C |
341C | 342C | 343C | 344C | 345C | 346C | 347C | 348C | 349C | 350C | 351C | 352C | 353C | 354C | 355C |
356C | 357C | 358C | 359C | 360C | 361C | 362C | 363C | 364C | 365C | 366C | 367C | 368C | 369C | 370C |
371C | 372C | 373C | 374C | 375C | 376C | 377C | 378C | 379C | 380C | 381C | 382C | 383C | 384C | 385C |
386C | 387C | 388C | 389C | 390C | 391C | 392C | 393C | 394C | 395C | 396C | 397C | 398C | 399C | 400C |
401C | 402C | 403C | 404C | 405C | 406C | 408C | 409C | 410C | 411C | 412C | 413C | 414C | 415C | 416C |
417C | 418C | 419C | 420C | 421C | 422C | 423C | 424C | 425C | 426C | 427C | 428C | 429C | 430C | 431C |
432C | 433C | 434C | 435C | 436C | 437C | 438C | 439C | 440C | 441C | 442C | 443C | 444C | 445C | 446C |
447C | 448C | 449C | 450C | 451C | 452C | 453C | 454C | 455C | 456C | 457C | 458C | 459C | 460C | 461C |
462C | 463C | 464C | 465C | 466C | 467C | 468C | 469C | 470C | 471C | 472C | 473C | 474C | 475C | 476C |
477C | 478C | 479C | 480C | 481C | 482C | 483C | 484C | 485C | 486C | 487C | 488C | 489C | 490C | 491C |
492C | 494C | 495C | 496C | 497C | 498C | 499C | 500C | 501C | 502C | 503C | 504C | 505C | 506C | 507C |
508C | 509C | 510C | 511C | 512C | 513C | 514C | 515C | 516C | 517C | 518C | 519C | 520C | 521C | 522C |
523C | 524C | 525C | 526C | 527C | 528C | 529C | 530C | 531C | 532C | 533C | 534C | 535C | 536C | 537C |
538C | 539C | 540C | 541C | 542C | 543C | 544C | 545C | 546C | 547C | 548C | 549C | 550C | 551C | 552C |
553C | 554C | 555C | 556C | 557C | 558C | 559C | 560C | 561C | 562C | 563C | 564C | 565C | 566C | 567C |
569C | 570C | 571C | 572C | 573C | 574C | 575C | 576C | 577C | 578C | 579C | 580C | 581C | 582C | 583C |
584C | 585C | 586C | 587C | 600C | 601C | 602C | 603C | 604C | 605C | 606C | 607C | 608C | 609C | 610C |
611C | 612C | 613C | 614C | 615C | 616C | 617C | 618C | 619C | 620C | 621C | 622C | 623C | 624C | 625C |
626C | 627C | 628C | 629C | 630C | 631C | 632C | 633C | 634C | 635C | 636C | 637C | 638C | 639C | 640C |
641C | 642C | 643C | 644C | 645C | 646C | 647C | 648C | 649C | 650C | 651C | 652C | 653C | 654C | 655C |
656C | 657C | 658C | 659C | 660C | 661C | 662C | 663C | 664C | 665C | 666C | 667C | 668C | 669C | 670C |
671C | 672C | 673C | 674C | 675C | 676C | 677C | 678C | 679C | 680C | 681C | 682C | 683C | 684C | 685C |
686C | 687C | 688C | 689C | 690C | 691C | 692C | 693C | 694C | 695C | 696C | 697C | 698C | 699C | 700C |
701C | 702C | 703C | 704C | 705C | 706C | 707C | 708C | 709C | 710C | 711C | 712C | 713C | 714C | 715C |
716C | 717C | 718C | 719C | 720C | 721C | 722C | 723C | 724C | 725C | 726C | 727C | 728C | 729C | 730C |
731C | 732C | 801C | 802C | 803C | 804C | 805C | 806C | 807C | 808C | 809C | 810C | 811C | 812C | 813C |
814C | 1205C | 1215C | 1225C | 1235C | 1245C | 1255C | 1265C | 1345C | 1355C | 1365C | 1375C | 1385C | 1395C | 1405C |
1485C | 1495C | 1505C | 1525C | 1535C | 1545C | 1555C | 1565C | 1575C | 1585C | 1595C | 1605C | 1615C | 1625C | 1635C |
1645C | 1655C | 1665C | 1675C | 1685C | 1765C | 1767C | 1775C | 1777C | 1785C | 1787C | 1788C | 1795C | 1797C | 1805C |
1807C | 1815C | 1817C | 1895C | 1905C | 1915C | 1925C | 1935C | 1945C | 1955C | 2365C | 2375C | 2385C | 2395C | 2405C |
2415C | 2425C | 2562C | 2563C | 2567C | 2572C | 2573C | 2577C | 2582C | 2583C | 2587C | 2592C | 2593C | 2597C | 2602C |
2603C | 2607C | 2612C | 2613C | 2617C | 2622C | 2623C | 2627C | 2635C | 2645C | 2655C | 2665C | 2685C | 2695C | 2705C |
2706C | 2707C | 2708C | 2715C | 2716C | 2717C | 2718C | 2725C | 2726C | 2727C | 2728C | 2735C | 2736C | 2738C | 2745C |
2746C | 2747C | 2748C | 2755C | 2756C | 2757C | 2758C | 2765C | 2766C | 2767C | 2768C | 2905C | 2915C | 2925C | 2935C |
2945C | 2955C | 2965C | 2975C | 2985C | 2995C | 3005C | 3015C | 3025C | 3035C | 3105C | 3115C | 3125C | 3135C | 3145C |
3155C | 3165C | 3242C | 3245C | 3248C | 3252C | 3255C | 3258C | 3262C | 3265C | 3268C | 3272C | 3275C | 3278C | 3282C |
3285C | 3288C | 3292C | 3295C | 3298C | 3302C | 3305C | 3308C | 3375C | 3385C | 3395C | 3405C | 3415C | 3425C | 3435C |
3935C | 3945C | 3955C | 3965C | 3975C | 3985C | 3995C | 4485C | 4495C | 4505C | 4515C | 4525C | 4535C | 4545C | 4625C |
4635C | 4645C | 4655C | 4665C | 4675C | 4685C | 4695C | 4705C | 4715C | 4725C | 4735C | 4745C | 4755C | 4975C | 4985C |
4995C | 5005C | 5015C | 5025C | 5035C | 5115C | 5125C | 5135C | 5145C | 5155C | 5165C | 5175C | 5185C | 5195C | 5205C |
5215C | 5225C | 5235C | 5245C | 5255C | 5265C | 5275C | 5285C | 5295C | 5305C | 5315C | 5395C | 5405C | 5415C | 5425C |
5435C | 5445C | 5455C | 5463C | 5467C | 5473C | 5477C | 5483C | 5487C | 5493C | 5497C | 5503C | 5507C | 5513C | 5517C |
5523C | 5527C | 5535C | 5545C | 5555C | 5565C | 5575C | 5585C | 5595C | 5605C | 5615C | 5625C | 5635C | 5645C | 5655C |
5665C | 5743C | 5747C | 5753C | 5757C | 5763C | 5767C | 5773C | 5777C | 5783C | 5787C | 5793C | 5797C | 5803C | 5807C |
5815C | 5825C | 5835C | 5845C | 5855C | 5865C | 5875C |
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])
ColorCube(pantone)
plt.show()
# 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
(47.0, 68.0, 45.0)
p=nearest_color(cmyk,pantone) # find best matching Pantone color
h(p,cmyk,deltaE(p,cmyk))
x,y,z,c,l=ColorCoords(pantone,'xyz','rgb')
plt.scatter(x,y,s=z,c=c) #already cool
plt.show()
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)
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x1d09bf5af60>
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'