wallhaven_rs/models/enums/
color.rs

1use std::{fmt::Display, str::FromStr};
2
3use num_enum::TryFromPrimitive;
4
5use crate::ColorParseError;
6
7/// Colors you can filter by.
8///
9/// Color names found thanks to [color-name.com](https://www.color-name.com/hex/).
10///
11/// This enum's value is just hex, so you can get the R, G, B values with the following calculations:
12/// - `let r = (val & 0xFF0000) >> 16`
13/// - `let g = (val & 0x00FF00) >> 8`
14/// - `let b = (val & 0x0000FF) >> 0`
15///
16/// This cannot be customized further as only values allowed by wallhaven can be used.
17#[repr(u32)]
18#[derive(Clone, Copy, Debug, TryFromPrimitive)]
19pub enum Color {
20    /// Color with hex code 0x660000
21    BloodRed = 0x66_00_00,
22    /// Color with hex code 0x990000
23    CrimsonRed = 0x99_00_00,
24    /// Color with hex code 0xcc0000
25    BostonUniversityRed = 0xcc_00_00,
26    /// Color with hex code 0xcc3333
27    PersianRed = 0xcc_33_33,
28    /// Color with hex code 0xea4c88
29    DarkPink = 0xea_4c_88,
30    /// Color with hex code 0x993399
31    CrayolaViolet = 0x99_33_99,
32    /// Color with hex code 0x663399
33    RebeccaPurple = 0x66_33_99,
34    /// Color with hex code 0x333399
35    BluePigment = 0x33_33_99,
36    /// Color with hex code 0x0066cc
37    TrueBlue = 0x00_66_cc,
38    /// Color with hex code 0x0099cc
39    RichElectricBlue = 0x00_99_cc,
40    /// Color with hex code 0x66cccc
41    SeaSerpent = 0x66_cc_cc,
42    /// Color with hex code 0x77cc33
43    RybGreen = 0x77_cc_33,
44    /// Color with hex code 0x669900
45    Avocado = 0x66_99_00,
46    /// Color with hex code 0x336600
47    MetallicGreen = 0x33_66_00,
48    /// Color with hex code 0x666600
49    BronzeYellow = 0x66_66_00,
50    /// Color with hex code 0x999900
51    DarkYellow = 0x99_99_00,
52    /// Color with hex code 0xcccc33
53    Pear = 0xcc_cc_33,
54    /// Color with hex code 0xffff00
55    Yellow = 0xff_ff_00,
56    /// Color with hex code 0xffcc33
57    Sunglow = 0xff_cc_33,
58    /// Color with hex code 0xff9900
59    VividGamboge = 0xff_99_00,
60    /// Color with hex code 0xff6600
61    Orange = 0xff_66_00,
62    /// Color with hex code 0xcc6633
63    MediumVermilion = 0xcc_66_33,
64    /// Color with hex code 0x996633
65    Coconut = 0x99_66_33,
66    /// Color with hex code 0x663300
67    PhilippineBronze = 0x66_33_00,
68    /// Color with hex code 0x000000
69    Black = 0x00_00_00,
70    /// Color with hex code 0x999999
71    SpanishGray = 0x99_99_99,
72    /// Color with hex code 0xABBCDA
73    LightSteelBlue = 0xAB_BC_DA,
74    /// Color with hex code 0xFDADC7
75    CarnationPink = 0xFD_AD_C7,
76    /// Color with hex code 0xE7D8B1
77    CookiesAndCream = 0xE7_D8_B1,
78    /// Color with hex code 0xcccccc
79    ChineseSilver = 0xcc_cc_cc,
80    /// Color with hex code 0xffffff
81    White = 0xff_ff_ff,
82    /// Color with hex code 0x424153
83    Arsenic = 0x42_41_53,
84}
85
86impl Display for Color {
87    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88        write!(f, "{:06X}", *self as u32)
89    }
90}
91
92impl FromStr for Color {
93    type Err = ColorParseError;
94
95    #[allow(clippy::print_stdout)]
96    fn from_str(s: &str) -> Result<Self, Self::Err> {
97        let s = s.strip_prefix("#").unwrap_or(s);
98        let x = u32::from_str_radix(s, 16)?;
99
100        Self::try_from(x).map_err(|_| ColorParseError::InvalidValue)
101    }
102}