[librsvg] RawColor: Add new_rgb() and new_argb() constructors to make callers nicer
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg] RawColor: Add new_rgb() and new_argb() constructors to make callers nicer
- Date: Thu, 18 May 2017 21:42:05 +0000 (UTC)
commit 81bbfe8e5a9815bda2f63d694900021a3c3d8a80
Author: Federico Mena Quintero <federico gnome org>
Date: Thu May 18 13:13:18 2017 -0500
RawColor: Add new_rgb() and new_argb() constructors to make callers nicer
rust/src/color.rs | 65 +++++++++++++++++++++++++++++++---------------------
1 files changed, 39 insertions(+), 26 deletions(-)
---
diff --git a/rust/src/color.rs b/rust/src/color.rs
index 62a8522..6ac2ee1 100644
--- a/rust/src/color.rs
+++ b/rust/src/color.rs
@@ -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]
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]