chromashift/
lch.rs

1use crate::{Lab, ToAlpha, round_dp};
2use core::fmt;
3
4/// A cylindrical colour space representing within the CIE colour space.
5/// The components are:
6/// - Lightness / Luminance - a number between 0.0 and 100.0
7/// - Chroma - a number between 0.0 and 150.0
8/// - Hue - a number between 0.0 and 360.0
9/// - Alpha - a number between 0.0 and 100.0
10#[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 { lightness, chroma, hue: hue.rem_euclid(360.0), alpha: alpha.clamp(0.0, 100.0) }
21	}
22}
23
24impl ToAlpha for Lch {
25	fn to_alpha(&self) -> f32 {
26		self.alpha
27	}
28}
29
30impl fmt::Display for Lch {
31	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32		let Self { lightness, chroma, hue, alpha } = self;
33		write!(f, "lch({}% {} {}", round_dp(*lightness, 2), round_dp(*chroma, 5), round_dp(*hue, 2))?;
34		if *alpha < 100.0 {
35			write!(f, " / {}", round_dp(*alpha as f64, 2))?;
36		}
37		write!(f, ")")
38	}
39}
40
41impl From<Lab> for Lch {
42	fn from(value: Lab) -> Self {
43		let Lab { lightness, a, b, alpha } = value;
44		let chroma = (a * a + b * b).sqrt();
45		let hue = b.atan2(a).to_degrees();
46		let hue = if hue < 0.0 { hue + 360.0 } else { hue }; // Normalize to [0, 360)
47		Lch::new(lightness, chroma, hue, alpha)
48	}
49}
50
51impl From<Lch> for Lab {
52	fn from(value: Lch) -> Self {
53		let Lch { lightness, chroma, hue, alpha } = value;
54		let h_rad = hue.to_radians();
55		Lab::new(lightness, chroma * h_rad.cos(), chroma * h_rad.sin(), alpha)
56	}
57}