Debian Codegraph @

src/okhsv/alpha.rs

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