1use crate::{Lab, ToAlpha, round_dp};
2use core::fmt;
3
4#[derive(Debug, Clone, Copy, PartialEq)]
11pub struct Lch {
12 pub lightness: f64,
13 pub chroma: f64,
14 pub hue: f64,
15 pub alpha: f32,
16}
17
18impl Lch {
19 pub fn new(lightness: f64, chroma: f64, hue: f64, alpha: f32) -> Self {
20 Self {
21 lightness: lightness.clamp(0.0, 100.0),
22 chroma: chroma.clamp(0.0, 150.0),
23 hue: hue.rem_euclid(360.0),
24 alpha: alpha.clamp(0.0, 100.0),
25 }
26 }
27}
28
29impl ToAlpha for Lch {
30 fn to_alpha(&self) -> f32 {
31 self.alpha
32 }
33}
34
35impl fmt::Display for Lch {
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37 let Self { lightness, chroma, hue, alpha } = self;
38 write!(f, "lch({}% {} {}", round_dp(*lightness, 2), round_dp(*chroma, 5), round_dp(*hue, 2))?;
39 if *alpha < 100.0 {
40 write!(f, " / {}", round_dp(*alpha as f64, 2))?;
41 }
42 write!(f, ")")
43 }
44}
45
46impl From<Lab> for Lch {
47 fn from(value: Lab) -> Self {
48 let Lab { lightness, a, b, alpha } = value;
49 let chroma = (a * a + b * b).sqrt();
50 let hue = b.atan2(a).to_degrees();
51 let hue = if hue < 0.0 { hue + 360.0 } else { hue }; Lch::new(lightness, chroma, hue, alpha)
53 }
54}
55
56impl From<Lch> for Lab {
57 fn from(value: Lch) -> Self {
58 let Lch { lightness, chroma, hue, alpha } = value;
59 let h_rad = hue.to_radians();
60 Lab::new(lightness, chroma * h_rad.cos(), chroma * h_rad.sin(), alpha)
61 }
62}