1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#![deny(missing_docs)]
use std::str::Chars;
pub fn char_to_hex(ch: char) -> Option<u8> {
Some(match ch {
'0' => 0,
'1' => 1,
'2' => 2,
'3' => 3,
'4' => 4,
'5' => 5,
'6' => 6,
'7' => 7,
'8' => 8,
'9' => 9,
'A' | 'a' => 10,
'B' | 'b' => 11,
'C' | 'c' => 12,
'D' | 'd' => 13,
'E' | 'e' => 14,
'F' | 'f' => 15,
_ => { return None; }
})
}
pub fn hex(chars: &mut Chars) -> Option<u8> {
match chars.next() {
None => { return None; }
Some(ch) => char_to_hex(ch)
}
}
pub fn hex_pair(chars: &mut Chars) -> Option<u8> {
let h1 = match hex(chars) {
None => { return None; }
Some(h1) => h1
};
let h2 = match hex(chars) {
None => { return None; }
Some(h2) => h2
};
Some((h1 << 4) | h2)
}
pub fn rgb(chars: &mut Chars) -> Option<[u8; 3]> {
let red = match hex_pair(chars) {
None => { return None; }
Some(x) => x
};
let green = match hex_pair(chars) {
None => { return None; }
Some(x) => x
};
let blue = match hex_pair(chars) {
None => { return None; }
Some(x) => x
};
Some([red, green, blue])
}
pub fn rgba(chars: &mut Chars) -> Option<[u8; 4]> {
let red = match hex_pair(chars) {
None => { return None; }
Some(x) => x
};
let green = match hex_pair(chars) {
None => { return None; }
Some(x) => x
};
let blue = match hex_pair(chars) {
None => { return None; }
Some(x) => x
};
let alpha = match hex_pair(chars) {
None => { return None; }
Some(x) => x
};
Some([red, green, blue, alpha])
}
pub fn rgb_maybe_a(chars: &mut Chars) -> Option<([u8; 3], Option<u8>)> {
let rgb = match rgb(chars) {
None => { return None; }
Some(x) => x
};
let a = match hex_pair(chars) {
None => { return Some((rgb, None)); }
Some(x) => x
};
Some((rgb, Some(a)))
}