chromashift/
hsl.rs

1use crate::{Srgb, ToAlpha, round_dp};
2use core::fmt;
3
4/// An colour represented as Hue, Saturation, and Lightness expressed in the sRGB colour space.
5/// The components are:
6/// - Hue - a number between 0.0 and 360.0
7/// - Saturation - a number between 0.0 and 100.0
8/// - Brightness - a number between 0.0 and 100.0
9/// - Alpha - a number between 0.0 and 100.0
10#[derive(Debug, Clone, Copy, PartialEq)]
11pub struct Hsl {
12	pub hue: f32,
13	pub saturation: f32,
14	pub lightness: f32,
15	pub alpha: f32,
16}
17
18impl Hsl {
19	pub fn new(hue: f32, saturation: f32, lightness: f32, alpha: f32) -> Self {
20		Self {
21			hue: hue.rem_euclid(360.0),
22			saturation: saturation.clamp(0.0, 100.0),
23			lightness: lightness.clamp(0.0, 100.0),
24			alpha: alpha.clamp(0.0, 100.0),
25		}
26	}
27}
28
29impl ToAlpha for Hsl {
30	fn to_alpha(&self) -> f32 {
31		self.alpha
32	}
33}
34
35impl fmt::Display for Hsl {
36	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37		let Self { hue, saturation, lightness, alpha } = self;
38		write!(
39			f,
40			"hsl({} {}% {}%",
41			round_dp(*hue as f64, 2),
42			round_dp(*saturation as f64, 2),
43			round_dp(*lightness as f64, 2)
44		)?;
45		if *alpha < 100.0 {
46			write!(f, " / {}", round_dp(*alpha as f64, 2))?;
47		}
48		write!(f, ")")
49	}
50}
51
52impl From<Srgb> for Hsl {
53	fn from(value: Srgb) -> Self {
54		let r = value.red as f32 / 255.0;
55		let g = value.green as f32 / 255.0;
56		let b = value.blue as f32 / 255.0;
57		let max = r.max(g).max(b);
58		let min = r.min(g).min(b);
59		let delta = max - min;
60		let lightness = (max + min) / 2.0;
61		let saturation = if delta == 0.0 { 0.0 } else { delta / (1.0 - (2.0 * lightness - 1.0).abs()) };
62		let hue = if delta == 0.0 {
63			0.0
64		} else if max == r {
65			60.0 * (((g - b) / delta) % 6.0)
66		} else if max == g {
67			60.0 * ((b - r) / delta + 2.0)
68		} else {
69			60.0 * ((r - g) / delta + 4.0)
70		};
71		let hue = if hue < 0.0 { hue + 360.0 } else { hue };
72		Hsl::new(hue, saturation * 100.0, lightness * 100.0, value.alpha)
73	}
74}
75
76impl From<Hsl> for Srgb {
77	fn from(value: Hsl) -> Self {
78		let h = value.hue / 60.0;
79		let s = value.saturation / 100.0;
80		let l = value.lightness / 100.0;
81		let c = (1.0 - (2.0 * l - 1.0).abs()) * s;
82		let x = c * (1.0 - (h % 2.0 - 1.0).abs());
83		let m = l - c / 2.0;
84		let (r_prime, g_prime, b_prime) = if h < 1.0 {
85			(c, x, 0.0)
86		} else if h < 2.0 {
87			(x, c, 0.0)
88		} else if h < 3.0 {
89			(0.0, c, x)
90		} else if h < 4.0 {
91			(0.0, x, c)
92		} else if h < 5.0 {
93			(x, 0.0, c)
94		} else {
95			(c, 0.0, x)
96		};
97		Srgb::new(
98			((r_prime + m) * 255.0).clamp(0.0, 255.0).round() as u8,
99			((g_prime + m) * 255.0).clamp(0.0, 255.0).round() as u8,
100			((b_prime + m) * 255.0).clamp(0.0, 255.0).round() as u8,
101			value.alpha,
102		)
103	}
104}