nannou/draw/background.rs
1use crate::color::{self, IntoLinSrgba, Srgb, Srgba};
2use crate::draw::properties::ColorScalar;
3use crate::draw::Draw;
4
5/// A type used to update the background colour.
6pub struct Background<'a> {
7 draw: &'a Draw,
8}
9
10/// Begin coloring the background.
11pub fn new<'a>(draw: &'a Draw) -> Background<'a> {
12 Background { draw }
13}
14
15impl<'a> Background<'a> {
16 /// Clear the background with the given color.
17 ///
18 /// This method supports any color type that can be converted into RGBA.
19 ///
20 /// Colors that have no alpha channel will be given an opaque alpha channel value `1.0`.
21 pub fn color<C>(self, color: C) -> Self
22 where
23 C: IntoLinSrgba<ColorScalar>,
24 {
25 if let Ok(mut state) = self.draw.state.try_borrow_mut() {
26 state.background_color = Some(color.into_lin_srgba());
27 }
28 self
29 }
30
31 /// Specify the color via red, green and blue channels.
32 pub fn rgb(self, r: ColorScalar, g: ColorScalar, b: ColorScalar) -> Self {
33 self.color(Srgb::new(r, g, b))
34 }
35
36 /// Specify the color via red, green, blue and alpha channels.
37 pub fn rgba(self, r: ColorScalar, g: ColorScalar, b: ColorScalar, a: ColorScalar) -> Self {
38 self.color(Srgba::new(r, g, b, a))
39 }
40
41 /// Specify the color via hue, saturation and luminance.
42 ///
43 /// If you're looking for HSV or HSB, use the `hsv` method instead.
44 ///
45 /// The given hue expects a value between `0.0` and `1.0` where `0.0` is 0 degress and `1.0` is
46 /// 360 degrees (or 2 PI radians).
47 ///
48 /// See the [wikipedia entry](https://en.wikipedia.org/wiki/HSL_and_HSV) for more details on
49 /// this color space.
50 pub fn hsl(self, h: ColorScalar, s: ColorScalar, l: ColorScalar) -> Self {
51 let hue = h * 360.0;
52 self.color(color::Hsl::new(hue, s, l))
53 }
54
55 /// Specify the color via hue, saturation, luminance and an alpha channel.
56 ///
57 /// If you're looking for HSVA or HSBA, use the `hsva` method instead.
58 ///
59 /// The given hue expects a value between `0.0` and `1.0` where `0.0` is 0 degress and `1.0` is
60 /// 360 degrees (or 2 PI radians).
61 ///
62 /// See the [wikipedia entry](https://en.wikipedia.org/wiki/HSL_and_HSV) for more details on
63 /// this color space.
64 pub fn hsla(self, h: ColorScalar, s: ColorScalar, l: ColorScalar, a: ColorScalar) -> Self {
65 let hue = h * 360.0;
66 self.color(color::Hsla::new(hue, s, l, a))
67 }
68
69 /// Specify the color via hue, saturation and *value* (brightness).
70 ///
71 /// This is sometimes also known as "hsb".
72 ///
73 /// The given hue expects a value between `0.0` and `1.0` where `0.0` is 0 degress and `1.0` is
74 /// 360 degrees (or 2 PI radians).
75 ///
76 /// See the [wikipedia entry](https://en.wikipedia.org/wiki/HSL_and_HSV) for more details on
77 /// this color space.
78 pub fn hsv(self, h: ColorScalar, s: ColorScalar, v: ColorScalar) -> Self {
79 let hue = h * 360.0;
80 self.color(color::Hsv::new(hue, s, v))
81 }
82
83 /// Specify the color via hue, saturation, *value* (brightness) and an alpha channel.
84 ///
85 /// This is sometimes also known as "hsba".
86 ///
87 /// The given hue expects a value between `0.0` and `1.0` where `0.0` is 0 degress and `1.0` is
88 /// 360 degrees (or 2 PI radians).
89 ///
90 /// See the [wikipedia entry](https://en.wikipedia.org/wiki/HSL_and_HSV) for more details on
91 /// this color space.
92 pub fn hsva(self, h: ColorScalar, s: ColorScalar, v: ColorScalar, a: ColorScalar) -> Self {
93 let hue = h * 360.0;
94 self.color(color::Hsva::new(hue, s, v, a))
95 }
96}