chromakitx 1.0.0

A comprehensive color manipulation library for Rust
Documentation
// SPDX-FileCopyrightText: 2023 CELESTIFYX Team
// SPDX-License-Identifier: GPL-3.0-or-later

#[macro_export]
macro_rules! define_text_styles {
    ($($name:ident = $code:expr),* $(,)?) => {
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        pub enum TextStyle {
            $($name,)*
        }

        impl TextStyle {
            fn code(&self) -> u8 {
                match self {
                    $(TextStyle::$name => $code,)*
                }
            }

            fn from_str(s: &str) -> Option<Self> {
                let normalized: String = s.replace(['_', '-', ' '], "").to_lowercase();

                match normalized.as_str() {
                    $(stringify!($name) => Some(TextStyle::$name),)*
                    _                   => None
                }
            }
        }
    };
}

#[macro_export]
macro_rules! define_ansi_colors {
    ($($name:ident = ($fg:expr, $bg:expr)),* $(,)?) => {
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        pub enum AnsiColor {
            $($name,)*
        }

        impl AnsiColor {
            fn values(&self) -> (u8, u8) {
                match self {
                    $(AnsiColor::$name => ($fg, $bg),)*
                }
            }

            fn from_str(s: &str) -> Option<Self> {
                let normalized: String = s.replace(['_', '-', ' '], "").to_lowercase();

                match normalized.as_str() {
                    $(stringify!($name) => Some(AnsiColor::$name),)*
                    _                   => None
                }
            }
        }
    };
}

#[macro_export]
macro_rules! define_css_colors {
    ($($name:ident = ($r:expr, $g:expr, $b:expr)),* $(,)?) => {
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        pub enum CssColor {
            $($name,)*
        }

        impl CssColor {
            fn rgb(&self) -> (u8, u8, u8) {
                match self {
                    $(CssColor::$name => ($r, $g, $b),)*
                }
            }

            fn from_str(s: &str) -> Option<Self> {
                let normalized: String = s.replace(['_', '-', ' '], "").to_lowercase();

                match normalized.as_str() {
                    $(stringify!($name) => Some(CssColor::$name),)*
                    _                   => None
                }
            }
        }
    };
}

#[macro_export]
macro_rules! define_xterm_colors {
    ($($name:ident = ($r:expr, $g:expr, $b:expr)),* $(,)?) => {
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        pub enum XtermColor {
            $($name,)*
        }

        impl XtermColor {
            fn rgb(&self) -> (u8, u8, u8) {
                match self {
                    $(XtermColor::$name => ($r, $g, $b),)*
                }
            }

            fn from_str(s: &str) -> Option<Self> {
                let normalized: String = s.replace(['_', '-', ' '], "").to_lowercase();

                match normalized.as_str() {
                    $(stringify!($name) => Some(XtermColor::$name),)*
                    _                   => None
                }
            }
        }
    }
}