pyface.util.color_helpers module

Routines supporting color computations

Most of what is needed is provided by Python’s builtin colorsys module, but we need a few additional routines for things that are not covered by that code.

pyface.util.color_helpers.channels_to_ints(channels, maximum=255)[source]

Convert an iterable of floating point channel values to integers.

Values are rounded to the nearest integer, rather than truncated.

Parameters
  • channels (iterable of float) – An iterable of channel values, each value between 0.0 and 1.0, inclusive.

  • maximum (int) – The maximum value of the integer range. Common values are 15, 65535 or 255, which is the default.

Returns

values – A tuple of values as integers between 0 and max, inclusive.

Return type

tuple of int

pyface.util.color_helpers.ints_to_channels(values, maximum=255)[source]

Convert an iterable of integers to floating point channel values.

Parameters
  • values (tuple of int) – An iterable of values as integers between 0 and max, inclusive.

  • maximum (int) – The maximum value of the integer range. Common values are 15, 65535 or 255, which is the default.

Returns

channels – A tuple of channel values, each value between 0.0 and 1.0, inclusive.

Return type

iterable of float

pyface.util.color_helpers.relative_luminance(rgb)[source]

The relative luminance of the color.

This value is the critical value when comparing colors for contrast when displayed next to each other, in particular for readability of text.

Parameters

rgb (tuple of red, green, blue values) – A tuple of values representing red, green and blue components of the color, as values from 0.0 to 1.0.

Returns

luminance – The relative luminance of the color.

Return type

float

References

Web Contrast Accessibility Guidelines https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef

pyface.util.color_helpers.is_dark(rgb)[source]

Is the color dark to human perception?

A color is dark if white contasts better with it according to the WC3 definition of contrast ratio. This is allows GUI code to choose either black or white as a contrasting color for things like text on a colored background.

Parameters

rgb (tuple of red, green, blue values) – A tuple of values representing red, green and blue components of the color, as values from 0.0 to 1.0.

References

Understanding Web Contrast Accessibility Guidelines https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html#contrast-ratiodef