6. opengl.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 colors of the default palette, with their name, RGB values in 0..1 range and luminance.

>>> for k,v in palette.items():
...     print("%12s = %s -> %0.3f" % (k,v,luminance(v)))
    darkgrey = (0.4, 0.4, 0.4) -> 0.133
         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
       white = (1.0, 1.0, 1.0) -> 1.000
       black = (0.0, 0.0, 0.0) -> 0.000
     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
   lightgrey = (0.8, 0.8, 0.8) -> 0.604

6.1. Functions defined in module opengl.colors

opengl.colors.GLcolor(color)[source]

Convert a color to an OpenGL RGB color.

The output is a tuple of three RGB float values ranging from 0.0 to 1.0. The input can be any of the following:

  • a QColor
  • 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

Any other input may give unpredictable results.

Examples: >>> GLcolor(‘red’) (1.0, 0.0, 0.0) >>> GLcolor(‘indianred’) # doctest: +ELLIPSIS (0.8039…, 0.3607…, 0.3607…) >>> GLcolor(‘grey90’) # doctest: +ELLIPSIS (0.8980…, 0.8980…, 0.8980…) >>> print(GLcolor(‘#ff0000’)) (1.0, 0.0, 0.0) >>> GLcolor(red) (1.0, 0.0, 0.0) >>> GLcolor([200,200,255]) (0.7843137254901961, 0.7843137254901961, 1.0) >>> GLcolor([1.,1.,1.]) (1.0, 1.0, 1.0) >>> GLcolor(0.6) (0.6, 0.6, 0.6)

opengl.colors.GLcolorA(color)[source]

Convert a color to an OpenGL RGB color.

The output is a tuple of three RGB float values ranging from 0.0 to 1.0. The input can be any of the following:

  • a QColor
  • a string specifying the Xwindow 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

Any other input may give unpredictable results.

Example

>>> GLcolorA('indianred')
array([ 0.8 ,  0.36,  0.36])
>>> print(GLcolorA('#ff0000'))
[ 1.  0.  0.]
>>> GLcolorA(red)
array([ 1.,  0.,  0.])
>>> GLcolorA([200,200,255])
array([ 0.78,  0.78,  1.  ])
>>> GLcolorA([1.,1.,1.])
array([ 1.,  1.,  1.])
>>> GLcolorA(0.6)
array([ 0.6,  0.6,  0.6])
>>> print(GLcolorA(['black','red','green','blue']))
[[ 0.  0.  0.]
 [ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]
opengl.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)
opengl.colors.RGBAcolor(color, alpha)[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).

opengl.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'
opengl.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'
opengl.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 be 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.

Example

>>> print([ "%0.2f" % luminance(c) for c in ['black','red','green','blue']])
['0.00', '0.21', '0.72', '0.07']
>>> print(luminance(['black','red','green','blue']))
[ 0.    0.21  0.72  0.07]
opengl.colors.closestColorName(color)[source]

Return the closest color name.

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

Adds an alpha channel to an RGB color

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

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