termwiz: also allow #FFF form of color spec
We allowed the 6-digit variant, also allow the 3-digit variant.
This commit is contained in:
1 parent
5c5bef934b
commit
2957173a57
2 files changed
+26
-3
No files matched your search
+24
-1
@@ -141,7 +141,20 @@ impl RgbColor {
|
||||
/// Construct a color from a string of the form `#RRGGBB` where
|
||||
/// R, G and B are all hex digits.
|
||||
pub fn from_rgb_str(s: &str) -> Option<RgbColor> {
|
||||
if s.len() == 7 && s.as_bytes()[0] == b'#' {
|
||||
if s.len() == 4 && s.as_bytes()[0] == b'#' {
|
||||
let mut chars = s.chars().skip(1);
|
||||
|
||||
macro_rules! digit {
|
||||
() => {{
|
||||
let lo = match chars.next().unwrap().to_digit(16) {
|
||||
Some(v) => v as u8,
|
||||
None => return None,
|
||||
};
|
||||
lo << 4 | lo
|
||||
}};
|
||||
}
|
||||
Some(Self::new(digit!(), digit!(), digit!()))
|
||||
} else if s.len() == 7 && s.as_bytes()[0] == b'#' {
|
||||
let mut chars = s.chars().skip(1);
|
||||
|
||||
macro_rules! digit {
|
||||
@@ -324,6 +337,16 @@ mod tests {
|
||||
assert!(RgbColor::from_rgb_str("").is_none());
|
||||
assert!(RgbColor::from_rgb_str("#xyxyxy").is_none());
|
||||
|
||||
let black = RgbColor::from_rgb_str("#000").unwrap();
|
||||
assert_eq!(black.red, 0);
|
||||
assert_eq!(black.green, 0);
|
||||
assert_eq!(black.blue, 0);
|
||||
|
||||
let black = RgbColor::from_rgb_str("#FFF").unwrap();
|
||||
assert_eq!(black.red, 0xff);
|
||||
assert_eq!(black.green, 0xff);
|
||||
assert_eq!(black.blue, 0xff);
|
||||
|
||||
let black = RgbColor::from_rgb_str("#000000").unwrap();
|
||||
assert_eq!(black.red, 0);
|
||||
assert_eq!(black.green, 0);
|
||||
|
||||
@@ -201,7 +201,7 @@ impl OperatingSystemCommand {
|
||||
} else {
|
||||
ColorOrQuery::Color(
|
||||
RgbColor::from_named_or_rgb_string(spec)
|
||||
.ok_or_else(|| anyhow!("invalid color spec"))?,
|
||||
.ok_or_else(|| anyhow!("invalid color spec {:?}", spec))?,
|
||||
)
|
||||
};
|
||||
|
||||
@@ -232,7 +232,7 @@ impl OperatingSystemCommand {
|
||||
let spec = str::from_utf8(spec)?;
|
||||
colors.push(ColorOrQuery::Color(
|
||||
RgbColor::from_named_or_rgb_string(spec)
|
||||
.ok_or_else(|| anyhow!("invalid color spec"))?,
|
||||
.ok_or_else(|| anyhow!("invalid color spec {:?}", spec))?,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user