chromashift/
oklch.rs

1use crate::{Oklab, ToAlpha, 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 { lightness, chroma, hue: hue.rem_euclid(360.0), alpha: alpha.clamp(0.0, 100.0) }
21	}
22}
23
24impl ToAlpha for Oklch {
25	fn to_alpha(&self) -> f32 {
26		self.alpha
27	}
28}
29
30impl fmt::Display for Oklch {
31	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32		let Self { lightness, chroma, hue, alpha } = self;
33		write!(f, "oklch({} {} {}", round_dp(*lightness, 2), round_dp(*chroma, 4), 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<Oklab> for Oklch {
42	fn from(value: Oklab) -> Self {
43		let Oklab { 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 };
47		Oklch::new(lightness, chroma, hue, alpha)
48	}
49}
50
51impl From<Oklch> for Oklab {
52	fn from(value: Oklch) -> Self {
53		let Oklch { lightness, chroma, hue, alpha } = value;
54		let hue_rad = hue.to_radians();
55		let a = chroma * hue_rad.cos();
56		let b = chroma * hue_rad.sin();
57		Oklab::new(lightness, a, b, alpha)
58	}
59}