Module scarlet::color

source ·
Expand description

This file defines the Color trait, the foundational defining trait of the entire library. Despite the dizzying amount of things Color can do in Scarlet, especially with its extending traits, the definition is quite simple: anything that can be converted to and from the CIE 1931 XYZ space. This color space is common to use as a master space, and Scarlet is no different. What makes XYZ unique is that it can be computed directly from the spectral data of a color. Although Scarlet does not implement this due to its scope, this property makes it possible to derive XYZ colors from real-world data, something that no other color space can do the same way.

The thing that makes XYZColor, the base implementation of the CIE 1931 XYZ space, special is that it is the only color object in Scarlet that keeps track of its own illuminant data. Every other color space assumes a viewing environment, but because XYZ color maps directly to neural perception it keeps track of what environment the color is being viewed in. This allows Scarlet to translate between color spaces that have different assumptions seamlessly. (If you notice that Scarlet’s values for conversions differ from other sources, this may be why: some sources don’t do this properly or implement it differently. Scarlet generally follows best practices and industry standards, but file an issue if you feel this is not true.) The essential workflow of Color, and therefore Scarlet, is generally like this: convert between different color spaces using the generic convert<T: Color>() method, which allows any Color to be interconverted to any other representation. Leverage the specific attributes of each color space if need be (for example, using the hue or luminance attributes), and then convert back to a suitable display space. The many other methods of Color make some of the more common such patterns simple to do.

Structs

  • A color with red, green, and blue primaries of specified intensity, specifically in the sRGB gamut: most computer screens use this to display colors. The attributes r, g, and b are floating-point numbers from 0 to 1 for visible colors, allowing the avoidance of rounding errors or clamping errors when converting to and from RGB. Many conveniences are afforded so that working with RGB as if it were instead three integers from 0-255 is painless. Note that the integers generated from the underlying floating-point numbers round away from 0.
  • A point in the CIE 1931 XYZ color space. Although any point in XYZ coordinate space is technically valid, in this library XYZ colors are treated as normalized so that Y=1 is the white point of whatever illuminant is being worked with.

Enums

  • An error type that results from an invalid attempt to convert a string into an RGB color.

Traits

  • A trait that represents any color representation that can be converted to and from the CIE 1931 XYZ color space. See module-level documentation for more information and examples.