Color Kit
colorkit is a lightweight #[no_std] color crate for Rust.
It provides an easy to use and strongly typed conversions between color spaces. Color Kit also provides layout and quantization tools for working with pixel data. One can also implement additional/custom color spaces and layouts.
Color Kit Overview
#[no_std]friendly and dependency-free.- Typed color spaces: [
Srgb], [LinSrgb], [OkLab], [Xyz<WhitePoint>] etc... - Conversion API via [
FromColor] / [IntoColor]. - Alpha wrappers for normal and premultiplied color: [
Alpha<S>],AlphaPre<S>. - Layout and quantization primitives:
Planar,MappedLayout,Packed565. - Built-in rounding and optional dithering hooks for scalar/layout conversions.
Getting Started
Add colorkit to your Cargo.toml:
[]
= "0.1.0"
Quick Start
Color spaces are simple types that implement the [ColorSpace] trait and it's super traits. Probably the most important super traits would be [ColorNew] and [ColorSlice] when working generically with color spaces.
Conversion
Convert between color spaces with [IntoColor]:
use ;
let srgb = new_u8;
let lab: OkLab = srgb.into_color;
let srgb_roundtrip: Srgb = lab.into_color;
Modifying Channel Data
This example first converts to the OkLab Perceptual color space, makes a few changes and outputs back into sRGB.
use ;
let input = new;
let mut lab: OkLab = input.into_color;
// Example adjustment in OkLab space:
lab.set_l;
lab += lab * -2.5; // Index access of `a` channel.
let output: Srgb = lab.into_color;
This image shows the starting color and the end result:

Data Layouts
Color space types such Srgb, OkLab etc... use f32 for channel data and have pre-defined channel order. Color data though likely is stored in other layouts. RGB data for example is very often stored as a bunch octets. Instead of trying to encode that potential complexity for channel order and type directly into each color space type. This may be handled by layouts instead. Layouts are types that simply implement the Layout trait. For example the Planar layout is for array like data layouts. MappedLayout is a wrapper around a Layout, but allows one to change the channel order (e.g. ARGB, RGBA, ...) using a LayoutMap.
Basic Usage Example
This example shows how to use layouts to load/quantize data to and from a color space via the ColorLayout trait.
use Planar;
use Rounding;
use ColorLayout;
use ;
let mut data_out = ;
let data_in: = ;
let chk_in = data_in..0;
let chk_out = data_out..0;
for in chk_in.iter.zip
This image shows the starting color data above and the result below:

Load and Store in Different Channel Orders
While the color spaces generally have a default order, data may or may not be stored in the default order. For example Srgb assumes red = 0, green = 1, blue = 2, but we may for example have data in the order AGBR ect...
use Map4;
use ;
use Rounding;
use ColorLayout;
use ;
// Load from AGBR Order
let data: = ;
let layout = as_mapped;
let color = from_layout;
assert_eq!; // Do stuff with the color
// Quantize to u16 in RGBA order, this is canonical channel order
// for Alpha<Srgb> so there is no need for a Mapped layout here.
let out: = color..into;
assert_eq!;
Project Status
colorkit is currently at 0.1.x, so the focus has mostly been on a clean API for color conversions and layout handling.
Contributing
Any PRs, Feedback and/or issues are welcome. Some things I definitely want are:
- Improve documentation and create a collection of useful examples.
- Add more commonly used Color Spaces
- Add more color operations in the
./opsdirectory (definitely needed) - Identify any common packed layouts and implement those.
- Think about and implement SIMD acceleration.
License
Color Kit is licensed under the following license:
- MIT license (LICENSE or http://opensource.org/licenses/MIT)