pushrod/color.rs
1// Color
2// Pushrod
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! # Color
17//!
18//! Pushrod's own color and font style types. `Widget`s and the [`Painter`](crate::paint::Painter)
19//! use these instead of `SDL2` types, so a widget library never compiles against a specific
20//! `SDL2` version. A backend converts them at its boundary.
21
22/// A color with red, green, blue, and alpha channels, each `0..=255`.
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
24pub struct Color {
25 /// Red channel.
26 pub r: u8,
27 /// Green channel.
28 pub g: u8,
29 /// Blue channel.
30 pub b: u8,
31 /// Alpha channel (`255` is opaque).
32 pub a: u8,
33}
34
35#[allow(non_snake_case)]
36impl Color {
37 /// Opaque black.
38 pub const BLACK: Color = Color::RGB(0, 0, 0);
39 /// Opaque white.
40 pub const WHITE: Color = Color::RGB(255, 255, 255);
41 /// Opaque gray.
42 pub const GRAY: Color = Color::RGB(128, 128, 128);
43 /// Opaque red.
44 pub const RED: Color = Color::RGB(255, 0, 0);
45 /// Opaque green.
46 pub const GREEN: Color = Color::RGB(0, 255, 0);
47 /// Opaque blue.
48 pub const BLUE: Color = Color::RGB(0, 0, 255);
49 /// Opaque yellow.
50 pub const YELLOW: Color = Color::RGB(255, 255, 0);
51 /// Opaque cyan.
52 pub const CYAN: Color = Color::RGB(0, 255, 255);
53 /// Opaque magenta.
54 pub const MAGENTA: Color = Color::RGB(255, 0, 255);
55
56 /// Creates an opaque color.
57 ///
58 /// # Arguments
59 ///
60 /// * `r` - Red channel.
61 /// * `g` - Green channel.
62 /// * `b` - Blue channel.
63 ///
64 /// # Returns
65 ///
66 /// The new `Color`, with alpha `255`.
67 pub const fn RGB(r: u8, g: u8, b: u8) -> Self {
68 Self { r, g, b, a: 255 }
69 }
70
71 /// Creates a color with an alpha channel.
72 ///
73 /// # Arguments
74 ///
75 /// * `r` - Red channel.
76 /// * `g` - Green channel.
77 /// * `b` - Blue channel.
78 /// * `a` - Alpha channel (`255` is opaque).
79 ///
80 /// # Returns
81 ///
82 /// The new `Color`.
83 pub const fn RGBA(r: u8, g: u8, b: u8, a: u8) -> Self {
84 Self { r, g, b, a }
85 }
86}
87
88/// Font style flags, which can be combined with `|`.
89#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
90pub struct FontStyle(u8);
91
92impl FontStyle {
93 /// Plain text.
94 pub const NORMAL: FontStyle = FontStyle(0);
95 /// Bold text.
96 pub const BOLD: FontStyle = FontStyle(1);
97 /// Italic text.
98 pub const ITALIC: FontStyle = FontStyle(2);
99 /// Underlined text.
100 pub const UNDERLINE: FontStyle = FontStyle(4);
101 /// Struck-through text.
102 pub const STRIKETHROUGH: FontStyle = FontStyle(8);
103
104 /// Returns the raw flag bits (bold `1`, italic `2`, underline `4`, strikethrough `8`).
105 ///
106 /// # Returns
107 ///
108 /// The bits.
109 pub const fn bits(self) -> u8 {
110 self.0
111 }
112
113 /// Returns whether every flag in `other` is set.
114 ///
115 /// # Arguments
116 ///
117 /// * `other` - The flags to look for.
118 ///
119 /// # Returns
120 ///
121 /// `true` if all of `other`'s flags are set in this style.
122 pub const fn contains(self, other: FontStyle) -> bool {
123 self.0 & other.0 == other.0
124 }
125}
126
127impl std::ops::BitOr for FontStyle {
128 type Output = FontStyle;
129
130 /// Combines two styles.
131 ///
132 /// # Arguments
133 ///
134 /// * `rhs` - The style to add.
135 ///
136 /// # Returns
137 ///
138 /// A style with the flags of both.
139 fn bitor(self, rhs: FontStyle) -> FontStyle {
140 FontStyle(self.0 | rhs.0)
141 }
142}
143
144#[cfg(test)]
145mod tests {
146 use super::*;
147
148 #[test]
149 fn rgb_is_opaque() {
150 assert_eq!(Color::RGB(1, 2, 3), Color::RGBA(1, 2, 3, 255));
151 }
152
153 #[test]
154 fn named_colors() {
155 assert_eq!(Color::BLACK, Color::RGBA(0, 0, 0, 255));
156 assert_eq!(Color::WHITE.r, 255);
157 }
158
159 #[test]
160 fn font_styles_combine() {
161 let style = FontStyle::BOLD | FontStyle::ITALIC;
162 assert!(style.contains(FontStyle::BOLD));
163 assert!(style.contains(FontStyle::ITALIC));
164 assert!(!style.contains(FontStyle::UNDERLINE));
165 assert_eq!(style.bits(), 3);
166 assert!(FontStyle::NORMAL.contains(FontStyle::NORMAL));
167 }
168}