RawColor: Add new_rgb() and new_argb() constructors to make callers nicer

parent 8f56c904
......@@ -8,6 +8,26 @@ pub struct RawColor {
pub argb: u32
}
impl RawColor {
pub fn new_rgb (r: u8, g: u8, b: u8) -> RawColor {
RawColor {
argb: (0xff000000 |
(r as u32) << 16 |
(g as u32) << 8 |
(b as u32))
}
}
pub fn new_argb (a: u8, r: u8, g: u8, b: u8) -> RawColor {
RawColor {
argb: ((a as u32) << 24 |
(r as u32) << 16 |
(g as u32) << 8 |
(b as u32))
}
}
}
impl FromStr for RawColor {
type Err = AttributeError;
......@@ -31,45 +51,38 @@ fn hex_digit (c: u8) -> Result<u8, ()> {
}
fn parse_hex (s: &[u8]) -> Result<RawColor, ()> {
let result: Result<u32, ()> = match s.len () {
match s.len () {
8 => {
// #rrggbbaa -> 0xaarrggbb
Ok (((hex_digit (s[0])? * 16 + hex_digit (s[1])?) as u32) << 16 |
((hex_digit (s[2])? * 16 + hex_digit (s[3])?) as u32) << 8 |
((hex_digit (s[4])? * 16 + hex_digit (s[5])?) as u32) |
((hex_digit (s[6])? * 16 + hex_digit (s[7])?) as u32) << 24)
Ok (RawColor::new_argb (hex_digit (s[6])? * 16 + hex_digit (s[7])?,
hex_digit (s[0])? * 16 + hex_digit (s[1])?,
hex_digit (s[2])? * 16 + hex_digit (s[3])?,
hex_digit (s[4])? * 16 + hex_digit (s[5])?))
},
6 => {
// #rrggbb -> 0xffrrggbb
Ok (0xff000000 |
((hex_digit (s[0])? * 16 + hex_digit (s[1])?) as u32) << 16 |
((hex_digit (s[2])? * 16 + hex_digit (s[3])?) as u32) << 8 |
((hex_digit (s[4])? * 16 + hex_digit (s[5])?) as u32))
Ok (RawColor::new_rgb (hex_digit (s[0])? * 16 + hex_digit (s[1])?,
hex_digit (s[2])? * 16 + hex_digit (s[3])?,
hex_digit (s[4])? * 16 + hex_digit (s[5])?))
},
4 => {
// #rgba -> 0xaarrggbb
Ok (((hex_digit (s[0])? * 0x11) as u32) << 16 |
((hex_digit (s[1])? * 0x11) as u32) << 8 |
((hex_digit (s[2])? * 0x11) as u32) |
((hex_digit (s[3])? * 0x11) as u32) << 24)
Ok (RawColor::new_argb (hex_digit (s[3])? * 0x11,
hex_digit (s[0])? * 0x11,
hex_digit (s[1])? * 0x11,
hex_digit (s[2])? * 0x11))
},
3 => {
// #rgb -> 0xffrrggbb
Ok (0xff000000 |
((hex_digit (s[0])? * 0x11) as u32) << 16 |
((hex_digit (s[1])? * 0x11) as u32) << 8 |
((hex_digit (s[2])? * 0x11) as u32))
Ok (RawColor::new_rgb (hex_digit (s[0])? * 0x11,
hex_digit (s[1])? * 0x11,
hex_digit (s[2])? * 0x11))
}
_ => Err (())
};
match result {
Ok (argb) => Ok (RawColor { argb: argb }),
Err (_) => Err (())
}
}
......@@ -80,10 +93,10 @@ mod tests {
#[test]
fn parses_hash_hex_colors () {
assert_eq! (RawColor::from_str ("#AB10fa20").unwrap (), RawColor { argb: 0x20ab10fa });
assert_eq! (RawColor::from_str ("#10fa20").unwrap (), RawColor { argb: 0xff10fa20 });
assert_eq! (RawColor::from_str ("#abcd").unwrap (), RawColor { argb: 0xddaabbcc });
assert_eq! (RawColor::from_str ("#123").unwrap (), RawColor { argb: 0xff112233 });
assert_eq! (RawColor::from_str ("#AB10fa20").unwrap (), RawColor::new_argb (0x20, 0xab, 0x10, 0xfa));
assert_eq! (RawColor::from_str ("#10fa20").unwrap (), RawColor::new_rgb (0x10, 0xfa, 0x20));
assert_eq! (RawColor::from_str ("#abcd").unwrap (), RawColor::new_argb (0xdd, 0xaa, 0xbb, 0xcc));
assert_eq! (RawColor::from_str ("#123").unwrap (), RawColor::new_rgb (0x11, 0x22, 0x33));
}
#[test]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment