Skip to main content

colr_types/model/
lab.rs

1//! CIELab and CIELCh color models.
2
3use core::marker::PhantomData;
4
5use crate::{BackingStore, ChannelMap};
6use crate::illuminant::{D65, Illuminant};
7
8/// CIE 1976 L*a*b* color space under reference white W.
9///
10/// Perceptually uniform for small color differences. L* is lightness
11/// [0, 100], a* is green-red [-128, 127], b* is blue-yellow [-128, 127].
12/// Reference: CIE 015:2018.
13///
14/// `OFFSET` is the storage index of the first color channel. `0` (default)
15/// places color channels at [0, 1, 2] with alpha at 3. `1` places color
16/// channels at [1, 2, 3] with alpha at 0, matching alpha-first texture layouts.
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
18pub struct Lab<W: Illuminant = D65, const OFFSET: usize = 0>(PhantomData<W>);
19
20/// CIE L*C*h*, the polar form of CIELab.
21///
22/// L* is lightness, C* is chroma, h is hue angle in radians [0, 2pi).
23///
24/// `OFFSET` is the storage index of the first color channel. `0` (default)
25/// places color channels at [0, 1, 2] with alpha at 3. `1` places color
26/// channels at [1, 2, 3] with alpha at 0, matching alpha-first texture layouts.
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
28pub struct LCh<W: Illuminant = D65, const OFFSET: usize = 0>(PhantomData<W>);
29
30impl<W: Illuminant, const OFFSET: usize> BackingStore<[f32; 3]> for Lab<W, OFFSET> {}
31impl<W: Illuminant, const OFFSET: usize> BackingStore<[f32; 4]> for Lab<W, OFFSET> {}
32impl<W: Illuminant, const OFFSET: usize> BackingStore<[f32; 3]> for LCh<W, OFFSET> {}
33impl<W: Illuminant, const OFFSET: usize> BackingStore<[f32; 4]> for LCh<W, OFFSET> {}
34
35impl<W: Illuminant, const OFFSET: usize> ChannelMap<4> for Lab<W, OFFSET> {
36    const INDICES: [usize; 4] = [OFFSET, OFFSET + 1, OFFSET + 2, 3 - OFFSET * 3];
37}
38
39impl<W: Illuminant, const OFFSET: usize> ChannelMap<4> for LCh<W, OFFSET> {
40    const INDICES: [usize; 4] = [OFFSET, OFFSET + 1, OFFSET + 2, 3 - OFFSET * 3];
41}
42
43#[cfg(feature = "glam")]
44mod glam_impls {
45    use super::*;
46    impl<W: Illuminant, const OFFSET: usize> BackingStore<glam::Vec3> for Lab<W, OFFSET> {}
47    impl<W: Illuminant, const OFFSET: usize> BackingStore<glam::Vec3A> for Lab<W, OFFSET> {}
48    impl<W: Illuminant, const OFFSET: usize> BackingStore<glam::Vec4> for Lab<W, OFFSET> {}
49    impl<W: Illuminant, const OFFSET: usize> BackingStore<glam::Vec3> for LCh<W, OFFSET> {}
50    impl<W: Illuminant, const OFFSET: usize> BackingStore<glam::Vec3A> for LCh<W, OFFSET> {}
51    impl<W: Illuminant, const OFFSET: usize> BackingStore<glam::Vec4> for LCh<W, OFFSET> {}
52}