chromashift/
hsb.rs

1use crate::Srgb;
2use core::fmt;
3
4/// An colour represented as Hue, Saturation, and Value 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/// - Value / 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 Hsv {
12	pub hue: f32,
13	pub saturation: f32,
14	pub value: f32,
15	pub alpha: f32,
16}
17
18impl Hsv {
19	pub fn new(hue: f32, saturation: f32, value: f32, alpha: f32) -> Self {
20		Self {
21			hue: hue.rem_euclid(360.0),
22			saturation: saturation.clamp(0.0, 100.0),
23			value: value.clamp(0.0, 100.0),
24			alpha: alpha.clamp(0.0, 100.0),
25		}
26	}
27}
28
29impl fmt::Display for Hsv {
30	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31		let Self { hue, saturation, value, alpha } = self;
32		write!(f, "hsv({hue} {saturation} {value}")?;
33		if *alpha < 100.0 {
34			write!(f, " / {alpha}")?;
35		}
36		write!(f, ")")
37	}
38}
39
40impl From<Srgb> for Hsv {
41	fn from(value: Srgb) -> Self {
42		let r = value.red as f32 / 255.0;
43		let g = value.green as f32 / 255.0;
44		let b = value.blue as f32 / 255.0;
45		let max = r.max(g).max(b);
46		let min = r.min(g).min(b);
47		let delta = max - min;
48		let v = max;
49		let saturation = if max == 0.0 { 0.0 } else { delta / max };
50		let hue = if delta == 0.0 {
51			0.0
52		} else if max == r {
53			60.0 * (((g - b) / delta) % 6.0)
54		} else if max == g {
55			60.0 * ((b - r) / delta + 2.0)
56		} else {
57			60.0 * ((r - g) / delta + 4.0)
58		};
59		let hue = if hue < 0.0 { hue + 360.0 } else { hue };
60		Hsv::new(hue, saturation * 100.0, v * 100.0, value.alpha)
61	}
62}
63
64impl From<Hsv> for Srgb {
65	fn from(value: Hsv) -> Self {
66		let h = value.hue / 60.0;
67		let s = value.saturation / 100.0;
68		let v = value.value / 100.0;
69		let c = v * s;
70		let x = c * (1.0 - (h % 2.0 - 1.0).abs());
71		let m = v - c;
72		let (r_prime, g_prime, b_prime) = if h < 1.0 {
73			(c, x, 0.0)
74		} else if h < 2.0 {
75			(x, c, 0.0)
76		} else if h < 3.0 {
77			(0.0, c, x)
78		} else if h < 4.0 {
79			(0.0, x, c)
80		} else if h < 5.0 {
81			(x, 0.0, c)
82		} else {
83			(c, 0.0, x)
84		};
85		Srgb::new(
86			((r_prime + m) * 255.0).clamp(0.0, 255.0).round() as u8,
87			((g_prime + m) * 255.0).clamp(0.0, 255.0).round() as u8,
88			((b_prime + m) * 255.0).clamp(0.0, 255.0).round() as u8,
89			value.alpha,
90		)
91	}
92}