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

Add hex color support #70

Merged
merged 3 commits into from
Dec 31, 2020
Merged

Conversation

TutenStain
Copy link
Contributor

This PR adds support for parsing hex colors, based on discussion in issue #65

Feel free to suggest changes. My first few lines in Rust.

@fcsonline
Copy link
Owner

fcsonline commented Dec 30, 2020

Welcome to Rust! And welcome to tmux-thumbs too! 👏

Awesome to add hex colors support for tmux-thumbs!

I suggest doing these changes to ensure better that the incoming format is a good one:

diff --git a/src/colors.rs b/src/colors.rs
index 7042286..96a3d46 100644
--- a/src/colors.rs
+++ b/src/colors.rs
@@ -1,13 +1,18 @@
 use termion::color;
+use regex::Regex;
 
 pub fn get_color(color_name: &str) -> Box<dyn color::Color> {
+  let rgb = Regex::new(r"#([[:xdigit:]]{2})([[:xdigit:]]{2})([[:xdigit:]]{2})").unwrap();
+
+  if let Some(captures) = rgb.captures(color_name) {
+    let r = u8::from_str_radix(captures.get(1).unwrap().as_str(), 16).unwrap();
+    let g = u8::from_str_radix(captures.get(2).unwrap().as_str(), 16).unwrap();
+    let b = u8::from_str_radix(captures.get(3).unwrap().as_str(), 16).unwrap();
+
+    return Box::new(color::Rgb(r, g, b));
+  }
+
   match color_name {
-    color_name if color_name.starts_with('#') && color_name.len() == 7 => {
-      let r = u8::from_str_radix(&color_name[1..3], 16).unwrap();
-      let g = u8::from_str_radix(&color_name[3..5], 16).unwrap();
-      let b = u8::from_str_radix(&color_name[5..7], 16).unwrap();
-      Box::new(color::Rgb(r, g, b))
-    },
     "black" => Box::new(color::Black),
     "red" => Box::new(color::Red),
     "green" => Box::new(color::Green),
@@ -35,12 +40,18 @@ mod tests {
 
   #[test]
   fn parse_rgb() {
-    let text1 = println!("{}{}", color::Fg(&*get_color("#1b1cbf")), "foo");
+    let text1 = println!("{}{}", color::Fg(&*get_color("#1b1cBF")), "foo");
     let text2 = println!("{}{}", color::Fg(color::Rgb(27, 28, 191)), "foo");
 
     assert_eq!(text1, text2);
   }
 
+  #[test]
+  #[should_panic]
+  fn parse_invalid_rgb() {
+    println!("{}{}", color::Fg(&*get_color("#1b1cbJ")), "foo");
+  }
+
   #[test]
   #[should_panic]
   fn no_match_color() {

Also, I would add some documentation in the Readme file about this.

@TutenStain
Copy link
Contributor Author

Thanks! I have addressed your comments and have force pushed the changes to parse and validate the colors using regexp instead.

@fcsonline
Copy link
Owner

Awesome! Merging!

@fcsonline fcsonline merged commit 53fdaa3 into fcsonline:master Dec 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants