psychopy.colors - For working with colors

Classes and functions for working with colors.

Overview

Color([color, space, contrast, conematrix])

A class to store color details, knows what colour space it's in and can supply colours in any space.

isValidColor(color[, space])

Depreciated as of 2021.0

hex2rgb255(hexColor)

Depreciated as of 2021.0

Details

class psychopy.colors.Color(color=None, space=None, contrast=None, conematrix=None)[source]

A class to store color details, knows what colour space it’s in and can supply colours in any space.

Parameters:
  • color (ArrayLike or None) – Color values (coordinates). Value must be in a format applicable to the specified space.

  • space (str or None) – Colorspace to interpret the value of color as being within.

  • contrast (int or float) – Factor to modulate the contrast of the color.

  • conematrix (ArrayLike or None) – Cone matrix for colorspaces which require it. Must be a 3x3 array.

property alpha

How opaque (1) or transparent (0) this color is. Synonymous with opacity.

property contrast
copy()[source]

Return a duplicate of this colour

property dkl

Color value expressed as a DKL triplet.

property dklCart

Color value expressed as a cartesian DKL triplet.

property dkla

Color value expressed as a DKL triplet, with alpha value (0 to 1).

property dklaCart

Color value expressed as a cartesian DKL triplet, with alpha value (0 to 1).

getReadable(contrast=0.21428571428571427)[source]

Get a color which will stand out and be easily readable against this one. Useful for choosing text colors based on background color.

Parameters:

contrast (float) – Desired perceived contrast between the two colors, between 0 (the same color) and 1 (as opposite as possible). Default is the w3c recommended minimum of 4.5/21 (dividing by 21 to adjust for sRGB units).

Returns:

A contrasting color to this color.

Return type:

colors.Color

property hex

Color value expressed as a hex string. Can be a ‘#’ followed by 6 values from 0 to F (e.g. #F2545B).

property hsv

Color value expressed as an HSV triplet.

property hsva

Color value expressed as an HSV triplet, with alpha value (0 to 1).

property lms

Color value expressed as an LMS triplet.

property lmsa

Color value expressed as an LMS triplet, with alpha value (0 to 1).

property named

The name of this color, if it has one (str).

property opacity

How opaque (1) or transparent (0) this color is (float). Synonymous with alpha.

render(space='rgb')[source]

Apply contrast to the base color value and return the adjusted color value.

property rgb

Color value expressed as an RGB triplet from -1 to 1.

property rgb1

Color value expressed as an RGB triplet from 0 to 1.

property rgb255

Color value expressed as an RGB triplet from 0 to 255.

property rgba

Color value expressed as an RGB triplet from -1 to 1, with alpha values (0 to 1).

property rgba1

Color value expressed as an RGB triplet from 0 to 1, with alpha value (0 to 1).

property rgba255

Color value expressed as an RGB triplet from 0 to 255, with alpha value (0 to 1).

set(color=None, space=None)[source]

Set the colour of this object - essentially the same as what happens on creation, but without having to initialise a new object.

property srgb

Color value expressed as an sRGB triplet

validate(color, space=None)[source]

Check that a color value is valid in the given space, or all spaces if space==None.

psychopy.colors.isValidColor(color, space='rgb')[source]

Depreciated as of 2021.0

psychopy.colors.hex2rgb255(hexColor)[source]

Depreciated as of 2021.0

Converts a hex color string (e.g. “#05ff66”) into an rgb triplet ranging from 0:255


Back to top