#color #rgb #colorspace

tincture

A crate for converting colors between different color spaces

2 unstable releases

0.5.0 Jan 7, 2021
0.4.3 Jan 7, 2021
0.3.0 Jan 6, 2021
0.2.0 Jan 6, 2021
0.1.1 Jan 6, 2021

#32 in #rgb

Download history 110/week @ 2021-02-19 368/week @ 2021-02-26 281/week @ 2021-03-05 56/week @ 2021-03-12 109/week @ 2021-03-19 121/week @ 2021-03-26 43/week @ 2021-04-02 27/week @ 2021-04-09 106/week @ 2021-04-16 32/week @ 2021-04-23 24/week @ 2021-04-30 28/week @ 2021-05-07 33/week @ 2021-05-14 53/week @ 2021-05-21 56/week @ 2021-05-28 30/week @ 2021-06-04

443 downloads per month
Used in mottle

MIT/Apache

16KB
340 lines

tincture

crates.io version docs.rs docs

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());

No runtime deps