108. opengl.sanitize — Sanitize data before rendering.

The pyFormex drawing functions were designed to require a minimal knowledge of OpenGL and rendering principles in general. They allow the user to specify rendering attributes in a simple, user-friendly, sometimes even sloppy way. This module contains some functions to sanitize the user input into the more strict attributes required by the OpenGl rendering engine.

These functions are generally not intended for direct used by the user, but for use in the opengl rendering functions.

108.1. Functions defined in module opengl.sanitize

opengl.sanitize.saneFloat(value)[source]

Return a float value or None.

If value can be converted to float, the float is returned, else None.

opengl.sanitize.saneLineWidth(value)

Return a float value or None.

If value can be converted to float, the float is returned, else None.

opengl.sanitize.saneLineStipple(stipple)[source]

Return a sane line stipple tuple.

A line stipple tuple is a tuple (factor,pattern) where pattern defines which pixels are on or off (maximum 16 bits), factor is a multiplier for each bit.

opengl.sanitize.saneColor(color=None)[source]

Return a sane color array derived from the input color.

A sane color is one that will be usable by the draw method. The input value of color can be either of the following:

  • None: indicates that the default color will be used,

  • a single color value in a format accepted by colors.GLcolor,

  • a tuple or list of such colors,

  • a (3,) shaped array of RGB values, ranging from 0.0 to 1.0,

  • an (n,3) shaped array of RGB values,

  • a (4,) shaped array of RGBA values, ranging from 0.0 to 1.0,

  • an (n,4) shaped array of RGBA values,

  • an (n,) shaped array of integer color indices. TODO: disallow!

The return value is one of the following: - None, indicating no color (current color will be used), - a float array with shape (3/4,), indicating a single color, - a float array with shape (n,3/4), holding a collection of colors, - an integer array with shape (n,), holding color index values.

!! Note that a single color can not be specified as integer RGB values. A single list of integers will be interpreted as a color index ! Turning the single color into a list with one item will work though. [[0, 0, 255]] will be the same as [‘blue’], while [0, 0, 255] would be a color index with 3 values.

Examples

>>> print(saneColor('red'))
[1.  0.  0.]
>>> print(saneColor('grey90'))
[0.898 0.898 0.898]
>>> saneColor(['red', 'green', 'blue'])
array([[1.,  0.,  0.],
       [0.,  1.,  0.],
       [0.,  0.,  1.]])
>>> saneColor([['red', 'green', 'blue'], ['cyan', 'magenta', 'yellow']])
array([[[1.,  0.,  0.],
        [0.,  1.,  0.],
        [0.,  0.,  1.]],
       [[0.,  1.,  1.],
        [1.,  0.,  1.],
        [1.,  1.,  0.]]])
opengl.sanitize.saneColorSet(color=None, colormap=None, *, shape=(1,))[source]

Return a sane set of colors.

A sane set of colors is one that guarantees correct use by the draw functions. This means either

  • no color (None)

  • a single color

  • at least as many colors as the shape argument specifies

  • a color index with at least as many colors as the shape argument specifies, and a colormap with enough colors to satisfy the index.

Parameters:
  • color (colors_like) – One or more valid color designations. This can be anything that is valid input for saneColor. These can be the real colors (if float) or indices into a colormap (if int).

  • colormap (list) – A list of colors to be used as the color map if colors are indices instead of the colors themselves.

  • shape (tuple) – A tuple specifying the shape of the color array to return. The tuple should have 1 or 2 dimensions: (nelems,) or (nelems, nplex). The tuple should not contain the number of color components.

Returns:

  • color (array) – Either a float32 array with shape shape + (3,) holding 3 RGB color components, or an int32 array with shape shape, holding indices into the colormap.

  • colormap (None | array) – None if color returns real colors, or a float32 array of shape (ncolors, 3), where ncolors is guaranteed to be larger than the maximum index returned in color.

Examples

No color always remains no color

>>> saneColorSet(None, shape=(2,))
(None, None)

Single color remains single color

>>> saneColorSet('red', shape=(2,))
(array([1., 0., 0.]), None)
>>> saneColorSet(colors.red, shape=(3,2))
(array([1., 0., 0.]), None)
>>> saneColorSet(1, colors.palette, shape=(2,))
(array([1., 0., 0.]), None)
>>> saneColorSet(1, colors.palette, shape=(3,2))
(array([1., 0., 0.]), None)

A set (tuple, list or array) of colors is expanded to match shape

>>> saneColorSet(('red',), shape=(2,))
(array([[1., 0., 0.],
       [1., 0., 0.]]), None)
>>> saneColorSet((colors.red, colors.green), shape=(3,2))
(array([[[1., 0., 0.],
        [1., 0., 0.]],

       [[0., 1., 0.],
        [0., 1., 0.]],

       [[1., 0., 0.],
        [1., 0., 0.]]]), None)
>>> saneColorSet((1,), colors.palette, shape=(2,))
(array([1., 0., 0.]), None)
>>> saneColorSet((1, 2), colors.palette, shape=(3,2))
(array([[1, 1],
       [2, 2],
       [1, 1]]), array([[0.4, 0.4, 0.4],
       [1. , 0. , 0. ],
       [0. , 1. , 0. ]]))