nannou/draw/properties/
color.rs

1use crate::color::{self, Component, IntoLinSrgba, LinSrgba};
2use crate::math::num_traits::Float;
3
4/// A **Srgba** type with the default Scalar.
5pub type DefaultSrgba = color::Srgba<color::DefaultScalar>;
6
7/// A **LinSrgba** type with the default Scalar.
8pub type DefaultLinSrgba = color::LinSrgba<color::DefaultScalar>;
9
10/// Nodes that support setting colors.
11pub trait SetColor<S>: Sized
12where
13    S: Component,
14{
15    /// Provide a mutable reference to the RGBA field which can be used for setting colors.
16    fn rgba_mut(&mut self) -> &mut Option<LinSrgba<S>>;
17
18    /// Specify a color.
19    ///
20    /// This method supports any color type that can be converted into RGBA.
21    ///
22    /// Colors that have no alpha channel will be given an opaque alpha channel value `1.0`.
23    fn color<C>(mut self, color: C) -> Self
24    where
25        C: IntoLinSrgba<S>,
26    {
27        *self.rgba_mut() = Some(color.into_lin_srgba());
28        self
29    }
30
31    /// Specify the color via red, green and blue channels.
32    fn rgb<T>(self, r: T, g: T, b: T) -> Self
33    where
34        T: Component,
35        S: Float,
36    {
37        self.color(color::Srgb::new(r, g, b))
38    }
39
40    /// Specify the color via red, green and blue channels as bytes
41    fn rgb8(self, r: u8, g: u8, b: u8) -> Self
42    where
43        S: Float,
44    {
45        self.color(color::Srgb::<u8>::new(r, g, b))
46    }
47
48    /// Specify the color via red, green, blue and alpha channels.
49    fn rgba<T>(self, r: T, g: T, b: T, a: T) -> Self
50    where
51        T: Component,
52        S: Float,
53    {
54        self.color(color::Srgba::new(r, g, b, a))
55    }
56
57    /// Specify the color via red, green, blue and alpha channels as bytes
58    fn rgba8(self, r: u8, g: u8, b: u8, a: u8) -> Self
59    where
60        S: Float,
61    {
62        self.color(color::Srgba::<u8>::new(r, g, b, a))
63    }
64
65    /// Specify the color via hue, saturation and luminance.
66    ///
67    /// If you're looking for HSV or HSB, use the `hsv` method instead.
68    ///
69    /// The given hue expects a value between `0.0` and `1.0` where `0.0` is 0 degress and `1.0` is
70    /// 360 degrees (or 2 PI radians).
71    ///
72    /// See the [wikipedia entry](https://en.wikipedia.org/wiki/HSL_and_HSV) for more details on
73    /// this color space.
74    fn hsl(self, h: S, s: S, l: S) -> Self
75    where
76        S: Float + Into<color::RgbHue<S>>,
77    {
78        let hue = color::RgbHue::from_degrees(h * S::from(360.0).unwrap());
79        self.color(color::Hsl::new(hue, s, l))
80    }
81
82    /// Specify the color via hue, saturation, luminance and an alpha channel.
83    ///
84    /// If you're looking for HSVA or HSBA, use the `hsva` method instead.
85    ///
86    /// The given hue expects a value between `0.0` and `1.0` where `0.0` is 0 degress and `1.0` is
87    /// 360 degrees (or 2 PI radians).
88    ///
89    /// See the [wikipedia entry](https://en.wikipedia.org/wiki/HSL_and_HSV) for more details on
90    /// this color space.
91    fn hsla(self, h: S, s: S, l: S, a: S) -> Self
92    where
93        S: Float + Into<color::RgbHue<S>>,
94    {
95        let hue = color::RgbHue::from_degrees(h * S::from(360.0).unwrap());
96        self.color(color::Hsla::new(hue, s, l, a))
97    }
98
99    /// Specify the color via hue, saturation and *value* (brightness).
100    ///
101    /// This is sometimes also known as "hsb".
102    ///
103    /// The given hue expects a value between `0.0` and `1.0` where `0.0` is 0 degress and `1.0` is
104    /// 360 degrees (or 2 PI radians).
105    ///
106    /// See the [wikipedia entry](https://en.wikipedia.org/wiki/HSL_and_HSV) for more details on
107    /// this color space.
108    fn hsv(self, h: S, s: S, v: S) -> Self
109    where
110        S: Float,
111    {
112        let hue = color::RgbHue::from_degrees(h * S::from(360.0).unwrap());
113        self.color(color::Hsv::new(hue, s, v))
114    }
115
116    /// Specify the color via hue, saturation, *value* (brightness) and an alpha channel.
117    ///
118    /// This is sometimes also known as "hsba".
119    ///
120    /// The given hue expects a value between `0.0` and `1.0` where `0.0` is 0 degress and `1.0` is
121    /// 360 degrees (or 2 PI radians).
122    ///
123    /// See the [wikipedia entry](https://en.wikipedia.org/wiki/HSL_and_HSV) for more details on
124    /// this color space.
125    fn hsva(self, h: S, s: S, v: S, a: S) -> Self
126    where
127        S: Float,
128    {
129        let hue = color::RgbHue::from_degrees(h * S::from(360.0).unwrap());
130        self.color(color::Hsva::new(hue, s, v, a))
131    }
132
133    /// Specify the color as gray scale
134    ///
135    /// The given g expects a value between `0.0` and `1.0` where `0.0` is black and `1.0` is white
136    fn gray<T>(self, g: T) -> Self
137    where
138        T: Component,
139        S: Float,
140    {
141        self.color(color::Srgb::new(g, g, g))
142    }
143}
144
145impl<S> SetColor<S> for Option<LinSrgba<S>>
146where
147    S: Component,
148{
149    fn rgba_mut(&mut self) -> &mut Option<LinSrgba<S>> {
150        self
151    }
152}