diff --git a/README.md b/README.md index 29096bc..35e69db 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ Coloring terminal so simple, you already know how to do it! "this is also red on blue".on_blue().red(); "you can use truecolor values too!".truecolor(0, 255, 136); "background truecolor also works :)".on_truecolor(135, 28, 167); + "truecolor from tuple".custom_color((0, 255, 136)); + "background truecolor from tuple".on_custom_color((0, 255, 136)); "bright colors are welcome as well".on_bright_blue().bright_red(); "you can also make bold comments".bold(); println!("{} {} {}", "or use".cyan(), "any".italic().yellow(), "string type".cyan()); diff --git a/examples/custom_colors.rs b/examples/custom_colors.rs index 05c588e..778efdf 100644 --- a/examples/custom_colors.rs +++ b/examples/custom_colors.rs @@ -3,4 +3,5 @@ fn main() { let my_color = CustomColor::new(0, 120, 120); println!("{}", "Greetings from Ukraine".custom_color(my_color)); println!("{}", "Slava Ukraini!".on_custom_color(my_color)); + println!("{}", "Hello World!".on_custom_color((0, 120, 120))); } diff --git a/src/customcolors.rs b/src/customcolors.rs index 896e92f..139d968 100644 --- a/src/customcolors.rs +++ b/src/customcolors.rs @@ -17,6 +17,12 @@ impl CustomColor { } } +impl From<(u8, u8, u8)> for CustomColor { + fn from((r, g, b): (u8, u8, u8)) -> Self { + Self::new(r, g, b) + } +} + #[cfg(test)] mod tests { use crate::*; @@ -25,4 +31,14 @@ mod tests { let my_color = CustomColor::new(0, 120, 120); println!("{}", "Greetings from Ukraine".custom_color(my_color)); } + + #[test] + fn from_tuple() { + let tuple = (1u8, 255u8, 0u8); + let cc = CustomColor::from(tuple); + + assert_eq!(cc.r, tuple.0); + assert_eq!(cc.g, tuple.1); + assert_eq!(cc.b, tuple.2); + } } diff --git a/src/lib.rs b/src/lib.rs index 2ac0a3a..4a521d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -177,10 +177,13 @@ pub trait Colorize { { self.color(Color::TrueColor { r, g, b }) } - fn custom_color(self, color: CustomColor) -> ColoredString + fn custom_color(self, color: T) -> ColoredString where Self: Sized, + T: Into, { + let color = color.into(); + self.color(Color::TrueColor { r: color.r, g: color.g, @@ -303,10 +306,13 @@ pub trait Colorize { { self.on_color(Color::TrueColor { r, g, b }) } - fn on_custom_color(self, color: CustomColor) -> ColoredString + fn on_custom_color(self, color: T) -> ColoredString where Self: Sized, + T: Into, { + let color = color.into(); + self.on_color(Color::TrueColor { r: color.r, g: color.g, @@ -684,6 +690,8 @@ mod tests { println!("{}", toto.truecolor(255, 0, 0)); println!("{}", toto.truecolor(255, 255, 0)); println!("{}", toto.on_truecolor(0, 80, 80)); + println!("{}", toto.custom_color((255, 255, 0))); + println!("{}", toto.on_custom_color((0, 80, 80))); // uncomment to see term output // assert!(false) }