ase_notebook.color module

Color Library.

Adapted from: https://github.com/vaab/colour/blob/master/colour.py vaab/colour is licensed under the BSD 2-Clause “Simplified” License

This module defines several color formats that can be converted to one or another.

Formats

  • HSL: 3-ple of Hue, Saturation, Lightness all between 0.0 and 1.0

  • RGB: 3-ple of Red, Green, Blue all between 0.0 and 1.0

  • HEX: string object beginning with ‘#’ and with red, green, blue value.

    This format accept color in 3 or 6 value ex: ‘#fff’ or ‘#ffffff’

  • WEB: string object that defaults to HEX representation or human if possible

Notes

Several function exists to convert from one format to another. But all function are not written. So the best way is to use the object Color.

Please see the documentation of this object for more information.

Note

Some constants are defined for convenience in HSL, RGB, HEX

class ase_notebook.color.Color(color=None, equality=None, **kwargs)[source]

Bases: object

Abstraction of a color object.

Color object keeps information of a color. It can input/output to different format (HSL, RGB, HEX, WEB) and their partial representation.

>>> from ase_notebook.color import Color, HSL
>>> b = Color()
>>> b.hsl = HSL.BLUE

Examples

>>> b.hue  
0.66...
>>> b.saturation
1.0
>>> b.luminance
0.5
>>> b.red
0.0
>>> b.blue
1.0
>>> b.green
0.0
>>> b.rgb
(0.0, 0.0, 1.0)
>>> b.hsl  
(0.66..., 1.0, 0.5)
>>> b.hex
'#00f'

Change values

Let’s change Hue toward red tint:

>>> b.hue = 0.0
>>> b.hex
'#f00'
>>> b.hue = 2.0/3
>>> b.hex
'#00f'

In the other way round:

>>> b.hex = '#f00'
>>> b.hsl
(0.0, 1.0, 0.5)

Long hex can be accessed directly:

>>> b.hex_l = '#123456'
>>> b.hex_l
'#123456'
>>> b.hex
'#123456'
>>> b.hex_l = '#ff0000'
>>> b.hex_l
'#ff0000'
>>> b.hex
'#f00'

Convenience

>>> c = Color('blue')
>>> c
<Color blue>
>>> c.hue = 0
>>> c
<Color red>
>>> c.saturation = 0.0
>>> c.hsl  
(..., 0.0, 0.5)
>>> c.rgb
(0.5, 0.5, 0.5)
>>> c.hex
'#7f7f7f'
>>> c
<Color #7f7f7f>
>>> c.luminance = 0.0
>>> c
<Color black>
>>> c.hex
'#000'
>>> c.green = 1.0
>>> c.blue = 1.0
>>> c.hex
'#0ff'
>>> c
<Color cyan>
>>> c = Color('blue', luminance=0.75)
>>> c
<Color #7f7fff>
>>> c = Color('red', red=0.5)
>>> c
<Color #7f0000>
>>> print(c)
#7f0000

Recursive init

To support blind conversion of web strings (or already converted object), the Color object supports instantiation with another Color object.

>>> Color(Color(Color('red')))
<Color red>

Equality support

Default equality is RGB hex comparison:

>>> Color('red') == Color('blue')
False
>>> Color('red') == Color('red')
True
>>> Color('red') != Color('blue')
True
>>> Color('red') != Color('red')
False

But this can be changed:

>>> saturation_equality = lambda c1, c2: c1.luminance == c2.luminance
>>> Color('red', equality=saturation_equality) == Color('blue')
True
_hsl = None
property blue

Get value.

property green

Get value.

property hex

Get value.

property hex_l

Get value.

property hsl

Get value.

property hue

Get value.

property luminance

Get value.

property red

Get value.

property rgb

Get value.

property saturation

Get value.

property web

Get value.

class ase_notebook.color.ContainerHEX[source]

Bases: object

RGB colors container.

Provides a quick color access.

>>> from ase_notebook.color import HEX
>>> HEX.WHITE
'#fff'
>>> HEX.BLUE
'#00f'
>>> HEX.DONOTEXISTS  
Traceback (most recent call last):
...
AttributeError: ... has no attribute 'DONOTEXISTS'
class ase_notebook.color.ContainerHSL[source]

Bases: object

HSL colors container.

class ase_notebook.color.ContainerRGB[source]

Bases: object

RGB colors container.

Provides a quick color access.

>>> from ase_notebook.color import RGB
>>> RGB.WHITE
(1.0, 1.0, 1.0)
>>> RGB.BLUE
(0.0, 0.0, 1.0)
>>> RGB.DONOTEXISTS  
Traceback (most recent call last):
...
AttributeError: ... has no attribute 'DONOTEXISTS'
ase_notebook.color._hue2rgb(v1, v2, v_h)[source]

Private helper function (Do not call directly).

Parameters

vH – rotation around the chromatic circle (between 0..1)

ase_notebook.color.hex2hsl(x)
ase_notebook.color.hex2rgb(str_rgb)[source]

Transform hex RGB representation to RGB tuple.

Parameters

str_rgb – 3 hex char or 6 hex char string representation

Returns

RGB 3-uple of float between 0 and 1

>>> from ase_notebook.color import hex2rgb
>>> hex2rgb('#00ff00')
(0.0, 1.0, 0.0)
>>> hex2rgb('#0f0')
(0.0, 1.0, 0.0)
>>> hex2rgb('#aaa')  
(0.66..., 0.66..., 0.66...)
>>> hex2rgb('#aa')  
Traceback (most recent call last):
...
ValueError: Invalid value '#aa' provided for rgb color.
ase_notebook.color.hex2web(hex_color)[source]

Convert HEX representation to WEB.

Parameters

rgb – 3 hex char or 6 hex char string representation

Returns

web string representation (human readable if possible)

WEB representation uses X11 rgb.txt to define conversion between RGB and english color names.

Examples

>>> from ase_notebook.color import hex2web
>>> hex2web('#ff0000')
'red'
>>> hex2web('#aaaaaa')
'#aaa'
>>> hex2web('#abc')
'#abc'
>>> hex2web('#acacac')
'#acacac'
ase_notebook.color.hsl2hex(x)
ase_notebook.color.hsl2rgb(hsl)[source]

Convert HSL representation towards RGB.

Parameters
  • h – Hue, position around the chromatic circle (h=1 equiv h=0)

  • s – Saturation, color saturation (0=full gray, 1=full color)

  • l – Ligthness, Overhaul lightness (0=full black, 1=full white)

Returns

3-uple for RGB values in float between 0 and 1

Hue, Saturation, Range from Lightness is a float between 0 and 1

Note that Hue can be set to any value but as it is a rotation around the chromatic circle, any value above 1 or below 0 can be expressed by a value between 0 and 1 (Note that h=0 is equiv to h=1).

This algorithm came from: http://www.easyrgb.com/index.php?X=MATH&H=19#text19

Here are some quick notion of HSL to RGB conversion:

>>> from ase_notebook.color import hsl2rgb

With a lightness put at 0, RGB is always rgbblack

>>> hsl2rgb((0.0, 0.0, 0.0))
(0.0, 0.0, 0.0)
>>> hsl2rgb((0.5, 0.0, 0.0))
(0.0, 0.0, 0.0)
>>> hsl2rgb((0.5, 0.5, 0.0))
(0.0, 0.0, 0.0)

Same for lightness put at 1, RGB is always rgbwhite

>>> hsl2rgb((0.0, 0.0, 1.0))
(1.0, 1.0, 1.0)
>>> hsl2rgb((0.5, 0.0, 1.0))
(1.0, 1.0, 1.0)
>>> hsl2rgb((0.5, 0.5, 1.0))
(1.0, 1.0, 1.0)

With saturation put at 0, the RGB should be equal to Lightness:

>>> hsl2rgb((0.0, 0.0, 0.25))
(0.25, 0.25, 0.25)
>>> hsl2rgb((0.5, 0.0, 0.5))
(0.5, 0.5, 0.5)
>>> hsl2rgb((0.5, 0.0, 0.75))
(0.75, 0.75, 0.75)

With saturation put at 1, and lightness put to 0.5, we can find normal full red, green, blue colors:

>>> hsl2rgb((0 , 1.0, 0.5))
(1.0, 0.0, 0.0)
>>> hsl2rgb((1 , 1.0, 0.5))
(1.0, 0.0, 0.0)
>>> hsl2rgb((1.0/3 , 1.0, 0.5))
(0.0, 1.0, 0.0)
>>> hsl2rgb((2.0/3 , 1.0, 0.5))
(0.0, 0.0, 1.0)

Of course: >>> hsl2rgb((0.0, 2.0, 0.5)) # doctest: +ELLIPSIS Traceback (most recent call last): … ValueError: Saturation must be between 0 and 1.

And: >>> hsl2rgb((0.0, 0.0, 1.5)) # doctest: +ELLIPSIS Traceback (most recent call last): … ValueError: Lightness must be between 0 and 1.

ase_notebook.color.hsl2web(x)
ase_notebook.color.hsl_equivalence(c1, c2)
ase_notebook.color.lighten_webcolor(web_color, fraction)[source]

Move a web color towards white by a fraction, or black if the fraction is negative.

ase_notebook.color.rgb2hex(rgb, force_long=False)[source]

Transform RGB tuple to hex RGB representation.

Parameters

rgb – RGB 3-uple of float between 0 and 1

Returns

3 hex char or 6 hex char string representation

Examples

>>> from ase_notebook.color import rgb2hex
>>> rgb2hex((0.0,1.0,0.0))
'#0f0'

Rounding try to be as natural as possible:

>>> rgb2hex((0.0,0.999999,1.0))
'#0ff'

And if not possible, the 6 hex char representation is used:

>>> rgb2hex((0.23,1.0,1.0))
'#3bffff'
>>> rgb2hex((0.0,0.999999,1.0), force_long=True)
'#00ffff'
ase_notebook.color.rgb2hsl(rgb)[source]

Convert RGB representation towards HSL.

Parameters
  • r – Red amount (float between 0 and 1)

  • g – Green amount (float between 0 and 1)

  • b – Blue amount (float between 0 and 1)

Returns

3-uple for HSL values in float between 0 and 1

This algorithm came from: http://www.easyrgb.com/index.php?X=MATH&H=19#text19

Here are some quick notion of RGB to HSL conversion:

>>> from ase_notebook.color import rgb2hsl

Note that if red amount is equal to green and blue, then you should have a gray value (from black to white).

>>> rgb2hsl((1.0, 1.0, 1.0))  
(..., 0.0, 1.0)
>>> rgb2hsl((0.5, 0.5, 0.5))  
(..., 0.0, 0.5)
>>> rgb2hsl((0.0, 0.0, 0.0))  
(..., 0.0, 0.0)

If only one color is different from the others, it defines the direct Hue:

>>> rgb2hsl((0.5, 0.5, 1.0))  
(0.66..., 1.0, 0.75)
>>> rgb2hsl((0.2, 0.1, 0.1))  
(0.0, 0.33..., 0.15...)

Having only one value set, you can check that:

>>> rgb2hsl((1.0, 0.0, 0.0))
(0.0, 1.0, 0.5)
>>> rgb2hsl((0.0, 1.0, 0.0))  
(0.33..., 1.0, 0.5)
>>> rgb2hsl((0.0, 0.0, 1.0))  
(0.66..., 1.0, 0.5)

Regression check upon very close values in every component of red, green and blue:

>>> rgb2hsl((0.9999999999999999, 1.0, 0.9999999999999994))
(0.0, 0.0, 0.999...)

Of course:

>>> rgb2hsl((0.0, 2.0, 0.5))  
Traceback (most recent call last):
...
ValueError: Green must be between 0 and 1. You provided 2.0.

And: >>> rgb2hsl((0.0, 0.0, 1.5)) # doctest: +ELLIPSIS Traceback (most recent call last): … ValueError: Blue must be between 0 and 1. You provided 1.5.

ase_notebook.color.rgb2web(x)
ase_notebook.color.rgb_equivalence(c1, c2)
ase_notebook.color.web2hex(web, force_long=False)[source]

Convert WEB representation to HEX.

Parameters

rgb – web string representation (human readable if possible)

Returns

3 hex char or 6 hex char string representation

WEB representation uses X11 rgb.txt to define conversion between RGB and english color names.

Examples

>>> from ase_notebook.color import web2hex
>>> web2hex('red')
'#f00'
>>> web2hex('#aaa')
'#aaa'
>>> web2hex('#foo')  
Traceback (most recent call last):
...
AttributeError: '#foo' is not in web format. Need 3 or 6 hex digit.
>>> web2hex('#aaa', force_long=True)
'#aaaaaa'
>>> web2hex('#aaaaaa')
'#aaaaaa'
>>> web2hex('#aaaa')  
Traceback (most recent call last):
...
AttributeError: '#aaaa' is not in web format. Need 3 or 6 hex digit.
>>> web2hex('pinky')  
Traceback (most recent call last):
...
ValueError: 'pinky' is not a recognized color.

And color names are case insensitive:

>>> Color('RED')
<Color red>
ase_notebook.color.web2hsl(x)
ase_notebook.color.web2rgb(x)