Color

class compas.colors.Color(red, green, blue, alpha=1.0, **kwargs)[source]

Bases: Data

Class for working with colors.

Parameters
  • red (float) – The red component in the range of 0-1.

  • green (float) – The green component in the range of 0-1.

  • blue (float) – The blue component in the range of 0-1.

  • alpha (float, optional) – Transparency setting. If alpha = 0.0, the color is fully transparent. If alpha = 1.0, the color is fully opaque.

Other Parameters

**kwargs (dict, optional) – See Data for more information.

Attributes
  • r (float) – Red component of the color in RGB1 color space.

  • g (float) – Green component of the color in RGB1 color space.

  • b (float) – Blue component of the color in RGB1 color space.

  • rgb (tuple[float, float, float]) – RGB1 color tuple, with components in the range 0-1.

  • rgb255 (tuple[int, int, int]) – RGB255 color tuple, with components in the range 0-255.

  • hex (str) – Hexadecimal color string.

  • hls (tuple[float, float, float]) – Hue, Lightness, Saturation.

  • hsv (tuple[float, float, float]) – Hue, Saturation, Value / Brightness.

  • lightness (float) – How much white the color appears to contain. This is the “Lightness” in HLS. Making a color “lighter” is like adding more white.

  • brightness (float) – 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.

  • is_light (bool) – If True, the color is considered light.

Examples

>>> Color(1, 0, 0)
Color(1.0, 0.0, 0.0, 1.0)
>>> Color.red()
Color(1.0, 0.0, 0.0, 1.0)
>>> Color(1, 0, 0) == Color.red()
True
>>> Color.magenta()
Color(1.0, 0.0, 1.0, 1.0)
>>> Color.lime()
Color(0.5, 1.0, 0.0, 1.0)
>>> Color.navy()
Color(0.0, 0.0, 0.5, 1.0)
>>> Color.olive()
Color(0.5, 0.5, 0.0, 1.0)
>>> Color.lime().is_light
True
>>> Color.navy().is_light
False

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_data

Construct an object of this type from the provided data.

from_hex

Construct a color from a hexadecimal color value.

from_hls

Construct a color from Hue, Luminance, 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_rgb255

Construct a color from RGB255 components.

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.

is_hex

Verify that the color is in hexadecimal format.

is_rgb1

Verify that the color is in the RGB 1 color space.

is_rgb255

Verify that the color is in the RGB 255 color space.

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 from serialized data contained in a JSON file.

from_jsonstring

Construct an object from serialized data contained in a JSON string.

sha256

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

to_data

Convert an object to its native data representation.

to_json

Serialize the data representation of an object to a JSON file.

to_jsonstring

Serialize the data representation of an object to a JSON string.

validate_data

Validate the object's data against its data schema.

validate_json

Validate the object's data against its json schema.