use super::stylable::Stylable;
use crate::color::{rgb_from_color_definition, rgb_to_ansi8, ColorConversionError, IntoRgb};
pub fn color_rgb<C, T>(color_input: C, text: T) -> Result<T::Output, ColorConversionError>
where
C: Copy + IntoRgb,
T: Stylable,
{
let f = |s: &str| -> Result<String, ColorConversionError> {
let [r, g, b] = rgb_from_color_definition(color_input)?;
Ok(format!("\x1b[38;2;{};{};{}m{}\x1b[0m", r, g, b, s))
};
text.apply_result(f)
}
pub fn background_rgb<C, T>(color_input: C, text: T) -> Result<T::Output, ColorConversionError>
where
C: Copy + IntoRgb,
T: Stylable,
{
let f = |s: &str| -> Result<String, ColorConversionError> {
let [r, g, b] = rgb_from_color_definition(color_input)?;
Ok(format!("\x1b[48;2;{};{};{}m{}\x1b[0m", r, g, b, s))
};
text.apply_result(f)
}
pub fn color_ansi<C, T>(color_input: C, text: T) -> Result<T::Output, ColorConversionError>
where
C: Copy + IntoRgb,
T: Stylable,
{
let f = |s: &str| -> Result<String, ColorConversionError> {
let rgb = rgb_from_color_definition(color_input)?;
let code = rgb_to_ansi8(rgb);
Ok(format!("\x1b[38;5;{}m{}\x1b[0m", code, s))
};
text.apply_result(f)
}
pub fn background_ansi<C, T>(color_input: C, text: T) -> Result<T::Output, ColorConversionError>
where
C: Copy + IntoRgb,
T: Stylable,
{
let f = |s: &str| -> Result<String, ColorConversionError> {
let rgb = rgb_from_color_definition(color_input)?;
let code = rgb_to_ansi8(rgb);
Ok(format!("\x1b[48;5;{}m{}\x1b[0m", code, s))
};
text.apply_result(f)
}
pub fn color<C, T>(color_input: C, text: T) -> Result<T::Output, ColorConversionError>
where
C: Copy + IntoRgb,
T: Stylable,
{
color_rgb(color_input, text)
}
pub fn background<C, T>(color_input: C, text: T) -> Result<T::Output, ColorConversionError>
where
C: Copy + IntoRgb,
T: Stylable,
{
background_rgb(color_input, text)
}