Crate palette [−] [src]
A library that makes linear color calculations and conversion easy and accessible for anyone. It provides both precision tools that lets you work in exactly the color space you want to, as well as a general color type that abstracts away some of the technical details.
Linear?
Colors in, for example, images are often "gamma corrected" or stored in sRGB format as a compression method and to prevent banding. This is also a bit of a legacy from the ages of the CRT monitors, where the output from the electron guns was nonlinear. The problem is that these formats doesn't represent the actual intensities, and the compression has to be reverted to make sure that any operations on the colors are accurate. This library uses a completely linear work flow, and comes with the tools for transitioning between linear and non-linear RGB.
For example, this does not work:
// An alias for Rgb<Srgb>, which is what most pictures store. use palette::Srgb; let orangeish = Srgb::new(1.0, 0.6, 0.0); let blueish = Srgb::new(0.0, 0.2, 1.0); // let whateve_it_becomes = orangeish + blueish;
Instead, they have to be made linear before adding:
// An alias for Rgb<Srgb>, which is what most pictures store. use palette::Srgb; let orangeish = Srgb::new(1.0, 0.6, 0.0).into_linear(); let blueish = Srgb::new(0.0, 0.2, 1.0).into_linear(); let whateve_it_becomes = orangeish + blueish; // Encode the result back into sRGB and create a byte array let pixel: [u8;3] = Srgb::linear_to_pixel(whateve_it_becomes);
Transparency
There are many cases where pixel transparency is important, but there are
also many cases where it becomes a dead weight, if it's always stored
together with the color, but not used. Palette has therefore adopted a
structure where the transparency component (alpha) is attachable using the
Alpha
type, instead of having copies of each color
space.
This approach comes with the extra benefit of allowing operations to selectively affect the alpha component:
use palette::{LinSrgb, LinSrgba}; let mut c1 = LinSrgba::new(1.0, 0.5, 0.5, 0.8); let c2 = LinSrgb::new(0.5, 1.0, 1.0); c1.color = c1.color * c2; //Leave the alpha as it is c1.blue += 0.2; //The color components can easily be accessed c1 = c1 * 0.5; //Scale both the color and the alpha
Reexports
pub use gradient::Gradient; |
pub use rgb::GammaSrgb; |
pub use rgb::GammaSrgba; |
pub use rgb::LinSrgb; |
pub use rgb::LinSrgba; |
pub use rgb::Srgb; |
pub use rgb::Srgba; |
Modules
blend |
Color blending and blending equations. |
chromatic_adaptation |
Convert colors from one reference white point to another |
gradient |
Types for interpolation between multiple colors. |
named |
A collection of named color constants. Can be toggled with the |
pixel |
Pixel encodings and pixel format conversion. |
rgb |
RGB types, spaces and standards. |
white_point |
Defines the tristimulus values of the CIE Illuminants. |
Structs
Alpha |
An alpha component wrapper for colors. |
Hsl |
Linear HSL color space. |
Hsv |
Linear HSV color space. |
Hwb |
Linear HWB color space. |
Lab |
The CIE L*a*b* (CIELAB) color space. |
LabHue |
A hue type for the CIE L*a*b* family of color spaces. |
Lch |
CIE L*C*h°, a polar version of CIE L*a*b*. |
Luma |
Linear luminance. |
RgbHue |
A hue type for the RGB family of color spaces. |
Xyz |
The CIE 1931 XYZ color space. |
Yxy |
The CIE 1931 Yxy (xyY) color space. |
Enums
Color |
A generic color type. |
Traits
Blend |
A trait for colors that can be blended together. |
ComponentWise |
Perform a unary or binary operation on each component of a color. |
FromColor |
FromColor provides conversion between the colors. |
GetHue |
A trait for colors where a hue may be calculated. |
Hue |
A trait for colors where the hue can be manipulated without conversion. |
IntoColor |
IntoColor provides conversion between the colors. |
Limited |
A trait for clamping and checking if colors are within their ranges. |
Mix |
A trait for linear color interpolation. |
Saturate |
A trait for colors where the saturation (or chroma) can be manipulated without conversion. |
Shade |
The |
Type Definitions
Colora |
Generic color with an alpha component. See the |
Hsla |
Linear HSL with an alpha component. See the |
Hsva |
Linear HSV with an alpha component. See the |
Hwba |
Linear HWB with an alpha component. See the |
Laba |
CIE L*a*b* (CIELAB) with an alpha component. See the |
Lcha |
CIE L*C*h° with an alpha component. See the |
Lumaa |
Linear luminance with an alpha component. See the |
Mat3 |
A 9 element array representing a 3x3 matrix |
Xyza |
CIE 1931 XYZ with an alpha component. See the |
Yxya |
CIE 1931 Yxy (xyY) with an alpha component. See the |