coloriz 0.2.0

A simple library for colorful temrinal
Documentation
use crate::{ColorStyle, Gradient, StyledText, RGB};

/// The trait that can be colored
pub trait Colorize: Into<StyledText> {
    /// Converts the given value to a [ColoredText] with the foreground color style
    fn fg<C: Into<ColorStyle>>(&self, color: C) -> StyledText;
    /// Converts the given value to a [ColoredText] with the background color style
    fn bg<C: Into<ColorStyle>>(&self, color: C) -> StyledText;
    /// Converts the given value to a [ColoredText] with the foreground grayscale
    fn fg_gray(&self, x: u8) -> StyledText;
    /// Converts the given value to a [ColoredText] with the background grayscale
    fn bg_gray(&self, x: u8) -> StyledText;
    /// Converts the given value to a [ColoredText] with the foreground gradient
    fn fg_gradient<C: Into<RGB>>(&self, start: C, end: C) -> StyledText;
    /// Converts the given value to a [ColoredText] with the background gradient
    fn bg_gradient<C: Into<RGB>>(&self, start: C, end: C) -> StyledText;
}

impl Colorize for StyledText {
    fn fg<C: Into<ColorStyle>>(&self, color: C) -> StyledText {
        self.with_foreground_color(color)
    }

    fn bg<C: Into<ColorStyle>>(&self, color: C) -> StyledText {
        self.with_background_color(color)
    }

    fn fg_gray(&self, x: u8) -> StyledText {
        self.with_foreground_color(RGB::gray(x))
    }

    fn bg_gray(&self, x: u8) -> StyledText {
        self.with_background_color(RGB::gray(x))
    }

    fn fg_gradient<C: Into<RGB>>(&self, start: C, end: C) -> StyledText {
        self.with_foreground_color(Gradient::new(start.into(), end.into()))
    }

    fn bg_gradient<C: Into<RGB>>(&self, start: C, end: C) -> StyledText {
        self.with_background_color(Gradient::new(start.into(), end.into()))
    }
}

impl Colorize for &str {
    fn fg<C: Into<ColorStyle>>(&self, color: C) -> StyledText {
        StyledText::new(self).with_foreground_color(color)
    }

    fn bg<C: Into<ColorStyle>>(&self, color: C) -> StyledText {
        StyledText::new(self).with_background_color(color)
    }

    fn fg_gray(&self, x: u8) -> StyledText {
        StyledText::new(self).with_foreground_color(RGB::gray(x))
    }

    fn bg_gray(&self, x: u8) -> StyledText {
        StyledText::new(self).with_background_color(RGB::gray(x))
    }

    fn fg_gradient<C: Into<RGB>>(&self, start: C, end: C) -> StyledText {
        StyledText::new(self).with_foreground_color(Gradient::new(start.into(), end.into()))
    }

    fn bg_gradient<C: Into<RGB>>(&self, start: C, end: C) -> StyledText {
        StyledText::new(self).with_background_color(Gradient::new(start.into(), end.into()))
    }
}

impl Colorize for String {
    fn fg<C: Into<ColorStyle>>(&self, color: C) -> StyledText {
        StyledText::new(self).with_foreground_color(color)
    }

    fn bg<C: Into<ColorStyle>>(&self, color: C) -> StyledText {
        StyledText::new(self).with_background_color(color)
    }

    fn fg_gray(&self, x: u8) -> StyledText {
        StyledText::new(self).with_foreground_color(RGB::gray(x))
    }

    fn bg_gray(&self, x: u8) -> StyledText {
        StyledText::new(self).with_background_color(RGB::gray(x))
    }

    fn fg_gradient<C: Into<RGB>>(&self, start: C, end: C) -> StyledText {
        StyledText::new(self).with_foreground_color(Gradient::new(start.into(), end.into()))
    }

    fn bg_gradient<C: Into<RGB>>(&self, start: C, end: C) -> StyledText {
        StyledText::new(self).with_background_color(Gradient::new(start.into(), end.into()))
    }
}