palette/okhsl/
alpha.rs

1use crate::hues::OklabHue;
2use crate::{angle::FromAngle, stimulus::FromStimulus, Alpha};
3
4use super::Okhsl;
5
6/// Okhsl with an alpha component.
7pub type Okhsla<T = f32> = Alpha<Okhsl<T>, T>;
8
9///<span id="Okhsla"></span>[`Okhsla`](crate::Okhsla) implementations.
10impl<T, A> Alpha<Okhsl<T>, A> {
11    /// Create an `Okhsl` color with transparency.
12    pub fn new<H: Into<OklabHue<T>>>(hue: H, saturation: T, lightness: T, alpha: A) -> Self {
13        Alpha {
14            color: Okhsl::new(hue, saturation, lightness),
15            alpha,
16        }
17    }
18
19    /// Create an `Okhsla` color. This is the same as `Okhsla::new` without the
20    /// generic hue type. It's temporary until `const fn` supports traits.
21    pub const fn new_const(hue: OklabHue<T>, saturation: T, lightness: T, alpha: A) -> Self {
22        Alpha {
23            color: Okhsl::new_const(hue, saturation, lightness),
24            alpha,
25        }
26    }
27
28    /// Convert into another component type.
29    pub fn into_format<U, B>(self) -> Alpha<Okhsl<U>, B>
30    where
31        U: FromStimulus<T> + FromAngle<T>,
32        B: FromStimulus<A>,
33    {
34        Alpha {
35            color: self.color.into_format(),
36            alpha: B::from_stimulus(self.alpha),
37        }
38    }
39
40    /// Convert from another component type.
41    pub fn from_format<U, B>(color: Alpha<Okhsl<U>, B>) -> Self
42    where
43        T: FromStimulus<U> + FromAngle<U>,
44        A: FromStimulus<B>,
45    {
46        color.into_format()
47    }
48
49    /// Convert to a `(hue, saturation, lightness, alpha)` tuple.
50    pub fn into_components(self) -> (OklabHue<T>, T, T, A) {
51        (
52            self.color.hue,
53            self.color.saturation,
54            self.color.lightness,
55            self.alpha,
56        )
57    }
58
59    /// Convert from a `(hue, saturation, lightness, alpha)` tuple.
60    pub fn from_components<H: Into<OklabHue<T>>>(
61        (hue, saturation, lightness, alpha): (H, T, T, A),
62    ) -> Self {
63        Self::new(hue, saturation, lightness, alpha)
64    }
65}