Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: impl From<(r, g, b)> to CustomColor #153

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
1 change: 1 addition & 0 deletions examples/custom_colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
16 changes: 16 additions & 0 deletions src/customcolors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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);
}
}
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,13 @@ pub trait Colorize {
{
self.color(Color::TrueColor { r, g, b })
}
fn custom_color(self, color: CustomColor) -> ColoredString
fn custom_color<T>(self, color: T) -> ColoredString
where
Self: Sized,
T: Into<CustomColor>,
{
let color = color.into();

self.color(Color::TrueColor {
r: color.r,
g: color.g,
Expand Down Expand Up @@ -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<T>(self, color: T) -> ColoredString
where
Self: Sized,
T: Into<CustomColor>,
{
let color = color.into();

self.on_color(Color::TrueColor {
r: color.r,
g: color.g,
Expand Down Expand Up @@ -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)
}
Expand Down
Loading