Palette 0.7.3

10 Aug 2023 — Permalink

Here’s a smaller update of Palette, that contains a bug fix in the Oklch to Oklab conversion, and new traits for more convenient casting.

For those who aren’t familiar with it, Palette is a Rust library for working with colors, including color conversion and color management. Its features range from common tasks, like color blending or converting HSL to RGB, to more advanced topics. It leverages Rust’s type system, by encoding color space information as type parameters, to be able to prevent mistakes and to keep it open to extension and customization.

Fix Oklch to Oklab Conversion

A mistake had made its way into the conversion from Oklch to Oklab, that caused the a and b values to be swapped. This update contains a correction for that.

QoL Traits For Array And Unsigned Integer Casting

Version 0.7.0 came with a new system for casting colors to and from arrays and integers. The API for this consisted of a set of functions for casting specific collections, such as slice to slice and array to array. This update adds a set of traits that improves this API, the same as From, Into, AsRef and AsMut improves conversion.

use palette::{cast::ComponentsAs, Srgb};

let buffer = [64, 139, 10, 93, 18, 214];
let color_buffer: &[Srgb<u8>] = buffer.components_as();

All the traits can be found in the cast module, and there are quite a few of them! There are different traits for each representation, so for arrays, components and unsigned integers. This is necessary to remove ambiguity, since the compiler would have a hard time figuring out if an array of numbers is meant to be components or integers, for example. Explicit names make the intent clear:

use palette::{
    cast::{ComponentsAs, UintsAs},
    rgb::PackedRgba,
    Srgb,
};

let buffer = [64u8, 139, 10, 93, 18, 214];

let color_buffer1: &[Srgb<u8>] = buffer.components_as(); // Ok
let color_buffer2: &[PackedRgba] = buffer.uints_as(); // Error, expected u32

In addition to having specific traits for specific representations, there are traits for both borrowing and owned casting. As well as variants for from and into colors, and from and into arrays/components/uints. There’s hopefully a casting trait for every situation by now.

Wrapping Up

I want to give a big thank you to everyone who contributed or took part in discussions. Your help is always appreciated!

Palette can be found on GitHub, and on crates.io.

Thank you for reading!