optic_color/hsl.rs
1use crate::{RGBA, ToRgba};
2
3/// HSL color.
4///
5/// | Field | Range | Description |
6/// |-------|-------|-------------|
7/// | `h` | 0..360 | Hue angle (wraps at 360) |
8/// | `s` | 0..1 | Saturation |
9/// | `l` | 0..1 | Lightness |
10///
11/// # Why no arithmetic?
12///
13/// Same reasoning as [`HSV`]: hue wraparound makes naive componentwise
14/// operations incorrect. Convert to [`RGBA`], manipulate there, convert back.
15///
16/// See [`HSV`] for details and alternatives.
17///
18/// [`HSV`]: crate::HSV
19#[derive(Copy, Clone, Debug)]
20pub struct HSL {
21 pub h: f32,
22 pub s: f32,
23 pub l: f32,
24}
25
26impl HSL {
27 /// Construct an HSL color with clamping.
28 ///
29 /// Hue is clamped to 0..360, saturation and lightness to 0..1.
30 pub fn new(h: f32, s: f32, l: f32) -> Self {
31 HSL { h: h.clamp(0.0, 360.0), s: s.clamp(0.0, 1.0), l: l.clamp(0.0, 1.0) }
32 }
33
34 /// Convert to RGBA with a custom alpha.
35 ///
36 /// Equivalent to `self.to_rgba().with_alpha(alpha)`.
37 pub fn to_rgba_alpha(self, alpha: f32) -> RGBA {
38 self.to_rgba().with_alpha(alpha)
39 }
40}