Color

class compas.colors.Color[source]

Bases: Data

Class for working with colors.

Parameters:
redfloat

The red component in the range [0.0, 1.0].

greenfloat

The green component in the range of [0.0, 1.0].

bluefloat

The blue component in the range of [0.0, 1.0].

alphafloat, optional

Transparency setting. If alpha = 0.0, the color is fully transparent. If alpha = 1.0, the color is fully opaque.

namestr, optional

The name of the color.

Attributes:
rfloat

Red component of the color in RGB1 color space.

gfloat

Green component of the color in RGB1 color space.

bfloat

Blue component of the color in RGB1 color space.

afloat

Transparency in RGB1 color space.

rgbtuple[float, float, float]

RGB1 color tuple, with components in the range [0.0, 1.0].

rgbatuple[float, float, float, float]

RGBA1 color tuple (including alpha), with components in the range [0.0, 1.0].

rgb255tuple[int, int, int]

RGB255 color tuple, with components in the range [0, 255].

rgba255tuple[int, int, int, int]

RGBA255 color tuple (including alpha), with components in the range [0, 255].

hexstr

Hexadecimal color string.

hlstuple[float, float, float]

Hue, Lightness, Saturation.

hsvtuple[float, float, float]

Hue, Saturation, Value / Brightness.

lightnessfloat

How much white the color appears to contain. This is the “Lightness” in HLS. Making a color “lighter” is like adding more white.

brightnessfloat

How well-lit the color appears to be. This is the “Value” in HSV. Making a color “brighter” is like shining a stronger light on it, or illuminating it better.

yuvtuple[float, float, float]

Luma and chroma components, with chroma defined by the blue and red projections.

lumafloat

The brightness of a yuv signal.

chromatuple[float, float]

The color of a yuv signal. “How different from a grey of the same lightness the color appears to be.”

luminancefloat

The amount of light that passes through, is emitted from, or is reflected from a particular area. Here, it expresses the preceived brightness of the color. Note that this is not the same as the “Lightness” of HLS or the “Value/Brightness” of HSV.

saturationfloat

The perceived freedom of whiteness.

is_lightbool

If True, the color is considered light.

contrastcompas.colors.Color

The contrasting color to the current color.

Examples

By default, this class will create a color with the RGB components in the range [0.0, 1.0].

>>> Color(1, 0, 0)
Color(red=1, green=0, blue=0, alpha=1.0)

Attempting to create a color with components outside of the range [0.0, 1.0] will raise a ValueError.

>>> Color(255, 0, 0)
Traceback (most recent call last):
...
ValueError: Components of an RGBA color should be in the range 0-1.

To create a color with components in the range [0, 255], use the from_rgb255() constructor.

>>> Color.from_rgb255(255, 0, 0)
Color(red=1.0, green=0.0, blue=0.0, alpha=1.0)

Similarly, other constructors are available to create colors from other color spaces.

>>> color = Color.from_hls(0.0, 0.5, 1.0)
>>> color = Color.from_hsv(0.0, 1.0, 1.0)
>>> color = Color.from_yiq(0.0, 0.0, 0.0)
>>> color = Color.from_yuv(0.0, 0.0, 0.0)

Or, to construct specific colors, for example, …

>>> color = Color.red()
>>> color = Color.magenta()
>>> color = Color.lime()
>>> color = Color.navy()
>>> color = Color.olive()

Colors can be modified through inversion, saturation/desaturation, and lightening/darkening.

>>> color = Color.red()
>>> color.desaturated(25)
Color(red=0.875, green=0.125, blue=0.125, alpha=1.0)
>>> color.desaturated(50)
Color(red=0.75, green=0.25, blue=0.25, alpha=1.0)
>>> color.desaturated(75)
Color(red=0.625, green=0.375, blue=0.375, alpha=1.0)
>>> color.desaturated(100)
Color(red=0.5, green=0.5, blue=0.5, alpha=1.0)

Methods

azure

Construct the color azure.

black

Construct the color black.

blue

Construct the color blue.

brown

Construct the color brown.

coerce

Coerce a color input into a color.

cyan

Construct the color cyan.

darken

Darken the color.

darkened

Return a darkened copy of the color.

desaturate

Desaturate the color by a given percentage.

desaturated

Return a desaturated copy of the color.

from_hex

Construct a color from a hexadecimal color value.

from_hls

Construct a color from Hue, Lightness, and Saturation.

from_hsv

Construct a color from Hue, Saturation, and Value.

from_i

Construct a color from a single number in the range 0-1.

from_name

Construct a color from a name in the extended color table of HTML/CSS/SVG.

from_number

Construct a color from a single number in the range 0-1.

from_rgb255

Construct a color from RGB255 components.

from_unknown

Construct a color from an unknown input.

from_yiq

Construct a color from components in the YIQ color space.

from_yuv

Construct a color from components in the YUV color space.

green

Construct the color green.

grey

Construct the color grey.

invert

Invert the current color wrt to the RGB color circle.

inverted

Return an inverted copy of the color.

lighten

Lighten the color.

lightened

Return a lightened copy of the color.

lime

Construct the color lime (or chartreuse green).

magenta

Construct the color magenta.

maroon

Construct the color maroon.

mint

Construct the color mint (or spring green).

navy

Construct the color navy.

olive

Construct the color olive.

orange

Construct the color orange.

pink

Construct the color pink.

purple

Construct the color purple.

red

Construct the color red.

saturate

Saturate the color by a given percentage.

saturated

Return a saturated copy of the color.

silver

Construct the color silver.

teal

Construct the color teal.

violet

Construct the color violet.

white

Construct the color white.

yellow

Construct the color yellow.

Inherited Methods

ToString

Converts the instance to a string.

copy

Make an independent copy of the data object.

from_json

Construct an object of this type from a JSON file.

from_jsonstring

Construct an object of this type from a JSON string.

sha256

Compute a hash of the data for comparison during version control using the sha256 algorithm.

to_json

Convert an object to its native data representation and save it to a JSON file.

to_jsonstring

Convert an object to its native data representation and save it to a JSON string.

validate_data

Validate the data against the object's data schema.