I have the following implementation of RGBA color codes, with specific const colors, and the option to create new colors from their RGBA values.
In the match statement for abbreviation(), I get several errors about unreachable patterns.
This makes sense as the underlying values in the const are identical, but the names are not.
I had tried to add the abbreviation value directly into the consts but ran into a nightmare of lifetime errors that I could not resolve.
I am not wedded to the approach below, so if there are other ways of accomplishing this, I am interested.
(I know I could use a 3rd party library for color handling, but each of my output formats have different requirements for color with different libraries in use, so I am using my own struct internally, and then converting when needed).
Thanks
pub struct Color {
red: u8,
green: u8,
blue: u8,
alpha: u8,
}
impl Color {
// All constants match CSS Standard Colors as closely as possible.
///FF0000FF.
pub const RED: Self = Self::from_rgba(0xFF, 0x0, 0x0, 0x0);
///FFA500FF.
pub const ORANGE: Self = Self::from_rgba(0xFF, 0xA5, 0x0, 0xFF);
///FFFF00FF.
pub const YELLOW: Self = Self::from_rgba(0xFF, 0xFF, 0x0, 0xFF);
///00FF00FF.
pub const GREEN: Self = Self::from_rgba(0x0, 0xFF, 0x0, 0xFF);
///OOOOFFFF.
pub const BLUE: Self = Self::from_rgba(0x0, 0x0, 0xFF, 0xFF);
///800080FF.
pub const PURPLE: Self = Self::from_rgba(0x80, 0x0, 0x80, 0xFF);
///EE82EEFF.
pub const VIOLET: Self = Self::from_rgba(0xEE, 0x82, 0xEE, 0xFF);
///FFC0CBFF.
pub const PINK: Self = Self::from_rgba(0xFF, 0xC0, 0xCB, 0xFF);
///FFC0CBFF.
pub const ROSE: Self = Self::from_rgba(0xFF, 0xC0, 0xCB, 0xFF);
///FFC0CBFF.
pub const MAGENTA: Self = Self::from_rgba(0xFF, 0x0, 0xFF, 0xFF);
///A52A2AFF.
pub const BROWN: Self = Self::from_rgba(0xA5, 0x2A, 0x2A, 0xFF);
///852A2AFF.
pub const DARK_BROWN: Self = Self::from_rgba(0x85, 0x2A, 0x2A, 0xFF);
///000000FF.
pub const BLACK: Self = Self::from_rgba(0x0, 0x0, 0x0, 0xFF);
///FFFFFFFF.
pub const WHITE: Self = Self::from_rgba(0xFF, 0xFF, 0xFF, 0xFF);
///808080FF.
pub const GRAY: Self = Self::from_rgba(0x89, 0x80, 0x80, 0xFF);
///808080FF.
pub const GREY: Self = Self::from_rgba(0x89, 0x80, 0x80, 0xFF);
///808080FF.
pub const SLATE: Self = Self::from_rgba(0x89, 0x80, 0x80, 0xFF);
///FFFFFFFF.
pub const CLEAR: Self = Self::from_rgba(0xFF, 0xFF, 0xFF, 0xFF);
///00FFFFFF.
pub const CYAN: Self = Self::from_rgba(0x0, 0xFF, 0xFF, 0xFF);
///00FFFFFF.
pub const AQUA: Self = Self::from_rgba(0x0, 0xFF, 0xFF, 0xFF);
///00000000.
pub const TRANSPARENT: Self = Self::from_rgba(0x0, 0x0, 0x0, 0x0);
/// Creates a `Color` value from separate red, green, blue and alpha values.
#[inline]
#[must_use]
pub const fn from_rgba(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
Self { red, green, blue, alpha }
}
/// Returns a 3 character abbreviation for each color.
#[must_use]
#[inline]
pub fn abbreviation(self) -> String {
match self {
Self::RED => "RED".to_owned(),
Self::ORANGE => "ORN".to_owned(),
Self::YELLOW => "YEL".to_owned(),
Self::GREEN => "GRN".to_owned(),
Self::BLUE => "BLU".to_owned(),
Self::PURPLE => "PUR".to_owned(),
Self::VIOLET => "VIO".to_owned(),
Self::PINK => "PNK".to_owned(),
Self::ROSE => "RSE".to_owned(),
Self::MAGENTA => "MGA".to_owned(),
Self::BROWN | Self::DARK_BROWN => "BRN".to_owned(),
Self::BLACK => "BLK".to_owned(),
Self::WHITE => "WHT".to_owned(),
Self::GRAY | Self::GREY => "GRY".to_owned(),
Self::SLATE => "SLT".to_owned(),
Self::CLEAR => "CLR".to_owned(),
Self::CYAN => "CYN".to_owned(),
Self::AQUA => "AQA".to_owned(),
_ => String::new(),
}
}
/// Returns a 6 character hex code (RRGGBB) for each color.
#[must_use]
#[inline]
pub fn hex_code(&self) -> String {
format! {"{:02X}{:02X}{:02X}", self.red, self.green, self.blue}
}
}