graphics/
colored.rs

1use crate::{
2    math::hsv,
3    radians::Radians,
4    types::{Color, ColorComponent},
5    Ellipse, Line, Rectangle,
6};
7
8/// Implemented by contexts that contains color.
9pub trait Colored: Sized {
10    /// Multiplies with red, green, blue and alpha values.
11    fn mul_rgba(
12        self,
13        r: ColorComponent,
14        g: ColorComponent,
15        b: ColorComponent,
16        a: ColorComponent,
17    ) -> Self;
18
19    /// Mixes the current color with white.
20    ///
21    /// 0 is black and 1 is white.
22    #[inline(always)]
23    fn tint(self, f: ColorComponent) -> Self {
24        self.mul_rgba(f, f, f, 1.0)
25    }
26
27    /// Mixes the current color with black.
28    ///
29    /// 0 is white and 1 is black.
30    #[inline(always)]
31    fn shade(self, f: ColorComponent) -> Self {
32        let f = 1.0 - f;
33        self.mul_rgba(f, f, f, 1.0)
34    }
35
36    /// Rotates hue by degrees.
37    #[inline(always)]
38    fn hue_deg(self, angle: ColorComponent) -> Self {
39        let pi: ColorComponent = Radians::_180();
40        self.hue_rad(angle * pi / 180.0)
41    }
42
43    /// Rotates hue by radians.
44    fn hue_rad(self, angle: ColorComponent) -> Self;
45}
46
47impl Colored for Color {
48    #[inline(always)]
49    fn mul_rgba(
50        self,
51        r: ColorComponent,
52        g: ColorComponent,
53        b: ColorComponent,
54        a: ColorComponent,
55    ) -> Self {
56        [self[0] * r, self[1] * g, self[2] * b, self[3] * a]
57    }
58
59    #[inline(always)]
60    fn hue_rad(self, angle: ColorComponent) -> Self {
61        hsv(self, angle, 1.0, 1.0)
62    }
63}
64
65impl Colored for Line {
66    #[inline(always)]
67    fn mul_rgba(
68        mut self,
69        r: ColorComponent,
70        g: ColorComponent,
71        b: ColorComponent,
72        a: ColorComponent,
73    ) -> Self {
74        self.color = self.color.mul_rgba(r, g, b, a);
75        self
76    }
77
78    #[inline(always)]
79    fn hue_rad(mut self, angle: ColorComponent) -> Self {
80        self.color = self.color.hue_rad(angle);
81        self
82    }
83}
84
85impl Colored for Ellipse {
86    #[inline(always)]
87    fn mul_rgba(
88        mut self,
89        r: ColorComponent,
90        g: ColorComponent,
91        b: ColorComponent,
92        a: ColorComponent,
93    ) -> Self {
94        self.color = self.color.mul_rgba(r, g, b, a);
95        self
96    }
97
98    #[inline(always)]
99    fn hue_rad(mut self, angle: ColorComponent) -> Self {
100        self.color = self.color.hue_rad(angle);
101        self
102    }
103}
104
105impl Colored for Rectangle {
106    #[inline(always)]
107    fn mul_rgba(
108        mut self,
109        r: ColorComponent,
110        g: ColorComponent,
111        b: ColorComponent,
112        a: ColorComponent,
113    ) -> Self {
114        self.color = self.color.mul_rgba(r, g, b, a);
115        self
116    }
117
118    #[inline(always)]
119    fn hue_rad(mut self, angle: ColorComponent) -> Self {
120        self.color = self.color.hue_rad(angle);
121        self
122    }
123}