style/color/
convert.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Color conversion algorithms.
6//!
7//! Algorithms, matrices and constants are from the [color-4] specification,
8//! unless otherwise specified:
9//!
10//! https://drafts.csswg.org/css-color-4/#color-conversion-code
11//!
12//! NOTE: Matrices has to be transposed from the examples in the spec for use
13//! with the `euclid` library.
14
15use crate::color::ColorComponents;
16use crate::values::normalize;
17
18type Transform = euclid::default::Transform3D<f32>;
19type Vector = euclid::default::Vector3D<f32>;
20
21/// Normalize hue into [0, 360).
22#[inline]
23pub fn normalize_hue(hue: f32) -> f32 {
24    hue - 360. * (hue / 360.).floor()
25}
26
27/// Calculate the hue from RGB components and return it along with the min and
28/// max RGB values.
29#[inline]
30fn rgb_to_hue_min_max(red: f32, green: f32, blue: f32) -> (f32, f32, f32) {
31    let max = red.max(green).max(blue);
32    let min = red.min(green).min(blue);
33
34    let delta = max - min;
35
36    let hue = if delta != 0.0 {
37        60.0 * if max == red {
38            (green - blue) / delta + if green < blue { 6.0 } else { 0.0 }
39        } else if max == green {
40            (blue - red) / delta + 2.0
41        } else {
42            (red - green) / delta + 4.0
43        }
44    } else {
45        f32::NAN
46    };
47
48    (hue, min, max)
49}
50
51/// Convert from HSL notation to RGB notation.
52/// https://drafts.csswg.org/css-color-4/#hsl-to-rgb
53#[inline]
54pub fn hsl_to_rgb(from: &ColorComponents) -> ColorComponents {
55    fn hue_to_rgb(t1: f32, t2: f32, hue: f32) -> f32 {
56        let hue = normalize_hue(hue);
57
58        if hue * 6.0 < 360.0 {
59            t1 + (t2 - t1) * hue / 60.0
60        } else if hue * 2.0 < 360.0 {
61            t2
62        } else if hue * 3.0 < 720.0 {
63            t1 + (t2 - t1) * (240.0 - hue) / 60.0
64        } else {
65            t1
66        }
67    }
68
69    // Convert missing components to 0.0.
70    let ColorComponents(hue, saturation, lightness) = from.map(normalize);
71    let saturation = saturation / 100.0;
72    let lightness = lightness / 100.0;
73
74    let t2 = if lightness <= 0.5 {
75        lightness * (saturation + 1.0)
76    } else {
77        lightness + saturation - lightness * saturation
78    };
79    let t1 = lightness * 2.0 - t2;
80
81    ColorComponents(
82        hue_to_rgb(t1, t2, hue + 120.0),
83        hue_to_rgb(t1, t2, hue),
84        hue_to_rgb(t1, t2, hue - 120.0),
85    )
86}
87
88/// Convert from RGB notation to HSL notation.
89/// https://drafts.csswg.org/css-color-4/#rgb-to-hsl
90pub fn rgb_to_hsl(from: &ColorComponents) -> ColorComponents {
91    let ColorComponents(red, green, blue) = *from;
92
93    let (hue, min, max) = rgb_to_hue_min_max(red, green, blue);
94
95    let lightness = (min + max) / 2.0;
96    let delta = max - min;
97
98    let saturation = if delta != 0.0 {
99        if lightness == 0.0 || lightness == 1.0 {
100            0.0
101        } else {
102            (max - lightness) / lightness.min(1.0 - lightness)
103        }
104    } else {
105        0.0
106    };
107
108    ColorComponents(hue, saturation * 100.0, lightness * 100.0)
109}
110
111/// Convert from HWB notation to RGB notation.
112/// https://drafts.csswg.org/css-color-4/#hwb-to-rgb
113#[inline]
114pub fn hwb_to_rgb(from: &ColorComponents) -> ColorComponents {
115    // Convert missing components to 0.0.
116    let ColorComponents(hue, whiteness, blackness) = from.map(normalize);
117
118    let whiteness = whiteness / 100.0;
119    let blackness = blackness / 100.0;
120
121    if whiteness + blackness >= 1.0 {
122        let gray = whiteness / (whiteness + blackness);
123        return ColorComponents(gray, gray, gray);
124    }
125
126    let x = 1.0 - whiteness - blackness;
127    hsl_to_rgb(&ColorComponents(hue, 100.0, 50.0)).map(|v| v * x + whiteness)
128}
129
130/// Convert from RGB notation to HWB notation.
131/// https://drafts.csswg.org/css-color-4/#rgb-to-hwb
132#[inline]
133pub fn rgb_to_hwb(from: &ColorComponents) -> ColorComponents {
134    let ColorComponents(red, green, blue) = *from;
135
136    let (hue, min, max) = rgb_to_hue_min_max(red, green, blue);
137
138    let whiteness = min;
139    let blackness = 1.0 - max;
140
141    ColorComponents(hue, whiteness * 100.0, blackness * 100.0)
142}
143
144/// Calculate an epsilon for a specified range.
145#[inline]
146pub fn epsilon_for_range(min: f32, max: f32) -> f32 {
147    (max - min) / 1.0e5
148}
149
150/// Convert from the rectangular orthogonal to the cylindrical polar coordinate
151/// system. This is used to convert (ok)lab to (ok)lch.
152/// <https://drafts.csswg.org/css-color-4/#lab-to-lch>
153#[inline]
154pub fn orthogonal_to_polar(from: &ColorComponents, e: f32) -> ColorComponents {
155    let ColorComponents(lightness, a, b) = *from;
156
157    let chroma = (a * a + b * b).sqrt();
158
159    let hue = if a.abs() < e && b.abs() < e {
160        // For extremely small values of a and b ... the reported hue angle
161        // swinging about wildly and being essentially random ... this means
162        // the hue is powerless, and treated as missing when converted into LCH
163        // or Oklch.
164        f32::NAN
165    } else if chroma.abs() < e {
166        // Very small chroma values make the hue component powerless.
167        f32::NAN
168    } else {
169        normalize_hue(b.atan2(a).to_degrees())
170    };
171
172    ColorComponents(lightness, chroma, hue)
173}
174
175/// Convert from the cylindrical polar to the rectangular orthogonal coordinate
176/// system. This is used to convert (ok)lch to (ok)lab.
177/// <https://drafts.csswg.org/css-color-4/#lch-to-lab>
178#[inline]
179pub fn polar_to_orthogonal(from: &ColorComponents) -> ColorComponents {
180    let ColorComponents(lightness, chroma, hue) = *from;
181
182    // A missing hue component results in an achromatic color.
183    if hue.is_nan() {
184        return ColorComponents(lightness, 0.0, 0.0);
185    }
186
187    let hue = hue.to_radians();
188    let a = chroma * hue.cos();
189    let b = chroma * hue.sin();
190
191    ColorComponents(lightness, a, b)
192}
193
194#[inline]
195fn transform(from: &ColorComponents, mat: &Transform) -> ColorComponents {
196    let result = mat.transform_vector3d(Vector::new(from.0, from.1, from.2));
197    ColorComponents(result.x, result.y, result.z)
198}
199
200fn xyz_d65_to_xyz_d50(from: &ColorComponents) -> ColorComponents {
201    #[rustfmt::skip]
202    const MAT: Transform = Transform::new(
203         1.0479298208405488,    0.029627815688159344, -0.009243058152591178, 0.0,
204         0.022946793341019088,  0.990434484573249,     0.015055144896577895, 0.0,
205        -0.05019222954313557,  -0.01707382502938514,   0.7518742899580008,   0.0,
206         0.0,                   0.0,                   0.0,                  1.0,
207    );
208
209    transform(from, &MAT)
210}
211
212fn xyz_d50_to_xyz_d65(from: &ColorComponents) -> ColorComponents {
213    #[rustfmt::skip]
214    const MAT: Transform = Transform::new(
215         0.9554734527042182,   -0.028369706963208136,  0.012314001688319899, 0.0,
216        -0.023098536874261423,  1.0099954580058226,   -0.020507696433477912, 0.0,
217         0.0632593086610217,    0.021041398966943008,  1.3303659366080753,   0.0,
218         0.0,                   0.0,                   0.0,                  1.0,
219    );
220
221    transform(from, &MAT)
222}
223
224/// A reference white that is used during color conversion.
225pub enum WhitePoint {
226    /// D50 white reference.
227    D50,
228    /// D65 white reference.
229    D65,
230}
231
232impl WhitePoint {
233    const fn values(&self) -> ColorComponents {
234        // <https://drafts.csswg.org/css-color-4/#color-conversion-code>
235        match self {
236            // [0.3457 / 0.3585, 1.00000, (1.0 - 0.3457 - 0.3585) / 0.3585]
237            WhitePoint::D50 => ColorComponents(0.9642956764295677, 1.0, 0.8251046025104602),
238            // [0.3127 / 0.3290, 1.00000, (1.0 - 0.3127 - 0.3290) / 0.3290]
239            WhitePoint::D65 => ColorComponents(0.9504559270516716, 1.0, 1.0890577507598784),
240        }
241    }
242}
243
244fn convert_white_point(from: WhitePoint, to: WhitePoint, components: &mut ColorComponents) {
245    match (from, to) {
246        (WhitePoint::D50, WhitePoint::D65) => *components = xyz_d50_to_xyz_d65(components),
247        (WhitePoint::D65, WhitePoint::D50) => *components = xyz_d65_to_xyz_d50(components),
248        _ => {},
249    }
250}
251
252/// A trait that allows conversion of color spaces to and from XYZ coordinate
253/// space with a specified white point.
254///
255/// Allows following the specified method of converting between color spaces:
256/// - Convert to values to sRGB linear light.
257/// - Convert to XYZ coordinate space.
258/// - Adjust white point to target white point.
259/// - Convert to sRGB linear light in target color space.
260/// - Convert to sRGB gamma encoded in target color space.
261///
262/// https://drafts.csswg.org/css-color-4/#color-conversion
263pub trait ColorSpaceConversion {
264    /// The white point that the implementer is represented in.
265    const WHITE_POINT: WhitePoint;
266
267    /// Convert the components from sRGB gamma encoded values to sRGB linear
268    /// light values.
269    fn to_linear_light(from: &ColorComponents) -> ColorComponents;
270
271    /// Convert the components from sRGB linear light values to XYZ coordinate
272    /// space.
273    fn to_xyz(from: &ColorComponents) -> ColorComponents;
274
275    /// Convert the components from XYZ coordinate space to sRGB linear light
276    /// values.
277    fn from_xyz(from: &ColorComponents) -> ColorComponents;
278
279    /// Convert the components from sRGB linear light values to sRGB gamma
280    /// encoded values.
281    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents;
282}
283
284/// Convert the color components from the specified color space to XYZ and
285/// return the components and the white point they are in.
286pub fn to_xyz<From: ColorSpaceConversion>(from: &ColorComponents) -> (ColorComponents, WhitePoint) {
287    // Convert the color components where in-gamut values are in the range
288    // [0 - 1] to linear light (un-companded) form.
289    let result = From::to_linear_light(from);
290
291    // Convert the color components from the source color space to XYZ.
292    (From::to_xyz(&result), From::WHITE_POINT)
293}
294
295/// Convert the color components from XYZ at the given white point to the
296/// specified color space.
297pub fn from_xyz<To: ColorSpaceConversion>(
298    from: &ColorComponents,
299    white_point: WhitePoint,
300) -> ColorComponents {
301    let mut xyz = from.clone();
302
303    // Convert the white point if needed.
304    convert_white_point(white_point, To::WHITE_POINT, &mut xyz);
305
306    // Convert the color from XYZ to the target color space.
307    let result = To::from_xyz(&xyz);
308
309    // Convert the color components of linear-light values in the range
310    // [0 - 1] to a gamma corrected form.
311    To::to_gamma_encoded(&result)
312}
313
314/// The sRGB color space.
315/// https://drafts.csswg.org/css-color-4/#predefined-sRGB
316pub struct Srgb;
317
318impl Srgb {
319    #[rustfmt::skip]
320    const TO_XYZ: Transform = Transform::new(
321        0.4123907992659595,  0.21263900587151036, 0.01933081871559185, 0.0,
322        0.35758433938387796, 0.7151686787677559,  0.11919477979462599, 0.0,
323        0.1804807884018343,  0.07219231536073371, 0.9505321522496606,  0.0,
324        0.0,                 0.0,                 0.0,                 1.0,
325    );
326
327    #[rustfmt::skip]
328    const FROM_XYZ: Transform = Transform::new(
329         3.2409699419045213, -0.9692436362808798,  0.05563007969699361, 0.0,
330        -1.5373831775700935,  1.8759675015077206, -0.20397695888897657, 0.0,
331        -0.4986107602930033,  0.04155505740717561, 1.0569715142428786,  0.0,
332         0.0,                 0.0,                 0.0,                 1.0,
333    );
334}
335
336impl ColorSpaceConversion for Srgb {
337    const WHITE_POINT: WhitePoint = WhitePoint::D65;
338
339    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
340        from.clone().map(|value| {
341            let abs = value.abs();
342
343            if abs < 0.04045 {
344                value / 12.92
345            } else {
346                value.signum() * ((abs + 0.055) / 1.055).powf(2.4)
347            }
348        })
349    }
350
351    fn to_xyz(from: &ColorComponents) -> ColorComponents {
352        transform(from, &Self::TO_XYZ)
353    }
354
355    fn from_xyz(from: &ColorComponents) -> ColorComponents {
356        transform(from, &Self::FROM_XYZ)
357    }
358
359    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
360        from.clone().map(|value| {
361            let abs = value.abs();
362
363            if abs > 0.0031308 {
364                value.signum() * (1.055 * abs.powf(1.0 / 2.4) - 0.055)
365            } else {
366                12.92 * value
367            }
368        })
369    }
370}
371
372/// Color specified with hue, saturation and lightness components.
373pub struct Hsl;
374
375impl ColorSpaceConversion for Hsl {
376    const WHITE_POINT: WhitePoint = Srgb::WHITE_POINT;
377
378    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
379        Srgb::to_linear_light(&hsl_to_rgb(from))
380    }
381
382    #[inline]
383    fn to_xyz(from: &ColorComponents) -> ColorComponents {
384        Srgb::to_xyz(from)
385    }
386
387    #[inline]
388    fn from_xyz(from: &ColorComponents) -> ColorComponents {
389        Srgb::from_xyz(from)
390    }
391
392    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
393        rgb_to_hsl(&Srgb::to_gamma_encoded(from))
394    }
395}
396
397/// Color specified with hue, whiteness and blackness components.
398pub struct Hwb;
399
400impl ColorSpaceConversion for Hwb {
401    const WHITE_POINT: WhitePoint = Srgb::WHITE_POINT;
402
403    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
404        Srgb::to_linear_light(&hwb_to_rgb(from))
405    }
406
407    #[inline]
408    fn to_xyz(from: &ColorComponents) -> ColorComponents {
409        Srgb::to_xyz(from)
410    }
411
412    #[inline]
413    fn from_xyz(from: &ColorComponents) -> ColorComponents {
414        Srgb::from_xyz(from)
415    }
416
417    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
418        rgb_to_hwb(&Srgb::to_gamma_encoded(from))
419    }
420}
421
422/// The same as sRGB color space, except the transfer function is linear light.
423/// https://drafts.csswg.org/css-color-4/#predefined-sRGB-linear
424pub struct SrgbLinear;
425
426impl ColorSpaceConversion for SrgbLinear {
427    const WHITE_POINT: WhitePoint = Srgb::WHITE_POINT;
428
429    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
430        // Already in linear light form.
431        from.clone()
432    }
433
434    fn to_xyz(from: &ColorComponents) -> ColorComponents {
435        Srgb::to_xyz(from)
436    }
437
438    fn from_xyz(from: &ColorComponents) -> ColorComponents {
439        Srgb::from_xyz(from)
440    }
441
442    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
443        // Stay in linear light form.
444        from.clone()
445    }
446}
447
448/// The Display-P3 color space.
449/// https://drafts.csswg.org/css-color-4/#predefined-display-p3
450pub struct DisplayP3;
451
452impl DisplayP3 {
453    #[rustfmt::skip]
454    const TO_XYZ: Transform = Transform::new(
455        0.48657094864821626, 0.22897456406974884, 0.0,                  0.0,
456        0.26566769316909294, 0.6917385218365062,  0.045113381858902575, 0.0,
457        0.1982172852343625,  0.079286914093745,   1.0439443689009757,   0.0,
458        0.0,                 0.0,                 0.0,                  1.0,
459    );
460
461    #[rustfmt::skip]
462    const FROM_XYZ: Transform = Transform::new(
463         2.4934969119414245,  -0.829488969561575,    0.035845830243784335, 0.0,
464        -0.9313836179191236,   1.7626640603183468,  -0.07617238926804171,  0.0,
465        -0.40271078445071684,  0.02362468584194359,  0.9568845240076873,   0.0,
466         0.0,                  0.0,                  0.0,                  1.0,
467    );
468}
469
470impl ColorSpaceConversion for DisplayP3 {
471    const WHITE_POINT: WhitePoint = WhitePoint::D65;
472
473    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
474        Srgb::to_linear_light(from)
475    }
476
477    fn to_xyz(from: &ColorComponents) -> ColorComponents {
478        transform(from, &Self::TO_XYZ)
479    }
480
481    fn from_xyz(from: &ColorComponents) -> ColorComponents {
482        transform(from, &Self::FROM_XYZ)
483    }
484
485    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
486        Srgb::to_gamma_encoded(from)
487    }
488}
489
490/// The a98-rgb color space.
491/// https://drafts.csswg.org/css-color-4/#predefined-a98-rgb
492pub struct A98Rgb;
493
494impl A98Rgb {
495    #[rustfmt::skip]
496    const TO_XYZ: Transform = Transform::new(
497        0.5766690429101308,  0.29734497525053616, 0.027031361386412378, 0.0,
498        0.18555823790654627, 0.627363566255466,   0.07068885253582714,  0.0,
499        0.18822864623499472, 0.07529145849399789, 0.9913375368376389,   0.0,
500        0.0,                 0.0,                 0.0,                  1.0,
501    );
502
503    #[rustfmt::skip]
504    const FROM_XYZ: Transform = Transform::new(
505         2.041587903810746,  -0.9692436362808798,   0.013444280632031024, 0.0,
506        -0.5650069742788596,  1.8759675015077206,  -0.11836239223101824,  0.0,
507        -0.3447313507783295,  0.04155505740717561,  1.0151749943912054,   0.0,
508         0.0,                 0.0,                  0.0,                  1.0,
509    );
510}
511
512impl ColorSpaceConversion for A98Rgb {
513    const WHITE_POINT: WhitePoint = WhitePoint::D65;
514
515    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
516        from.clone().map(|v| v.signum() * v.abs().powf(2.19921875))
517    }
518
519    fn to_xyz(from: &ColorComponents) -> ColorComponents {
520        transform(from, &Self::TO_XYZ)
521    }
522
523    fn from_xyz(from: &ColorComponents) -> ColorComponents {
524        transform(from, &Self::FROM_XYZ)
525    }
526
527    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
528        from.clone()
529            .map(|v| v.signum() * v.abs().powf(0.4547069271758437))
530    }
531}
532
533/// The ProPhoto RGB color space.
534/// https://drafts.csswg.org/css-color-4/#predefined-prophoto-rgb
535pub struct ProphotoRgb;
536
537impl ProphotoRgb {
538    #[rustfmt::skip]
539    const TO_XYZ: Transform = Transform::new(
540        0.7977604896723027,  0.2880711282292934,     0.0,                0.0,
541        0.13518583717574031, 0.7118432178101014,     0.0,                0.0,
542        0.0313493495815248,  0.00008565396060525902, 0.8251046025104601, 0.0,
543        0.0,                 0.0,                    0.0,                1.0,
544    );
545
546    #[rustfmt::skip]
547    const FROM_XYZ: Transform = Transform::new(
548         1.3457989731028281,  -0.5446224939028347,  0.0,                0.0,
549        -0.25558010007997534,  1.5082327413132781,  0.0,                0.0,
550        -0.05110628506753401,  0.02053603239147973, 1.2119675456389454, 0.0,
551         0.0,                  0.0,                 0.0,                1.0,
552    );
553}
554
555impl ColorSpaceConversion for ProphotoRgb {
556    const WHITE_POINT: WhitePoint = WhitePoint::D50;
557
558    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
559        from.clone().map(|value| {
560            const ET2: f32 = 16.0 / 512.0;
561
562            let abs = value.abs();
563
564            if abs <= ET2 {
565                value / 16.0
566            } else {
567                value.signum() * abs.powf(1.8)
568            }
569        })
570    }
571
572    fn to_xyz(from: &ColorComponents) -> ColorComponents {
573        transform(from, &Self::TO_XYZ)
574    }
575
576    fn from_xyz(from: &ColorComponents) -> ColorComponents {
577        transform(from, &Self::FROM_XYZ)
578    }
579
580    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
581        const ET: f32 = 1.0 / 512.0;
582
583        from.clone().map(|v| {
584            let abs = v.abs();
585            if abs >= ET {
586                v.signum() * abs.powf(1.0 / 1.8)
587            } else {
588                16.0 * v
589            }
590        })
591    }
592}
593
594/// The Rec.2020 color space.
595/// https://drafts.csswg.org/css-color-4/#predefined-rec2020
596pub struct Rec2020;
597
598impl Rec2020 {
599    const ALPHA: f32 = 1.09929682680944;
600    const BETA: f32 = 0.018053968510807;
601
602    #[rustfmt::skip]
603    const TO_XYZ: Transform = Transform::new(
604        0.6369580483012913,  0.26270021201126703,  0.0,                  0.0,
605        0.14461690358620838, 0.677998071518871,    0.028072693049087508, 0.0,
606        0.16888097516417205, 0.059301716469861945, 1.0609850577107909,   0.0,
607        0.0,                 0.0,                  0.0,                  1.0,
608    );
609
610    #[rustfmt::skip]
611    const FROM_XYZ: Transform = Transform::new(
612         1.7166511879712676, -0.666684351832489,    0.017639857445310915, 0.0,
613        -0.3556707837763924,  1.616481236634939,   -0.042770613257808655, 0.0,
614        -0.2533662813736598,  0.01576854581391113,  0.942103121235474,    0.0,
615         0.0,                 0.0,                  0.0,                  1.0,
616    );
617}
618
619impl ColorSpaceConversion for Rec2020 {
620    const WHITE_POINT: WhitePoint = WhitePoint::D65;
621
622    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
623        from.clone().map(|value| {
624            let abs = value.abs();
625
626            if abs < Self::BETA * 4.5 {
627                value / 4.5
628            } else {
629                value.signum() * ((abs + Self::ALPHA - 1.0) / Self::ALPHA).powf(1.0 / 0.45)
630            }
631        })
632    }
633
634    fn to_xyz(from: &ColorComponents) -> ColorComponents {
635        transform(from, &Self::TO_XYZ)
636    }
637
638    fn from_xyz(from: &ColorComponents) -> ColorComponents {
639        transform(from, &Self::FROM_XYZ)
640    }
641
642    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
643        from.clone().map(|v| {
644            let abs = v.abs();
645
646            if abs > Self::BETA {
647                v.signum() * (Self::ALPHA * abs.powf(0.45) - (Self::ALPHA - 1.0))
648            } else {
649                4.5 * v
650            }
651        })
652    }
653}
654
655/// A color in the XYZ coordinate space with a D50 white reference.
656/// https://drafts.csswg.org/css-color-4/#predefined-xyz
657pub struct XyzD50;
658
659impl ColorSpaceConversion for XyzD50 {
660    const WHITE_POINT: WhitePoint = WhitePoint::D50;
661
662    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
663        from.clone()
664    }
665
666    fn to_xyz(from: &ColorComponents) -> ColorComponents {
667        from.clone()
668    }
669
670    fn from_xyz(from: &ColorComponents) -> ColorComponents {
671        from.clone()
672    }
673
674    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
675        from.clone()
676    }
677}
678
679/// A color in the XYZ coordinate space with a D65 white reference.
680/// https://drafts.csswg.org/css-color-4/#predefined-xyz
681pub struct XyzD65;
682
683impl ColorSpaceConversion for XyzD65 {
684    const WHITE_POINT: WhitePoint = WhitePoint::D65;
685
686    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
687        from.clone()
688    }
689
690    fn to_xyz(from: &ColorComponents) -> ColorComponents {
691        from.clone()
692    }
693
694    fn from_xyz(from: &ColorComponents) -> ColorComponents {
695        from.clone()
696    }
697
698    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
699        from.clone()
700    }
701}
702
703/// The Lab color space.
704/// https://drafts.csswg.org/css-color-4/#specifying-lab-lch
705pub struct Lab;
706
707impl Lab {
708    const KAPPA: f32 = 24389.0 / 27.0;
709    const EPSILON: f32 = 216.0 / 24389.0;
710}
711
712impl ColorSpaceConversion for Lab {
713    const WHITE_POINT: WhitePoint = WhitePoint::D50;
714
715    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
716        // No need for conversion.
717        from.clone()
718    }
719
720    /// Convert a CIELAB color to XYZ as specified in [1] and [2].
721    ///
722    /// [1]: https://drafts.csswg.org/css-color/#lab-to-predefined
723    /// [2]: https://drafts.csswg.org/css-color/#color-conversion-code
724    fn to_xyz(from: &ColorComponents) -> ColorComponents {
725        let ColorComponents(lightness, a, b) = *from;
726
727        let f1 = (lightness + 16.0) / 116.0;
728        let f0 = f1 + a / 500.0;
729        let f2 = f1 - b / 200.0;
730
731        let f0_cubed = f0 * f0 * f0;
732        let x = if f0_cubed > Self::EPSILON {
733            f0_cubed
734        } else {
735            (116.0 * f0 - 16.0) / Self::KAPPA
736        };
737
738        let y = if lightness > Self::KAPPA * Self::EPSILON {
739            let v = (lightness + 16.0) / 116.0;
740            v * v * v
741        } else {
742            lightness / Self::KAPPA
743        };
744
745        let f2_cubed = f2 * f2 * f2;
746        let z = if f2_cubed > Self::EPSILON {
747            f2_cubed
748        } else {
749            (116.0 * f2 - 16.0) / Self::KAPPA
750        };
751
752        ColorComponents(x, y, z) * Self::WHITE_POINT.values()
753    }
754
755    /// Convert an XYZ color to LAB as specified in [1] and [2].
756    ///
757    /// [1]: https://drafts.csswg.org/css-color/#rgb-to-lab
758    /// [2]: https://drafts.csswg.org/css-color/#color-conversion-code
759    fn from_xyz(from: &ColorComponents) -> ColorComponents {
760        let adapted = *from / Self::WHITE_POINT.values();
761
762        // 4. Convert D50-adapted XYZ to Lab.
763        let ColorComponents(f0, f1, f2) = adapted.map(|v| {
764            if v > Self::EPSILON {
765                v.cbrt()
766            } else {
767                (Self::KAPPA * v + 16.0) / 116.0
768            }
769        });
770
771        let lightness = 116.0 * f1 - 16.0;
772        let a = 500.0 * (f0 - f1);
773        let b = 200.0 * (f1 - f2);
774
775        ColorComponents(lightness, a, b)
776    }
777
778    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
779        // No need for conversion.
780        from.clone()
781    }
782}
783
784/// The Lch color space.
785/// https://drafts.csswg.org/css-color-4/#specifying-lab-lch
786pub struct Lch;
787
788impl ColorSpaceConversion for Lch {
789    const WHITE_POINT: WhitePoint = Lab::WHITE_POINT;
790
791    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
792        // No need for conversion.
793        from.clone()
794    }
795
796    fn to_xyz(from: &ColorComponents) -> ColorComponents {
797        // Convert LCH to Lab first.
798        let lab = polar_to_orthogonal(from);
799
800        // Then convert the Lab to XYZ.
801        Lab::to_xyz(&lab)
802    }
803
804    fn from_xyz(from: &ColorComponents) -> ColorComponents {
805        // First convert the XYZ to LAB.
806        let lab = Lab::from_xyz(&from);
807
808        // Then convert the Lab to LCH.
809        orthogonal_to_polar(&lab, epsilon_for_range(0.0, 100.0))
810    }
811
812    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
813        // No need for conversion.
814        from.clone()
815    }
816}
817
818/// The Oklab color space.
819/// https://drafts.csswg.org/css-color-4/#specifying-oklab-oklch
820pub struct Oklab;
821
822impl Oklab {
823    #[rustfmt::skip]
824    const XYZ_TO_LMS: Transform = Transform::new(
825         0.8190224432164319,  0.0329836671980271,  0.048177199566046255, 0.0,
826         0.3619062562801221,  0.9292868468965546,  0.26423952494422764,  0.0,
827        -0.12887378261216414, 0.03614466816999844, 0.6335478258136937,   0.0,
828         0.0,                 0.0,                 0.0,                  1.0,
829    );
830
831    #[rustfmt::skip]
832    const LMS_TO_OKLAB: Transform = Transform::new(
833         0.2104542553,  1.9779984951,  0.0259040371, 0.0,
834         0.7936177850, -2.4285922050,  0.7827717662, 0.0,
835        -0.0040720468,  0.4505937099, -0.8086757660, 0.0,
836         0.0,           0.0,           0.0,          1.0,
837    );
838
839    #[rustfmt::skip]
840    const LMS_TO_XYZ: Transform = Transform::new(
841         1.2268798733741557,  -0.04057576262431372, -0.07637294974672142, 0.0,
842        -0.5578149965554813,   1.1122868293970594,  -0.4214933239627914,  0.0,
843         0.28139105017721583, -0.07171106666151701,  1.5869240244272418,  0.0,
844         0.0,                  0.0,                  0.0,                 1.0,
845    );
846
847    #[rustfmt::skip]
848    const OKLAB_TO_LMS: Transform = Transform::new(
849        0.99999999845051981432,  1.0000000088817607767,    1.0000000546724109177,   0.0,
850        0.39633779217376785678, -0.1055613423236563494,   -0.089484182094965759684, 0.0,
851        0.21580375806075880339, -0.063854174771705903402, -1.2914855378640917399,   0.0,
852        0.0,                     0.0,                      0.0,                     1.0,
853    );
854}
855
856impl ColorSpaceConversion for Oklab {
857    const WHITE_POINT: WhitePoint = WhitePoint::D65;
858
859    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
860        // No need for conversion.
861        from.clone()
862    }
863
864    fn to_xyz(from: &ColorComponents) -> ColorComponents {
865        let lms = transform(&from, &Self::OKLAB_TO_LMS);
866        let lms = lms.map(|v| v * v * v);
867        transform(&lms, &Self::LMS_TO_XYZ)
868    }
869
870    fn from_xyz(from: &ColorComponents) -> ColorComponents {
871        let lms = transform(&from, &Self::XYZ_TO_LMS);
872        let lms = lms.map(|v| v.cbrt());
873        transform(&lms, &Self::LMS_TO_OKLAB)
874    }
875
876    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
877        // No need for conversion.
878        from.clone()
879    }
880}
881
882/// The Oklch color space.
883/// https://drafts.csswg.org/css-color-4/#specifying-oklab-oklch
884pub struct Oklch;
885
886impl ColorSpaceConversion for Oklch {
887    const WHITE_POINT: WhitePoint = Oklab::WHITE_POINT;
888
889    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
890        // No need for conversion.
891        from.clone()
892    }
893
894    fn to_xyz(from: &ColorComponents) -> ColorComponents {
895        // First convert OkLCH to Oklab.
896        let oklab = polar_to_orthogonal(from);
897
898        // Then convert Oklab to XYZ.
899        Oklab::to_xyz(&oklab)
900    }
901
902    fn from_xyz(from: &ColorComponents) -> ColorComponents {
903        // First convert XYZ to Oklab.
904        let lab = Oklab::from_xyz(&from);
905
906        // Then convert Oklab to OkLCH.
907        orthogonal_to_polar(&lab, epsilon_for_range(0.0, 1.0))
908    }
909
910    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
911        // No need for conversion.
912        from.clone()
913    }
914}