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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use crate::pixelcolor::PixelColor;
const RED_MASK: u16 = 0b11111_000000_00000;
const GREEN_MASK: u16 = 0b00000_111111_00000;
const BLUE_MASK: u16 = 0b00000_000000_11111;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rgb565(pub u16);
impl Rgb565 {
pub fn r(&self) -> u8 {
((self.0 & RED_MASK) >> 8) as u8
}
pub fn g(&self) -> u8 {
((self.0 & GREEN_MASK) >> 3) as u8
}
pub fn b(&self) -> u8 {
((self.0 & BLUE_MASK) << 3) as u8
}
}
impl PixelColor for Rgb565 {}
impl From<u8> for Rgb565 {
fn from(other: u8) -> Self {
Self::from((other, other, other))
}
}
impl From<u16> for Rgb565 {
fn from(other: u16) -> Self {
Self(other)
}
}
impl From<(u8, u8, u8)> for Rgb565 {
fn from((r, g, b): (u8, u8, u8)) -> Self {
let value =
(((r as u16) << 8) & RED_MASK)
| (((g as u16) << 3) & GREEN_MASK)
| ((b as u16) >> 3 & BLUE_MASK);
Self(value)
}
}
impl From<[u8; 2]> for Rgb565 {
fn from(other: [u8; 2]) -> Self {
Self(u16::from_be_bytes(other))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_u8() {
assert_eq!(Rgb565::from(0u8), Rgb565(0));
assert_eq!(Rgb565::from(0xffu8), Rgb565(0xffff));
assert_eq!(Rgb565::from(0b0000_1111u8), Rgb565(0b00001_000011_00001));
assert_eq!(Rgb565::from(0b1010_1010u8), Rgb565(0b10101_101010_10101));
}
#[test]
fn from_u16() {
assert_eq!(Rgb565::from(0xffu16), Rgb565(0x00ff));
assert_eq!(Rgb565::from(0xffffu16), Rgb565(0xffff));
assert_eq!(Rgb565::from(0xababu16), Rgb565(0xabab));
}
#[test]
fn from_tuple() {
assert_eq!(Rgb565::from((0xff, 0xff, 0xff)), Rgb565(0xffff));
assert_eq!(
Rgb565::from((0xff, 0x0f, 0b0101_0101)),
Rgb565(0b11111_000011_01010)
);
}
#[test]
fn from_be_bytes() {
assert_eq!(Rgb565::from([0xff, 0x00]), Rgb565(0xff00));
assert_eq!(Rgb565::from([0xab, 0xcd]), Rgb565(0xabcd));
}
#[test]
fn accessors() {
let p = Rgb565(0b11001_010101_10010);
assert_eq!(p.r(), 0b11001_000);
assert_eq!(p.g(), 0b010101_00);
assert_eq!(p.b(), 0b10010_000);
}
}