-
Notifications
You must be signed in to change notification settings - Fork 563
/
colour.rs
309 lines (294 loc) · 9.02 KB
/
colour.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
use std::default::Default;
use ::internal::prelude::*;
macro_rules! colour {
($(#[$attr:meta] $name:ident, $val:expr;)*) => {
impl Colour {
$(
#[$attr]
pub fn $name() -> Colour {
Colour::new($val)
}
)*
}
}
}
/// A utility struct to help with working with the basic representation of a
/// colour. This is particularly useful when working with a [`Role`]'s colour,
/// as the API works with an integer value instead of an RGB value.
///
/// Instances can be created by using the struct's associated functions. These
/// produce presets equivalent to those found in the official client's colour
/// picker.
///
/// # Examples
///
/// Passing in a role's colour, and then retrieving its green component
/// via [`get_g`]:
///
/// ```rust,ignore
/// use serenity::utils::Colour;
///
/// // assuming a `role` has already been bound
///
/// let colour = Colour::new(role.colour);
/// let green = colour.get_g();
///
/// println!("The green component is: {}", green);
/// ```
///
/// Creating an instance with the [`dark_teal`] presets:
///
/// ```rust
/// use serenity::utils::Colour;
///
/// let colour = Colour::dark_teal();
///
/// assert_eq!(colour.get_tuple(), (17, 128, 106));
/// ```
///
/// Colours can also be directly compared for equivilance:
///
/// ```rust
/// use serenity::utils::Colour;
///
/// let blitz_blue = Colour::blitz_blue();
/// let fooyoo = Colour::fooyoo();
/// let fooyoo2 = Colour::fooyoo();
/// assert!(blitz_blue != fooyoo);
/// assert_eq!(fooyoo, fooyoo2);
/// assert!(blitz_blue > fooyoo);
/// ```
///
/// [`Role`]: ../model/struct.Role.html
/// [`dark_teal`]: #method.dark_teal
/// [`get_g`]: #method.get_g
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Colour(pub u32);
impl Colour {
/// Generates a new Colour with the given integer value set.
///
/// # Examples
///
/// Create a new Colour, and then ensure that its inner value is equivalent
/// to a specific RGB value, retrieved via [`get_tuple`]:
///
/// ```rust
/// use serenity::utils::Colour;
///
/// let colour = Colour::new(6573123);
///
/// assert_eq!(colour.get_tuple(), (100, 76, 67));
/// ```
///
/// [`get_tuple`]: #method.get_tuple
#[inline]
pub fn new(value: u32) -> Colour {
Colour(value)
}
/// Generates a new Colour from an RGB value, creating an inner u32
/// representation.
///
/// # Examples
///
/// Creating a `Colour` via its RGB values will set its inner u32 correctly:
///
/// ```rust
/// use serenity::utils::Colour;
///
/// assert!(Colour::from_rgb(255, 0, 0).0 == 0xFF0000);
/// assert!(Colour::from_rgb(217, 23, 211).0 == 0xD917D3);
/// ```
///
/// And you can then retrieve those same RGB values via its methods:
///
/// ```rust
/// use serenity::utils::Colour;
///
/// let colour = Colour::from_rgb(217, 45, 215);
///
/// assert_eq!(colour.get_r(), 217);
/// assert_eq!(colour.get_g(), 45);
/// assert_eq!(colour.get_b(), 215);
/// assert_eq!(colour.get_tuple(), (217, 45, 215));
/// ```
pub fn from_rgb(r: u8, g: u8, b: u8) -> Colour {
let mut uint = r as u32;
uint = (uint << 8) | (g as u32);
uint = (uint << 8) | (b as u32);
Colour(uint)
}
#[doc(hidden)]
pub fn decode(value: Value) -> Result<Colour> {
match value {
Value::U64(v) => Ok(Colour(v as u32)),
Value::I64(v) => Ok(Colour(v as u32)),
other => Err(Error::Decode("Expected valid colour", other)),
}
}
/// Returns the red RGB component of this Colour.
///
/// # Examples
///
/// ```rust
/// use serenity::utils::Colour;
///
/// assert_eq!(Colour::new(6573123).get_r(), 100);
/// ```
pub fn get_r(&self) -> u8 {
((self.0 >> 16) & 255) as u8
}
/// Returns the green RGB component of this Colour.
///
/// # Examples
///
/// ```rust
/// use serenity::utils::Colour;
///
/// assert_eq!(Colour::new(6573123).get_g(), 76);
/// ```
pub fn get_g(&self) -> u8 {
((self.0 >> 8) & 255) as u8
}
/// Returns the blue RGB component of this Colour.
///
/// # Examples
///
/// ```rust
/// use serenity::utils::Colour;
///
/// assert_eq!(Colour::new(6573123).get_b(), 67);
pub fn get_b(&self) -> u8 {
(self.0 & 255) as u8
}
/// Returns a tuple of the red, green, and blue components of this Colour.
///
/// This is equivalent to creating a tuple with the return values of
/// [`get_r`], [`get_g`], and [`get_b`].
///
/// # Examples
///
/// ```rust
/// use serenity::utils::Colour;
///
/// assert_eq!(Colour::new(6573123).get_tuple(), (100, 76, 67));
/// ```
///
/// [`get_r`]: #method.get_r
/// [`get_g`]: #method.get_g
/// [`get_b`]: #method.get_b
pub fn get_tuple(&self) -> (u8, u8, u8) {
(self.get_r(), self.get_g(), self.get_b())
}
}
impl From<i32> for Colour {
/// Constructs a Colour from a i32.
///
/// This is used for functions that accept `Into<Colour>`.
///
/// This is useful when providing hex values.
///
/// # Examples
///
/// ```rust
/// use serenity::utils::Colour;
///
/// assert_eq!(Colour::from(0xDEA584).get_tuple(), (222, 165, 132));
/// ```
fn from(value: i32) -> Colour {
Colour(value as u32)
}
}
impl From<u32> for Colour {
/// Constructs a Colour from a u32.
///
/// This is used for functions that accept `Into<Colour>`.
///
/// # Examples
///
/// ```rust
/// use serenity::utils::Colour;
///
/// assert_eq!(Colour::from(6573123u32).get_r(), 100);
/// ```
fn from(value: u32) -> Colour {
Colour(value)
}
}
impl From<u64> for Colour {
/// Constructs a Colour from a u32.
///
/// This is used for functions that accept `Into<Colour>`.
///
/// # Examples
///
/// ```rust
/// use serenity::utils::Colour;
///
/// assert_eq!(Colour::from(6573123u64).get_r(), 100);
/// ```
fn from(value: u64) -> Colour {
Colour(value as u32)
}
}
colour! {
/// Creates a new `Colour`, setting its RGB value to `(111, 198, 226)`.
blitz_blue, 0x6FC6E2;
/// Creates a new `Colour`, setting its RGB value to `(52, 152, 219)`.
blue, 0x3498DB;
/// Creates a new `Colour`, setting its RGB value to `(114, 137, 218)`.
blurple, 0x7289DA;
/// Creates a new `Colour`, setting its RGB value to `(32, 102, 148)`.
dark_blue, 0x206694;
/// Creates a new `Colour`, setting its RGB value to `(194, 124, 14)`.
dark_gold, 0xC27C0E;
/// Creates a new `Colour`, setting its RGB value to `(31, 139, 76)`.
dark_green, 0x1F8B4C;
/// Creates a new `Colour`, setting its RGB value to `(96, 125, 139)`.
dark_grey, 0x607D8B;
/// Creates a new `Colour`, setting its RGB value to `(173, 20, 87)`.
dark_magenta, 0xAD1457;
/// Creates a new `Colour`, setting its RGB value to `(168, 67, 0)`.
dark_orange, 0xA84300;
/// Creates a new `Colour`, setting its RGB value to `(113, 54, 138)`.
dark_purple, 0x71368A;
/// Creates a new `Colour`, setting its RGB value to `(153, 45, 34)`.
dark_red, 0x992D22;
/// Creates a new `Colour`, setting its RGB value to `(17, 128, 106)`.
dark_teal, 0x11806A;
/// Creates a new `Colour`, setting its RGB value to `(84, 110, 122)`.
darker_grey, 0x546E7A;
/// Creates a new `Colour`, setting its RGB value to `(250, 177, 237)`.
fabled_pink, 0xFAB1ED;
/// Creates a new `Colour`, setting its RGB value to `(17, 202, 128)`.
fooyoo, 0x11CA80;
/// Creates a new `Colour`, setting its RGB value to `(241, 196, 15)`.
gold, 0xF1C40F;
/// Creates a new `Colour`, setting its RGB value to `(186, 218, 85)`.
kerbal, 0xBADA55;
/// Creates a new `Colour`, setting its RGB value to `(151, 156, 159)`.
light_grey, 0x979C9F;
/// Creates a new `Colour`, setting its RGB value to `(149, 165, 166)`.
lighter_grey, 0x95A5A6;
/// Creates a new `Colour`, setting its RGB value to `(233, 30, 99)`.
magenta, 0xE91E63;
/// Creates a new `Colour`, setting its RGB value to `(230, 131, 151)`.
meibi_pink, 0xE68397;
/// Creates a new `Colour`, setting its RGB value to `(230, 126, 34)`.
orange, 0xE67E22;
/// Creates a new `Colour`, setting its RGB value to `(155, 89, 182)`.
purple, 0x9B59B6;
/// Creates a new `Colour`, setting its RGB value to `(231, 76, 60)`.
red, 0xE74C3C;
/// Creates a new `Colour`, setting its RGB value to `(117, 150, 255)`.
rohrkatze_blue, 0x7596FF;
/// Creates a new `Colour`, setting its RGB value to `(246, 219, 216)`.
rosewater, 0xF6DBD8;
/// Creates a new `Colour`, setting its RGB value to `(26, 188, 156)`.
teal, 0x1ABC9C;
}
impl Default for Colour {
/// Creates a default value for a `Colour`, setting the inner value to `0`.
fn default() -> Colour {
Colour(0)
}
}