psychopy.tools.colorspacetools.rescaleColor

psychopy.tools.colorspacetools.rescaleColor(rgb, convertTo='signed', clip=False)[source]

Rescale RGB colors.

This function can be used to convert RGB value triplets from the PsychoPy signed color format to the normalized OpenGL format.

PsychoPy represents colors using values between -1 and 1. However, colors are commonly represented using values between 0 and 1 when working with OpenGL and various other contexts. This function simply rescales values to switch between these formats.

Parameters
  • rgb (array_like) – 1-, 2-, 3-D vector of RGB coordinates to convert. The last dimension should be length-3 in all cases, specifying a single coordinate.

  • convertTo (str) – If ‘signed’, this function will assume rgb is in OpenGL format [0:1] and rescale them to PsychoPy’s format [-1:1]. If ‘unsigned’, input values are treated as OpenGL format and will be rescaled to use PsychoPy’s. Default is ‘signed’.

  • clip (bool) – Clip values to the range that can be represented on a display. This is an optional step. Default is False.

Returns

Rescaled values with the same shape as rgb.

Return type

ndarray

Notes

The convertTo argument also accepts strings ‘opengl’ and ‘psychopy’ as substitutes for ‘signed’ and ‘unsigned’, respectively. This might be more explicit in some contexts.

Examples

Convert a signed RGB value to unsigned format:

rgb_signed = [-1, 0, 1]
rgb_unsigned = rescaleColor(rgb_signed, convertTo='unsigned')

Back to top