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. Ifalpha = 1.0
, the color is fully opaque.- namestr, optional
The name of the color.
See also
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(1.0, 0.0, 0.0, alpha=1.0)
Attempting to create a color with components outside of the range
[0.0, 1.0]
will raise aValueError
.>>> 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 thefrom_rgb255()
constructor.>>> Color.from_rgb255(255, 0, 0) Color(1.0, 0.0, 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(0.875, 0.125, 0.125, alpha=1.0) >>> color.desaturated(50) Color(0.75, 0.25, 0.25, alpha=1.0) >>> color.desaturated(75) Color(0.625, 0.375, 0.375, alpha=1.0) >>> color.desaturated(100) Color(0.5, 0.5, 0.5, alpha=1.0)
- 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.
Methods
Construct the color azure.
Construct the color black.
Construct the color blue.
Construct the color brown.
Coerce a color input into a color.
Construct the color cyan.
Darken the color.
Return a darkened copy of the color.
Desaturate the color by a given percentage.
Return a desaturated copy of the color.
Construct a color from a hexadecimal color value.
Construct a color from Hue, Lightness, and Saturation.
Construct a color from Hue, Saturation, and Value.
Construct a color from a single number in the range 0-1.
Construct a color from a name in the extended color table of HTML/CSS/SVG.
Construct a color from a single number in the range 0-1.
Construct a color from RGB255 components.
Construct a color from an unknown input.
Construct a color from components in the YIQ color space.
Construct a color from components in the YUV color space.
Construct the color green.
Construct the color grey.
Invert the current color wrt to the RGB color circle.
Return an inverted copy of the color.
Lighten the color.
Return a lightened copy of the color.
Construct the color lime (or chartreuse green).
Construct the color magenta.
Construct the color maroon.
Construct the color mint (or spring green).
Construct the color navy.
Construct the color olive.
Construct the color orange.
Construct the color pink.
Construct the color purple.
Construct the color red.
Saturate the color by a given percentage.
Return a saturated copy of the color.
Construct the color silver.
Construct the color teal.
Construct the color violet.
Construct the color white.
Construct the color yellow.
Inherited Methods
Converts the instance to a string.
Make an independent copy of the data object.
Construct an object of this type from a JSON file.
Construct an object of this type from a JSON string.
Compute a hash of the data for comparison during version control using the sha256 algorithm.
Convert an object to its native data representation and save it to a JSON file.
Convert an object to its native data representation and save it to a JSON string.
Validate the data against the object's data schema.