chromakitx 1.0.2

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),* $(,)?) => {
        use std::str::FromStr;

        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        pub enum TextStyle {
            $($name,)*
        }

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

        impl FromStr for TextStyle {
            type Err = String;

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

                $(
                    let name_norm: String = stringify!($name).replace(['_', '-', ' '], "").to_lowercase();

                    if normalized == name_norm {
                        return Ok(TextStyle::$name);
                    }
                )*

                Err(s.into())
            }
        }
    };
}

#[macro_export]
macro_rules! define_ansi_colors {
    ($($name:ident = ($fg:expr, $bg:expr)),* $(,)?) => {
        use std::str::FromStr;

        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        pub enum AnsiColor {
            $($name,)*
        }

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

        impl FromStr for AnsiColor {
            type Err = String;

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

                $(
                    let name_norm: String = stringify!($name).replace(['_', '-', ' '], "").to_lowercase();

                    if normalized == name_norm {
                        return Ok(AnsiColor::$name);
                    }
                )*

                Err(s.into())
            }
        }
    };
}

#[macro_export]
macro_rules! define_css_colors {
    ($($name:ident = ($r:expr, $g:expr, $b:expr)),* $(,)?) => {
        use std::str::FromStr;

        #[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),)*
                }
            }
        }

        impl FromStr for CssColor {
            type Err = String;

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

                $(
                    let name_norm: String = stringify!($name).replace(['_', '-', ' '], "").to_lowercase();

                    if normalized == name_norm {
                        return Ok(CssColor::$name);
                    }
                )*

                Err(s.into())
            }
        }
    };
}

#[macro_export]
macro_rules! define_xterm_colors {
    ($($name:ident = ($r:expr, $g:expr, $b:expr)),* $(,)?) => {
        use std::str::FromStr;

        #[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),)*
                }
            }
        }

        impl FromStr for XtermColor {
            type Err = String;

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

                $(
                    let name_norm: String = stringify!($name).replace(['_', '-', ' '], "").to_lowercase();

                    if normalized == name_norm {
                        return Ok(XtermColor::$name);
                    }
                )*

                Err(s.into())
            }
        }
    }
}