Palette 0.7.4

28 Jan 2024 — Permalink

New year, more Palette!

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.

alloc Feature

A new alloc Cargo feature has been added for the parts that make user of allocating types (i.e. Box<T> and Vec<T>). This used to be part of the std feature, but separating it makes it possible to use these types without requiring the whole std library. Those who already have std enabled will get both, so there’s no need to do anything in that case. The parts that still require std are Error trait implementations. std will also provide floating point operations, but libm can be used instead.

More Color Difference Traits

This version adds the DeltaE trait (not to be confused with Ciede2000), which represents the “default” ΔE color distance formula for a color space. This is the formula that is specified in the specification or research paper that introduced the color space, and is usually an alias for EuclideanDistance. There may be better options, but this is the “original” for those that have one.

In addition to DeltaE, two other traits are added. These are ImprovedDeltaE and ImprovedCiede2000, representing modified versions of DeltaE and Ciede2000, according to Power functions improving the performance of color-difference formulas by Huang et al. These add a power function, fine tuned for each formula, to make their results better on average. It’s a more costly option, but may be worth it for the increased accuracy.

Rgba::from_str

It was previously possible to parse a hexadecimal string as Rgb<_, u8>. Now it’s also possible to parse it with an alpha channel, thanks to rafaelbeckel.

let rgba_from_hex: Srgba<u8> = "#f034e652".parse().unwrap(); // #RRGGBBAA

This also supports the shorter #RGBA format and the # is optional on both cases.

Documentation Improvements

The documentation had been updated with some more introductory info, quick links at the top of the main page, and a number of small usage examples for Rgb. The hope is that this will be helpful for those who are new to the library and make it easier to get started with the most common parts. More color types will get similar examples later, but this is a start.

A typo in the documentation for the Stimulus trait was also kindly fixed by MultisampledNight.

Okhsl Fix

There were a couple of cases where conversion from Oklab to Okhsl could return NaN values. Particularly if the lightness is 1.0 or 0.0 and a or b is something other than 0.0. This has been fixed and only actual numbers should be returned now.

NaN values are usually considered bugs, so any reports of them are appreciated.

Refactor and Hwb Equality

The internals of the crate have been refactored to reduce some repetition, with the help of macros. Palette has been using macros for this for quite a while, since a lot of the traits have very similar implementations across the color types, but it was time for another round of macrofication. It’s nice to be able to delete around 2000 lines.

As part of this, the equality check for Hwb has been changed to always take the hue into account. This brings it in line with all the other hue based color spaces, which should have been done long ago. Hwb is an odd color space, compared to the rest, so it didn’t fit into most of the earlier macros. Nowadays, it’s joined by Okhwb, and the incentive to add macros for both of them was higher. This mistake was found while unifying their implementations.

This shouldn’t affect anyone, unless you happen to rely on Hwb for color comparison. In that case, it’s strongly recommended to use a color space without hue, to consider the traits from the color_difference module, or to implement your own equality check.

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!