Cufflinks also provides a wide set of tools for color managements; including color conversion across multiple spectrums and color table generation.
import cufflinks as cl
Colors can be represented as strings:
HEX "#db4052"
RGB "rgb(219, 64, 82)"
RGBA "rgba(219, 64, 82, 1.0)"
# The colors module includes a pre-defined set of commonly used colors
red=cl.red
red
'#db4052'
# HEX to RGB
cl.hex_to_rgb(red)
'rgb(219, 64, 82)'
# RGB to HEX
cl.rgb_to_hex('rgb(219, 64, 82)')
'#db4052'
# RGB or HEX to RGBA (transparency)
cl.to_rgba('#3780bf',.5), cl.to_rgba('rgb(219, 64, 82)',.4)
('rgba(55, 128, 191, 0.5)', 'rgba(219, 64, 82, 0.4)')
# RGBA to RGB (flatten transparency)
# By default assumes that the transparency color is *white*, however this can be also passed as a parameter.
bg=cl.white
cl.rgba_to_rgb('rgba(219, 64, 82, 0.4)',bg)
'rgb(240, 178, 185)'
# Cufflinks.colors.normalize will always return the an hex value for all types of colors
colors=['#f08','rgb(240, 178, 185)','rgba(219, 64, 82, 0.4)',cl.green]
[cl.normalize(c) for c in colors]
['#ff0088', '#f0b2b9', '#f0b2b9', '#32ab60']
A range of colors can be generated using a base color and varying the saturation.
# 10 different tones of pink
cl.color_range(cl.pink,10)
['#000000', '#33001b', '#660036', '#990051', '#cc006c', '#ff0088', '#ff329f', '#ff65b7', '#ff99cf', '#ffcce7', '#ffffff']
This function is meant to be used in an iPython Notebook. It generates an HTML table to display either a defined list of colors or to automatically generate a range of colors.
# Displaying a table of defined colors (list)
colors=['#f08', 'rgb(240, 178, 185)', cl.blue , '#32ab60']
cl.color_table(colors)
# Generating 15 shades of orange
cl.color_table(cl.orange,15)
A color generator can be used to produce shades of colors in an iterative form. For example when plotting N timeseries so the color used are as distinctive as possible.
# Create a generator using 3 defined base colors
colors=[cl.green,cl.orange,cl.blue]
gen=cl.colorgen(colors)
outputColors=[gen.next() for _ in range(15)]
cl.color_table(outputColors)
# Create a generator with default set of colors
gen=cl.colorgen()
outputColors=[gen.next() for _ in range(15)]
cl.color_table(outputColors)