mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-27 21:33:09 +00:00
Fix a bug in the color.rs port
This was incorrectly parsing FFF as 0x0F0F0F instead of 0xFFFFFF.
This commit is contained in:
parent
a0a2475ccb
commit
1bbd60c597
1 changed files with 31 additions and 21 deletions
|
@ -1,4 +1,4 @@
|
||||||
use std::{array, cmp::Ordering};
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
wchar::{widestrs, wstr, WExt, WString, L},
|
wchar::{widestrs, wstr, WExt, WString, L},
|
||||||
|
@ -99,6 +99,14 @@ impl RgbColor {
|
||||||
.or_else(|| Self::try_parse_rgb(s))
|
.or_else(|| Self::try_parse_rgb(s))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create an RGB color.
|
||||||
|
pub fn from_rgb(r: u8, g: u8, b: u8) -> Self {
|
||||||
|
Self {
|
||||||
|
typ: Type::Rgb(Color24 { r, g, b }),
|
||||||
|
flags: Flags::DEFAULT,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns whether the color is the normal special color.
|
/// Returns whether the color is the normal special color.
|
||||||
pub const fn is_normal(self) -> bool {
|
pub const fn is_normal(self) -> bool {
|
||||||
matches!(self.typ, Type::Normal)
|
matches!(self.typ, Type::Normal)
|
||||||
|
@ -225,36 +233,29 @@ impl RgbColor {
|
||||||
s = &s[1..];
|
s = &s[1..];
|
||||||
}
|
}
|
||||||
|
|
||||||
let hex_digit = |i| {
|
let hex_digit = |i| -> Option<u8> {
|
||||||
s.char_at(i)
|
s.char_at(i)
|
||||||
.to_digit(16)
|
.to_digit(16)
|
||||||
.map(|n| n.try_into().expect("hex digit should always be < 256"))
|
.map(|n| n.try_into().expect("hex digit should always be < 256"))
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: `array::try_from_fn()`: https://github.com/rust-lang/rust/issues/89379
|
let r;
|
||||||
let rgb: [_; 3] = if s.len() == 3 {
|
let g;
|
||||||
|
let b;
|
||||||
|
if s.len() == 3 {
|
||||||
// Format: FA3
|
// Format: FA3
|
||||||
array::from_fn(hex_digit)
|
r = hex_digit(0)? * 16 + hex_digit(0)?;
|
||||||
|
g = hex_digit(1)? * 16 + hex_digit(1)?;
|
||||||
|
b = hex_digit(2)? * 16 + hex_digit(2)?;
|
||||||
} else if s.len() == 6 {
|
} else if s.len() == 6 {
|
||||||
// Format: F3A035
|
// Format: F3A035
|
||||||
array::from_fn(|i| {
|
r = hex_digit(0)? * 16 + hex_digit(1)?;
|
||||||
let hi = hex_digit(2 * i)?;
|
g = hex_digit(2)? * 16 + hex_digit(3)?;
|
||||||
let lo = hex_digit(2 * i + 1)?;
|
b = hex_digit(4)? * 16 + hex_digit(5)?;
|
||||||
|
|
||||||
Some(hi * 16 + lo)
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
return None;
|
return None;
|
||||||
};
|
}
|
||||||
|
Some(RgbColor::from_rgb(r, g, b))
|
||||||
Some(Self {
|
|
||||||
typ: Type::Rgb(Color24 {
|
|
||||||
r: rgb[0]?,
|
|
||||||
g: rgb[1]?,
|
|
||||||
b: rgb[2]?,
|
|
||||||
}),
|
|
||||||
flags: Flags::default(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Try parsing an explicit color name like "magenta".
|
/// Try parsing an explicit color name like "magenta".
|
||||||
|
@ -425,6 +426,15 @@ mod tests {
|
||||||
assert!(RgbColor::from_wstr("mooganta"L).is_none());
|
assert!(RgbColor::from_wstr("mooganta"L).is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[widestrs]
|
||||||
|
fn parse_rgb() {
|
||||||
|
assert!(RgbColor::from_wstr("##FF00A0"L) == None);
|
||||||
|
assert!(RgbColor::from_wstr("#FF00A0"L) == Some(RgbColor::from_rgb(0xff, 0x00, 0xa0)));
|
||||||
|
assert!(RgbColor::from_wstr("FF00A0"L) == Some(RgbColor::from_rgb(0xff, 0x00, 0xa0)));
|
||||||
|
assert!(RgbColor::from_wstr("FAF"L) == Some(RgbColor::from_rgb(0xff, 0xAA, 0xff)));
|
||||||
|
}
|
||||||
|
|
||||||
// Regression test for multiplicative overflow in convert_color.
|
// Regression test for multiplicative overflow in convert_color.
|
||||||
#[test]
|
#[test]
|
||||||
fn test_term16_color_for_rgb() {
|
fn test_term16_color_for_rgb() {
|
||||||
|
|
Loading…
Reference in a new issue