chromashift/
oklch.rs

1use crate::{Oklab, round_dp};
2use core::fmt;
3
4/// A more adequate expression of LCH, in 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 Oklch {
12	pub lightness: f64,
13	pub chroma: f64,
14	pub hue: f64,
15	pub alpha: f32,
16}
17
18impl Oklch {
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 Oklch {
30	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31		let Self { lightness, chroma, hue, alpha } = self;
32		write!(f, "oklch({} {} {}", round_dp(*lightness, 2), round_dp(*chroma, 4), 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<Oklab> for Oklch {
41	fn from(value: Oklab) -> Self {
42		let Oklab { 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 };
46		Oklch::new(lightness, chroma, hue, alpha)
47	}
48}
49
50impl From<Oklch> for Oklab {
51	fn from(value: Oklch) -> Self {
52		let Oklch { lightness, chroma, hue, alpha } = value;
53		let hue_rad = hue.to_radians();
54		let a = chroma * hue_rad.cos();
55		let b = chroma * hue_rad.sin();
56		Oklab::new(lightness, a, b, alpha)
57	}
58}