| 1 |
/// Custom color structure, it will generate a true color in the result |
| 2 |
#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 3 |
pub struct CustomColor { |
| 4 |
/// Red |
| 5 |
pub r: u8, |
| 6 |
/// Green |
| 7 |
pub g: u8, |
| 8 |
/// Blue |
| 9 |
pub b: u8, |
| 10 |
} |
| 11 |
|
| 12 |
/// This only makes custom color creation easier. |
| 13 |
impl CustomColor { |
| 14 |
/// Create a new custom color |
| 15 |
#[must_use] |
| 16 |
pub const fn new(r: u8, g: u8, b: u8) -> Self { |
| 17 |
Self { r, g, b } |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
impl From<(u8, u8, u8)> for CustomColor { |
| 22 |
fn from((r, g, b): (u8, u8, u8)) -> Self { |
| 23 |
Self::new(r, g, b) |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
#[cfg(test)] |
| 28 |
mod tests { |
| 29 |
use crate::*; |
| 30 |
#[cfg_attr(feature = "no-color", ignore)] |
| 31 |
#[test] |
| 32 |
fn main() { |
| 33 |
let my_color = CustomColor::new(0, 120, 120); |
| 34 |
insta::assert_snapshot!("Greetings from Ukraine".custom_color(my_color)); |
| 35 |
} |
| 36 |
|
| 37 |
#[test] |
| 38 |
fn from_tuple() { |
| 39 |
let tuple = (1u8, 255u8, 0u8); |
| 40 |
let cc = CustomColor::from(tuple); |
| 41 |
|
| 42 |
assert_eq!(cc.r, tuple.0); |
| 43 |
assert_eq!(cc.g, tuple.1); |
| 44 |
assert_eq!(cc.b, tuple.2); |
| 45 |
} |
| 46 |
} |