1use crate::{Lab, 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 fmt::Display for Lch {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 let Self { lightness, chroma, hue, alpha } = self;
32 write!(f, "lch({}% {} {}", round_dp(*lightness, 2), round_dp(*chroma, 5), round_dp(*hue, 2))?;
33 if *alpha < 100.0 {
34 write!(f, " / {}", round_dp(*alpha as f64, 2))?;
35 }
36 write!(f, ")")
37 }
38}
39
40impl From<Lab> for Lch {
41 fn from(value: Lab) -> Self {
42 let Lab { lightness, a, b, alpha } = value;
43 let chroma = (a * a + b * b).sqrt();
44 let hue = b.atan2(a).to_degrees();
45 let hue = if hue < 0.0 { hue + 360.0 } else { hue }; Lch::new(lightness, chroma, hue, alpha)
47 }
48}
49
50impl From<Lch> for Lab {
51 fn from(value: Lch) -> Self {
52 let Lch { lightness, chroma, hue, alpha } = value;
53 let h_rad = hue.to_radians();
54 Lab::new(lightness, chroma * h_rad.cos(), chroma * h_rad.sin(), alpha)
55 }
56}