Skip to main content

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;
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).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).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
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
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 Display-P3-linear color space. This is basically display-p3 without gamma encoding.
491pub struct DisplayP3Linear;
492impl ColorSpaceConversion for DisplayP3Linear {
493    const WHITE_POINT: WhitePoint = DisplayP3::WHITE_POINT;
494
495    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
496        *from
497    }
498
499    fn to_xyz(from: &ColorComponents) -> ColorComponents {
500        DisplayP3::to_xyz(from)
501    }
502
503    fn from_xyz(from: &ColorComponents) -> ColorComponents {
504        DisplayP3::from_xyz(from)
505    }
506
507    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
508        *from
509    }
510}
511
512/// The a98-rgb color space.
513/// https://drafts.csswg.org/css-color-4/#predefined-a98-rgb
514pub struct A98Rgb;
515
516impl A98Rgb {
517    #[rustfmt::skip]
518    const TO_XYZ: Transform = Transform::new(
519        0.5766690429101308,  0.29734497525053616, 0.027031361386412378, 0.0,
520        0.18555823790654627, 0.627363566255466,   0.07068885253582714,  0.0,
521        0.18822864623499472, 0.07529145849399789, 0.9913375368376389,   0.0,
522        0.0,                 0.0,                 0.0,                  1.0,
523    );
524
525    #[rustfmt::skip]
526    const FROM_XYZ: Transform = Transform::new(
527         2.041587903810746,  -0.9692436362808798,   0.013444280632031024, 0.0,
528        -0.5650069742788596,  1.8759675015077206,  -0.11836239223101824,  0.0,
529        -0.3447313507783295,  0.04155505740717561,  1.0151749943912054,   0.0,
530         0.0,                 0.0,                  0.0,                  1.0,
531    );
532}
533
534impl ColorSpaceConversion for A98Rgb {
535    const WHITE_POINT: WhitePoint = WhitePoint::D65;
536
537    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
538        (*from).map(|v| v.signum() * v.abs().powf(2.19921875))
539    }
540
541    fn to_xyz(from: &ColorComponents) -> ColorComponents {
542        transform(from, &Self::TO_XYZ)
543    }
544
545    fn from_xyz(from: &ColorComponents) -> ColorComponents {
546        transform(from, &Self::FROM_XYZ)
547    }
548
549    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
550        (*from).map(|v| v.signum() * v.abs().powf(0.4547069271758437))
551    }
552}
553
554/// The ProPhoto RGB color space.
555/// https://drafts.csswg.org/css-color-4/#predefined-prophoto-rgb
556pub struct ProphotoRgb;
557
558impl ProphotoRgb {
559    #[rustfmt::skip]
560    const TO_XYZ: Transform = Transform::new(
561        0.7977604896723027,  0.2880711282292934,     0.0,                0.0,
562        0.13518583717574031, 0.7118432178101014,     0.0,                0.0,
563        0.0313493495815248,  0.00008565396060525902, 0.8251046025104601, 0.0,
564        0.0,                 0.0,                    0.0,                1.0,
565    );
566
567    #[rustfmt::skip]
568    const FROM_XYZ: Transform = Transform::new(
569         1.3457989731028281,  -0.5446224939028347,  0.0,                0.0,
570        -0.25558010007997534,  1.5082327413132781,  0.0,                0.0,
571        -0.05110628506753401,  0.02053603239147973, 1.2119675456389454, 0.0,
572         0.0,                  0.0,                 0.0,                1.0,
573    );
574}
575
576impl ColorSpaceConversion for ProphotoRgb {
577    const WHITE_POINT: WhitePoint = WhitePoint::D50;
578
579    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
580        (*from).map(|value| {
581            const ET2: f32 = 16.0 / 512.0;
582
583            let abs = value.abs();
584
585            if abs <= ET2 {
586                value / 16.0
587            } else {
588                value.signum() * abs.powf(1.8)
589            }
590        })
591    }
592
593    fn to_xyz(from: &ColorComponents) -> ColorComponents {
594        transform(from, &Self::TO_XYZ)
595    }
596
597    fn from_xyz(from: &ColorComponents) -> ColorComponents {
598        transform(from, &Self::FROM_XYZ)
599    }
600
601    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
602        const ET: f32 = 1.0 / 512.0;
603
604        (*from).map(|v| {
605            let abs = v.abs();
606            if abs >= ET {
607                v.signum() * abs.powf(1.0 / 1.8)
608            } else {
609                16.0 * v
610            }
611        })
612    }
613}
614
615/// The Rec.2020 color space.
616/// https://drafts.csswg.org/css-color-4/#predefined-rec2020
617pub struct Rec2020;
618
619impl Rec2020 {
620    const ALPHA: f32 = 1.09929682680944;
621    const BETA: f32 = 0.018053968510807;
622
623    #[rustfmt::skip]
624    const TO_XYZ: Transform = Transform::new(
625        0.6369580483012913,  0.26270021201126703,  0.0,                  0.0,
626        0.14461690358620838, 0.677998071518871,    0.028072693049087508, 0.0,
627        0.16888097516417205, 0.059301716469861945, 1.0609850577107909,   0.0,
628        0.0,                 0.0,                  0.0,                  1.0,
629    );
630
631    #[rustfmt::skip]
632    const FROM_XYZ: Transform = Transform::new(
633         1.7166511879712676, -0.666684351832489,    0.017639857445310915, 0.0,
634        -0.3556707837763924,  1.616481236634939,   -0.042770613257808655, 0.0,
635        -0.2533662813736598,  0.01576854581391113,  0.942103121235474,    0.0,
636         0.0,                 0.0,                  0.0,                  1.0,
637    );
638}
639
640impl ColorSpaceConversion for Rec2020 {
641    const WHITE_POINT: WhitePoint = WhitePoint::D65;
642
643    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
644        (*from).map(|value| {
645            let abs = value.abs();
646
647            if abs < Self::BETA * 4.5 {
648                value / 4.5
649            } else {
650                value.signum() * ((abs + Self::ALPHA - 1.0) / Self::ALPHA).powf(1.0 / 0.45)
651            }
652        })
653    }
654
655    fn to_xyz(from: &ColorComponents) -> ColorComponents {
656        transform(from, &Self::TO_XYZ)
657    }
658
659    fn from_xyz(from: &ColorComponents) -> ColorComponents {
660        transform(from, &Self::FROM_XYZ)
661    }
662
663    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
664        (*from).map(|v| {
665            let abs = v.abs();
666
667            if abs > Self::BETA {
668                v.signum() * (Self::ALPHA * abs.powf(0.45) - (Self::ALPHA - 1.0))
669            } else {
670                4.5 * v
671            }
672        })
673    }
674}
675
676/// A color in the XYZ coordinate space with a D50 white reference.
677/// https://drafts.csswg.org/css-color-4/#predefined-xyz
678pub struct XyzD50;
679
680impl ColorSpaceConversion for XyzD50 {
681    const WHITE_POINT: WhitePoint = WhitePoint::D50;
682
683    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
684        *from
685    }
686
687    fn to_xyz(from: &ColorComponents) -> ColorComponents {
688        *from
689    }
690
691    fn from_xyz(from: &ColorComponents) -> ColorComponents {
692        *from
693    }
694
695    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
696        *from
697    }
698}
699
700/// A color in the XYZ coordinate space with a D65 white reference.
701/// https://drafts.csswg.org/css-color-4/#predefined-xyz
702pub struct XyzD65;
703
704impl ColorSpaceConversion for XyzD65 {
705    const WHITE_POINT: WhitePoint = WhitePoint::D65;
706
707    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
708        *from
709    }
710
711    fn to_xyz(from: &ColorComponents) -> ColorComponents {
712        *from
713    }
714
715    fn from_xyz(from: &ColorComponents) -> ColorComponents {
716        *from
717    }
718
719    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
720        *from
721    }
722}
723
724/// The Lab color space.
725/// https://drafts.csswg.org/css-color-4/#specifying-lab-lch
726pub struct Lab;
727
728impl Lab {
729    const KAPPA: f32 = 24389.0 / 27.0;
730    const EPSILON: f32 = 216.0 / 24389.0;
731}
732
733impl ColorSpaceConversion for Lab {
734    const WHITE_POINT: WhitePoint = WhitePoint::D50;
735
736    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
737        // No need for conversion.
738        *from
739    }
740
741    /// Convert a CIELAB color to XYZ as specified in [1] and [2].
742    ///
743    /// [1]: https://drafts.csswg.org/css-color/#lab-to-predefined
744    /// [2]: https://drafts.csswg.org/css-color/#color-conversion-code
745    fn to_xyz(from: &ColorComponents) -> ColorComponents {
746        let ColorComponents(lightness, a, b) = *from;
747
748        let f1 = (lightness + 16.0) / 116.0;
749        let f0 = f1 + a / 500.0;
750        let f2 = f1 - b / 200.0;
751
752        let f0_cubed = f0 * f0 * f0;
753        let x = if f0_cubed > Self::EPSILON {
754            f0_cubed
755        } else {
756            (116.0 * f0 - 16.0) / Self::KAPPA
757        };
758
759        let y = if lightness > Self::KAPPA * Self::EPSILON {
760            let v = (lightness + 16.0) / 116.0;
761            v * v * v
762        } else {
763            lightness / Self::KAPPA
764        };
765
766        let f2_cubed = f2 * f2 * f2;
767        let z = if f2_cubed > Self::EPSILON {
768            f2_cubed
769        } else {
770            (116.0 * f2 - 16.0) / Self::KAPPA
771        };
772
773        ColorComponents(x, y, z) * Self::WHITE_POINT.values()
774    }
775
776    /// Convert an XYZ color to LAB as specified in [1] and [2].
777    ///
778    /// [1]: https://drafts.csswg.org/css-color/#rgb-to-lab
779    /// [2]: https://drafts.csswg.org/css-color/#color-conversion-code
780    fn from_xyz(from: &ColorComponents) -> ColorComponents {
781        let adapted = *from / Self::WHITE_POINT.values();
782
783        // 4. Convert D50-adapted XYZ to Lab.
784        let ColorComponents(f0, f1, f2) = adapted.map(|v| {
785            if v > Self::EPSILON {
786                v.cbrt()
787            } else {
788                (Self::KAPPA * v + 16.0) / 116.0
789            }
790        });
791
792        let lightness = 116.0 * f1 - 16.0;
793        let a = 500.0 * (f0 - f1);
794        let b = 200.0 * (f1 - f2);
795
796        ColorComponents(lightness, a, b)
797    }
798
799    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
800        // No need for conversion.
801        *from
802    }
803}
804
805/// The Lch color space.
806/// https://drafts.csswg.org/css-color-4/#specifying-lab-lch
807pub struct Lch;
808
809impl ColorSpaceConversion for Lch {
810    const WHITE_POINT: WhitePoint = Lab::WHITE_POINT;
811
812    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
813        // No need for conversion.
814        *from
815    }
816
817    fn to_xyz(from: &ColorComponents) -> ColorComponents {
818        // Convert LCH to Lab first.
819        let lab = polar_to_orthogonal(from);
820
821        // Then convert the Lab to XYZ.
822        Lab::to_xyz(&lab)
823    }
824
825    fn from_xyz(from: &ColorComponents) -> ColorComponents {
826        // First convert the XYZ to LAB.
827        let lab = Lab::from_xyz(from);
828
829        // Then convert the Lab to LCH.
830        orthogonal_to_polar(&lab, epsilon_for_range(0.0, 100.0))
831    }
832
833    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
834        // No need for conversion.
835        *from
836    }
837}
838
839/// The Oklab color space.
840/// https://drafts.csswg.org/css-color-4/#specifying-oklab-oklch
841pub struct Oklab;
842
843impl Oklab {
844    #[rustfmt::skip]
845    const XYZ_TO_LMS: Transform = Transform::new(
846         0.8190224432164319,  0.0329836671980271,  0.048177199566046255, 0.0,
847         0.3619062562801221,  0.9292868468965546,  0.26423952494422764,  0.0,
848        -0.12887378261216414, 0.03614466816999844, 0.6335478258136937,   0.0,
849         0.0,                 0.0,                 0.0,                  1.0,
850    );
851
852    #[rustfmt::skip]
853    const LMS_TO_OKLAB: Transform = Transform::new(
854         0.2104542553,  1.9779984951,  0.0259040371, 0.0,
855         0.7936177850, -2.4285922050,  0.7827717662, 0.0,
856        -0.0040720468,  0.4505937099, -0.8086757660, 0.0,
857         0.0,           0.0,           0.0,          1.0,
858    );
859
860    #[rustfmt::skip]
861    const LMS_TO_XYZ: Transform = Transform::new(
862         1.2268798733741557,  -0.04057576262431372, -0.07637294974672142, 0.0,
863        -0.5578149965554813,   1.1122868293970594,  -0.4214933239627914,  0.0,
864         0.28139105017721583, -0.07171106666151701,  1.5869240244272418,  0.0,
865         0.0,                  0.0,                  0.0,                 1.0,
866    );
867
868    #[rustfmt::skip]
869    const OKLAB_TO_LMS: Transform = Transform::new(
870        0.99999999845051981432,  1.0000000088817607767,    1.0000000546724109177,   0.0,
871        0.39633779217376785678, -0.1055613423236563494,   -0.089484182094965759684, 0.0,
872        0.21580375806075880339, -0.063854174771705903402, -1.2914855378640917399,   0.0,
873        0.0,                     0.0,                      0.0,                     1.0,
874    );
875}
876
877impl ColorSpaceConversion for Oklab {
878    const WHITE_POINT: WhitePoint = WhitePoint::D65;
879
880    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
881        // No need for conversion.
882        *from
883    }
884
885    fn to_xyz(from: &ColorComponents) -> ColorComponents {
886        let lms = transform(from, &Self::OKLAB_TO_LMS);
887        let lms = lms.map(|v| v * v * v);
888        transform(&lms, &Self::LMS_TO_XYZ)
889    }
890
891    fn from_xyz(from: &ColorComponents) -> ColorComponents {
892        let lms = transform(from, &Self::XYZ_TO_LMS);
893        let lms = lms.map(|v| v.cbrt());
894        transform(&lms, &Self::LMS_TO_OKLAB)
895    }
896
897    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
898        // No need for conversion.
899        *from
900    }
901}
902
903/// The Oklch color space.
904/// https://drafts.csswg.org/css-color-4/#specifying-oklab-oklch
905pub struct Oklch;
906
907impl ColorSpaceConversion for Oklch {
908    const WHITE_POINT: WhitePoint = Oklab::WHITE_POINT;
909
910    fn to_linear_light(from: &ColorComponents) -> ColorComponents {
911        // No need for conversion.
912        *from
913    }
914
915    fn to_xyz(from: &ColorComponents) -> ColorComponents {
916        // First convert OkLCH to Oklab.
917        let oklab = polar_to_orthogonal(from);
918
919        // Then convert Oklab to XYZ.
920        Oklab::to_xyz(&oklab)
921    }
922
923    fn from_xyz(from: &ColorComponents) -> ColorComponents {
924        // First convert XYZ to Oklab.
925        let lab = Oklab::from_xyz(from);
926
927        // Then convert Oklab to OkLCH.
928        orthogonal_to_polar(&lab, epsilon_for_range(0.0, 1.0))
929    }
930
931    fn to_gamma_encoded(from: &ColorComponents) -> ColorComponents {
932        // No need for conversion.
933        *from
934    }
935}