palette/okhwb/
alpha.rs

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