Skip to main content

colr_types/model/
oklab.rs

1//! Oklab and Oklch color models.
2
3use crate::{BackingStore, ChannelMap};
4
5/// Oklab perceptual color space by Björn Ottosson, 2020.
6///
7/// Better hue linearity than CIELab. L is lightness [0, 1], a and b are
8/// opponent axes. D65 adaptation is baked into the specification.
9/// Adopted in CSS Color Level 4.
10///
11/// `OFFSET` is the storage index of the first color channel. `0` (default)
12/// places color channels at [0, 1, 2] with alpha at 3. `1` places color
13/// channels at [1, 2, 3] with alpha at 0, matching alpha-first texture layouts.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
15pub struct Oklab<const OFFSET: usize = 0>;
16
17/// Oklch, the polar form of Oklab.
18///
19/// L is lightness [0, 1], C is chroma, h is hue angle in radians [0, 2pi).
20///
21/// `OFFSET` is the storage index of the first color channel. `0` (default)
22/// places color channels at [0, 1, 2] with alpha at 3. `1` places color
23/// channels at [1, 2, 3] with alpha at 0, matching alpha-first texture layouts.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
25pub struct Oklch<const OFFSET: usize = 0>;
26
27impl<const OFFSET: usize> BackingStore<[f32; 3]> for Oklab<OFFSET> {}
28impl<const OFFSET: usize> BackingStore<[f32; 4]> for Oklab<OFFSET> {}
29impl<const OFFSET: usize> BackingStore<[f32; 3]> for Oklch<OFFSET> {}
30impl<const OFFSET: usize> BackingStore<[f32; 4]> for Oklch<OFFSET> {}
31
32impl<const OFFSET: usize> ChannelMap<4> for Oklab<OFFSET> {
33    const INDICES: [usize; 4] = [OFFSET, OFFSET + 1, OFFSET + 2, 3 - OFFSET * 3];
34}
35
36impl<const OFFSET: usize> ChannelMap<4> for Oklch<OFFSET> {
37    const INDICES: [usize; 4] = [OFFSET, OFFSET + 1, OFFSET + 2, 3 - OFFSET * 3];
38}
39
40#[cfg(feature = "glam")]
41mod glam_impls {
42    use super::*;
43    impl<const OFFSET: usize> BackingStore<glam::Vec3> for Oklab<OFFSET> {}
44    impl<const OFFSET: usize> BackingStore<glam::Vec3A> for Oklab<OFFSET> {}
45    impl<const OFFSET: usize> BackingStore<glam::Vec4> for Oklab<OFFSET> {}
46    impl<const OFFSET: usize> BackingStore<glam::Vec3> for Oklch<OFFSET> {}
47    impl<const OFFSET: usize> BackingStore<glam::Vec3A> for Oklch<OFFSET> {}
48    impl<const OFFSET: usize> BackingStore<glam::Vec4> for Oklch<OFFSET> {}
49}