A color management and conversion library that focuses on maintaining correctness, flexibility and ease of use. It makes use of the type system to prevent mistakes, support a wide range of color spaces (including user defined variants) and offer different ways of integrating with other libraries.
- Type system representations of color spaces, including RGB, HSL, HSV, HWB, L*a*b*, L*C*h°, XYZ and xyY.
- Copy free conversion to and from color buffers allows simple integration with other crates and systems.
- Color operations implemented as traits, such as arithmetic, lighten/darken, hue shifting, mixing/interpolating, and SVG blend functions.
- Color spaces can be customized, using type parameters, to support different levels of precision, linearity, white points, RGB standards, etc.
Minimum Supported Rust Version (MSRV)
This version of Palette has been automatically tested with Rust version
1.60.0 and the
nightly channels. Future versions of the library may advance the minimum supported version to make use of new language features, but this will normally be considered a breaking change. Exceptions may be made for security patches and similar changes.
Add the following lines to your
 = "0.7.2"
or these lines if you want to opt out of
 = "0.7.2" = false = ["libm"] # Uses libm instead of std for floating point math
These features are enabled by default:
"named"- Enables color constants, located in the
named::from_str, which maps name strings to colors.
"std"- Enables use of the standard library.
"approx"- Enables approximate comparison using
These features are disabled by default:
"serializing"- Enables color serializing and deserializing using
"random"- Enables generating random colors using
"libm"- Uses the
libmfloating point math library (for when the
stdfeature is disabled).
"bytemuck"- Enables casting between plain data types using
"wide"- Enables support for using SIMD types from
"find-crate"- Enables derives to find the
palettecrate when it's renamed in
Using palette in an embedded environment
#![no_std] environments by disabling the
"std" feature. It uses
libm to provide the floating-point operations that are typically in
std. However, serializing with
serde is not available without the standard library.
These are examples of some of the features listed in the feature summary.
It's possible to convert from one color space to another with the
IntoColor traits. They are similar to
Into, but tailored for colors:
use ; let my_rgb = new; let mut my_lch = from_color; my_lch.hue += 180.0; let mut my_hsl: Hsl = my_lch.into_color; my_hsl.lightness *= 0.6; let my_new_rgb = from_color;
This image shows the starting color and the results of the two changes:
Most of the common color spaces are already implemented in Palette, but some situations may require something more customized. The conversion traits make it possible to integrate custom color types into the system. For example, this can be used for adding new color spaces or making a simpler user-facing API.
A longer and more advanced example that shows how to implement the conversion traits for a custom color type can be found further down.
Pixels And Buffers
When working with image or pixel buffers, or any color type that can be converted to a slice of components (ex.
cast module provides functions for turning them into slices of Palette colors without cloning the whole buffer:
use ; // The input to this function could be data from an image file or // maybe a texture in a game.
It's also possible to create a single color from a slice or array. Let's say we are using something that implements
This makes it possible to use Palette with any other crate that can convert their color types to slices and arrays, with minimal glue code and little to no overhead. It's also possible to go the opposite direction and convert Palette types to slices and arrays.
Palette comes with a number of color operations built in, such as saturate/desaturate, hue shift, etc., in the form of operator traits. That means it's possible to write generic functions that perform these operation on any color space that supports them. The output will vary depending on the color space's characteristics.
use ; let new_hsl = transform_color; let new_hsv = transform_color;
This image shows the transition from the color to
new_color in HSL and HSV:
In addition to the operator traits, the SVG blend and composition functions have also been implemented.
use ; // The input to this function could be data from image files.
|Image 1||Image 2||Result|
There's also the option to explicitly convert to and from premultiplied alpha, to avoid converting back and forth more than necessary, using the
Most color types are directly compatible with gradient and interpolation crates, such as
use ; use LinSrgb; let gradient = equidistant_unchecked; let taken_colors: = gradient.take.collect;
Here's the gradient as both its continuous form and as the 10 colors from
Customizing Color Spaces
The built-in color spaces have been made customizable to account for as much variation as possible. The more common variants have been exposed as type aliases (like
LinSrgb from above), but it's entirely possible to make custom compositions, including with entirely new parameters. For example, making up your own RGB standard:
use ; // RgbStandard and RgbSpace are implemented for 2 and 3 element tuples, // allowing mixing and matching of existing types. In this case we are // combining sRGB primaries, the CIE equal energy white point and the // sRGB transfer function (a.k.a. encoding or gamma). type EqualEnergyStandard = ; type EqualEnergySrgb<T> = ; let ee_rgb = new; // We need to use chromatic adaptation when going between white points. let srgb = adapt_from;
It's also possible to implement the traits for a custom type, for when the built-in options are not enough.
Converting Custom Color Types
The following example shows how it's possible for Palette users to convert from and into a custom made
Color type. It's not exactly a one-liner, but it can still save a lot of repetitive manual work.
use ; // This implements conversion to and from all Palette colors. // We have to tell Palette that we will take care of converting to/from sRGB. // There's no blanket implementation for Self -> Self, unlike the From trait. // This is to better allow cases like Self<A> -> Self<B>. // Convert from any kind of f32 sRGB. // Convert into any kind of f32 sRGB. // Add the required clamping. // This function uses only our `Color`, but Palette users can convert to it. do_something; do_something; // This function has the conversion built in and takes any compatible // color type as input. generic_do_something; generic_do_something;
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.