7. colors — Playing with colors.

This module defines some colors and color conversion functions. It also defines a default palette of colors.

The following table shows the built-in colors, with their name, RGB values in 0..1 range and luminance.

>>> for k,v in PF_colors.items():
...     print(f"{k:15s} = {v} -> {luminance(v):.3f}")
                red = (1.0, 0.0, 0.0) -> 0.213
              green = (0.0, 1.0, 0.0) -> 0.715
               blue = (0.0, 0.0, 1.0) -> 0.072
               cyan = (0.0, 1.0, 1.0) -> 0.787
            magenta = (1.0, 0.0, 1.0) -> 0.285
             yellow = (1.0, 1.0, 0.0) -> 0.928
            darkred = (0.5, 0.0, 0.0) -> 0.046
          darkgreen = (0.0, 0.5, 0.0) -> 0.153
           darkblue = (0.0, 0.0, 0.5) -> 0.015
           darkcyan = (0.0, 0.5, 0.5) -> 0.169
        darkmagenta = (0.5, 0.0, 0.5) -> 0.061
         darkyellow = (0.5, 0.5, 0.0) -> 0.199
      pyformex_pink = (1.0, 0.2, 0.4) -> 0.246
              black = (0.0, 0.0, 0.0) -> 0.000
           darkgrey = (0.4, 0.4, 0.4) -> 0.133
         mediumgrey = (0.6, 0.6, 0.6) -> 0.319
          lightgrey = (0.8, 0.8, 0.8) -> 0.604
     lightlightgrey = (0.9, 0.9, 0.9) -> 0.787
              white = (1.0, 1.0, 1.0) -> 1.000

7.1. Functions defined in module colors

colors.loadX11Colors(filename)[source]

Load the X11 colors

colors.GLcolor(color)[source]

Convert a color to an OpenGL RGB color.

Parameters:

color (color_like) –

Data specifying an RGB color. This can be any of the following:

  • a single int: returns the palette color with that index (modulo the palette length

  • a float: returns the grey color with that value

  • a string with the name of one of the built-in PF_colors

  • a string specifying the X11 name of the color

  • a hex string ‘#RGB’ with 1 to 4 hexadecimal digits per color

  • a tuple or list of 3 integer values in the range 0..255

  • a tuple or list of 3 float values in the range 0.0..1.0

  • a QColor or any data that can be used to create a QColor

Returns:

tuple | ndarray – A tuple of three RGB float values, normally in the range 0.0..1.0.

Warning

There is currently no check that the result is in the range 0.0..1.0, because OpenGL allows to make clever use of values exceeding these limits. The values will be clipped at render time.

Raises:

ValueError – If the input is not one of the accepted data.:

Examples

>>> GLcolor(2)
(0.0, 1.0, 0.0)
>>> GLcolor('red')
(1.0, 0.0, 0.0)
>>> GLcolor('indianred')
(0.8039..., 0.3607..., 0.3607...)
>>> GLcolor('grey90')
(0.8980...,  0.8980...,  0.8980...)
>>> print(GLcolor('#ff0000'))
(1.0, 0.0, 0.0)
>>> GLcolor("zorro")
(0.0, 0.0, 0.0)
>>> GLcolor(red)
(1.0, 0.0, 0.0)
>>> GLcolor([200,200,255])
(0.7843..., 0.7843..., 1.0)
>>> GLcolor(np.array([200,200,255], dtype=np.uint8))
(0.7843..., 0.7843..., 1.0)
>>> GLcolor([1.,1.,1.])
(1.0, 1.0, 1.0)
>>> GLcolor(0.6)
(0.6, 0.6, 0.6)
>>> at.mapArray(GLcolor, ['red'])
array([[1.,  0.,  0.]])
>>> at.mapArray(GLcolor, ['red', 'green'])
array([[1.,  0.,  0.],
       [0.,  1.,  0.]])
>>> at.mapArray(GLcolor, [['red', 'green'], ['cyan','magenta']])
array([[[1.,  0.,  0.],
        [0.,  1.,  0.]],
       [[0.,  1.,  1.],
        [1.,  0.,  1.]]])
colors.GLcolor4(color, alpha=0.5)[source]

Like GLcolor with alpha.

Returns tuple of shape (4,) If color does not contain alpha, adds 0.5

colors.RGBcolor(color)[source]

Return an RGB (0-255) tuple for a color

color can be anything that is accepted by GLcolor.

Returns the corresponding RGB colors as a numpy array of type uint8 and shape (..,3).

Example

>>> RGBcolor(red)
array([255,   0,   0], dtype=uint8)
colors.RGBAcolor(color, alpha=0.5)[source]

Return an RGBA (0-255) tuple for a color and alpha value.

color can be anything that is accepted by GLcolor.

Returns the corresponding RGBA colors as a numpy array of type uint8 and shape (…, 4).

Examples

>>> RGBAcolor(yellow)
array([255, 255,   0, 128], dtype=uint8)
>>> RGBAcolor('yellow')
array([255, 255,   0, 128], dtype=uint8)
>>> RGBAcolor((1., 1., 0.))
array([255, 255,   0, 128], dtype=uint8)
>>> RGBAcolor((1., 1., 0., 0.5))
array([255, 255,   0, 128], dtype=uint8)
>>> RGBAcolor((255, 255, 0))
array([255, 255,   0, 128], dtype=uint8)
>>> RGBAcolor((255, 255, 0, 128))
array([255, 255,   0, 128], dtype=uint8)
>>> RGBAcolor(yellow, 1.0)
array([255, 255,   0, 255], dtype=uint8)
>>> RGBAcolor((255, 255, 255, 255))
array([255, 255, 255, 255], dtype=uint8)
colors.WEBcolor(color)[source]

Return an RGB hex string for a color

color can be anything that is accepted by GLcolor. Returns the corresponding WEB color, which is a hexadecimal string representation of the RGB components.

Example

>>> WEBcolor(red)
'#ff0000'
colors.colorName(color)[source]

Return a string designation for the color.

color can be anything that is accepted by GLcolor. In the current implementation, the returned color name is the WEBcolor (hexadecimal string).

Example

>>> colorName('red')
'#ff0000'
>>> colorName('#ffddff')
'#ffddff'
>>> colorName([1.,0.,0.5])
'#ff0080'
colors.luminance(color, gamma=True)[source]

Compute the luminance of a color.

Returns a floating point value in the range 0..1 representing the luminance of the color. The higher the value, the brighter the color appears to the human eye.

This can for example be used to derive a good contrasting foreground color to display text on a colored background. Values lower than 0.5 contrast well with white, larger value contrast better with black.

Examples

>>> print([f"{luminance(c):0.2f}" for c in ['black','red','green','blue']])
['0.00', '0.21', '0.72', '0.07']
>>> print(luminance(np.array([black,red,green,blue])))
[0.     0.2126 0.7152 0.0722]
colors.closestColorName(color)[source]

Return the closest color name.

colors.RGBA(rgb, alpha=1.0)[source]

Adds an alpha channel to an RGB color

colors.GREY(val, alpha=1.0)[source]

Returns a grey OpenGL color of given intensity (0..1)