badge_maker/badge/color/
color.rs

1use std::fmt::{Display, Formatter};
2
3#[derive(Debug, PartialEq, Eq, Clone, Hash)]
4pub enum Color {
5    Named(NamedColor),
6    Alias(AliasColor),
7    Rgb(u8, u8, u8),
8}
9
10impl Display for Color {
11    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
12        match self {
13            Color::Named(named) => {
14                write!(f, "{}", named.hex())
15            }
16
17            Color::Alias(alias) => {
18                write!(f, "{}", alias.hex())
19            }
20            Color::Rgb(r, g, b) => {
21                write!(f, "rgb({},{},{})", r, g, b)
22            }
23        }
24    }
25}
26
27/// Colors from the
28/// [badge-maker](https://github.com/badges/shields/blob/master/badge-maker/lib/color.js) spec
29#[derive(Debug, PartialEq, Eq, Clone, Hash)]
30pub enum NamedColor {
31    BrightGreen,
32    Green,
33    Yellow,
34    YellowGreen,
35    Orange,
36    Red,
37    Blue,
38    Grey,
39    LightGrey,
40}
41
42#[derive(Debug, PartialEq, Eq, Clone, Hash)]
43pub enum AliasColor {
44    Gray,
45    LightGray,
46    Critical,
47    Important,
48    Success,
49    Informational,
50    Inactive,
51}
52
53impl NamedColor {
54    pub fn hex(&self) -> &'static str {
55        match self {
56            NamedColor::BrightGreen => "#4c1",
57            NamedColor::Green => "#97ca00",
58            NamedColor::Yellow => "#dfb317",
59            NamedColor::YellowGreen => "#a4a61d",
60            NamedColor::Orange => "#fe7d37",
61            NamedColor::Red => "#e05d44",
62            NamedColor::Blue => "#007ec6",
63            NamedColor::Grey => "#555",
64            NamedColor::LightGrey => "#9f9f9f",
65        }
66    }
67}
68
69impl AliasColor {
70    pub fn hex(&self) -> &'static str {
71        match self {
72            AliasColor::Gray => NamedColor::Grey.hex(),
73            AliasColor::LightGray => NamedColor::LightGrey.hex(),
74            AliasColor::Critical => NamedColor::Red.hex(),
75            AliasColor::Important => NamedColor::Orange.hex(),
76            AliasColor::Success => NamedColor::BrightGreen.hex(),
77            AliasColor::Informational => NamedColor::Blue.hex(),
78            AliasColor::Inactive => NamedColor::LightGrey.hex(),
79        }
80    }
81}
82
83impl From<AliasColor> for Color {
84    fn from(alias: AliasColor) -> Self {
85        Color::Alias(alias)
86    }
87}
88
89impl From<NamedColor> for Color {
90    fn from(named: NamedColor) -> Self {
91        Color::Named(named)
92    }
93}
94
95impl From<(u8, u8, u8)> for Color {
96    fn from((r, g, b): (u8, u8, u8)) -> Self {
97        Self::Rgb(r, g, b)
98    }
99}