## tincture

A crate for converting colors between different color spaces

### 2 unstable releases

 0.5.0 Jan 7, 2021 Jan 7, 2021 Jan 6, 2021 Jan 6, 2021 Jan 6, 2021

#32 in #rgb

Used in mottle

MIT/Apache

16KB
340 lines

# tincture

A crate for converting colors between different color spaces. See the docs for more.

### `lib.rs`:

A crate for converting colors between different color spaces.

Color spaces can be converted between one another using [`convert`]:

``````use tincture::{LinearRgb, Oklab};

let rebeccapurple = LinearRgb {
r: 0.4,
g: 0.2,
b: 0.6,
};

let oklab: Oklab = tincture::convert(rebeccapurple);

assert_eq!(
oklab,
Oklab {
l: 0.66066486,
a: 0.079970956,
b: -0.095915854,
},
);
``````

Variations on the core color spaces do not implement [`CoreColorSpace`], which is necessary for [`convert`]. Instead, they implement `From<ACoreColorSpace>`, allowing you to convert this variation to its corresponding core color space and call [`convert`]. Examples of variations include [`Oklch`] (a variation on [`Oklab`]) and [`Srgb`] (a variation on [`LinearRgb`]).

``````use tincture::{Hue, LinearRgb, Oklab, Oklch, Srgb};

// `Oklch` is a variation on `Oklab` (`Oklch` uses polar coordinates).
let peach = Oklch {
l: 0.8,
c: 0.25,
h: Hue::from_degrees(40.0).unwrap(),
};

// This means we can create an `Oklab` using `From`.
let oklab = Oklab::from(peach);

// We can now convert `oklab` to any other core color space, such as `LinearRgb`.
let linear_rgb: LinearRgb = tincture::convert(oklab);

// `Srgb` is a variant of `LinearRgb`, so we again create one using `From`.
let srgb = Srgb::from(linear_rgb);
``````

All color spaces implement [`ColorSpace`], which provides the constants `BLACK` and `WHITE`:

``````use tincture::{ColorSpace, Srgb};

assert_eq!(Srgb::BLACK, Srgb { r: 0.0, g: 0.0, b: 0.0 });
assert_eq!(Srgb::WHITE, Srgb { r: 1.0, g: 1.0, b: 1.0 });
``````

[`ColorSpace`] also provides the [`in_bounds`] method:

``````use tincture::{ColorSpace, Srgb};

let out_of_bounds = Srgb {
r: 2.0,
g: -100.0,
b: 0.5,
};

let in_bounds = Srgb {
r: 0.25,
g: 0.75,
b: 0.25,
};

assert!(!out_of_bounds.in_bounds());
assert!(in_bounds.in_bounds());
``````