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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use embedded_graphics_core::pixelcolor::{
Bgr555, Bgr565, Bgr888, BinaryColor, Gray2, Gray4, Gray8, GrayColor, Rgb555, Rgb565, Rgb888,
RgbColor, WebColors,
};
pub trait ColorMapping: Into<Rgb888> {
const NONE_COLOR: Rgb888 = Rgb888::new(128, 128, 128);
fn char_to_color(c: char) -> Self;
fn color_to_char(color: Self) -> char;
}
impl ColorMapping for BinaryColor {
fn char_to_color(c: char) -> Self {
match c {
'.' => BinaryColor::Off,
'#' => BinaryColor::On,
_ => panic!("Invalid char in pattern: '{}'", c),
}
}
fn color_to_char(color: Self) -> char {
match color {
BinaryColor::Off => '.',
BinaryColor::On => '#',
}
}
}
macro_rules! impl_gray_color_mapping {
($type:ident, $radix:expr) => {
impl ColorMapping for $type {
const NONE_COLOR: Rgb888 = Rgb888::CSS_STEEL_BLUE;
fn char_to_color(c: char) -> Self {
if let Some(digit) = c.to_digit($radix) {
Self::new(digit as u8)
} else {
panic!("invalid char in pattern: '{}'", c)
}
}
fn color_to_char(color: Self) -> char {
core::char::from_digit(color.luma() as u32, $radix)
.unwrap()
.to_ascii_uppercase()
}
}
};
}
impl_gray_color_mapping!(Gray2, 4);
impl_gray_color_mapping!(Gray4, 16);
impl ColorMapping for Gray8 {
const NONE_COLOR: Rgb888 = Rgb888::CSS_STEEL_BLUE;
fn char_to_color(c: char) -> Self {
if let Some(digit) = c.to_digit(16) {
Self::new(digit as u8 * 0x11)
} else {
panic!("invalid char in pattern: '{}'", c);
}
}
fn color_to_char(color: Self) -> char {
let luma = color.luma();
let lower = luma & 0xF;
let upper = luma >> 4;
if lower != upper {
'?'
} else {
core::char::from_digit(lower as u32, 16)
.unwrap()
.to_ascii_uppercase()
}
}
}
macro_rules! impl_rgb_color_mapping {
($type:ident) => {
impl ColorMapping for $type {
fn char_to_color(c: char) -> Self {
match c {
'K' => Self::BLACK,
'R' => Self::RED,
'G' => Self::GREEN,
'B' => Self::BLUE,
'Y' => Self::YELLOW,
'M' => Self::MAGENTA,
'C' => Self::CYAN,
'W' => Self::WHITE,
_ => panic!("Invalid char in pattern: '{}'", c),
}
}
fn color_to_char(color: Self) -> char {
match color {
Self::BLACK => 'K',
Self::RED => 'R',
Self::GREEN => 'G',
Self::BLUE => 'B',
Self::YELLOW => 'Y',
Self::MAGENTA => 'M',
Self::CYAN => 'C',
Self::WHITE => 'W',
_ => '?',
}
}
}
};
}
impl_rgb_color_mapping!(Rgb555);
impl_rgb_color_mapping!(Bgr555);
impl_rgb_color_mapping!(Rgb565);
impl_rgb_color_mapping!(Bgr565);
impl_rgb_color_mapping!(Rgb888);
impl_rgb_color_mapping!(Bgr888);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn gray2_mapping() {
for luma in 0..4 {
let color = Gray2::new(luma);
assert_eq!(color, Gray2::char_to_color(Gray2::color_to_char(color)));
}
}
#[test]
fn gray4_mapping() {
for luma in 0..16 {
let color = Gray4::new(luma);
assert_eq!(color, Gray4::char_to_color(Gray4::color_to_char(color)));
}
}
#[test]
fn gray8_mapping() {
for luma in 0..16 {
let color = Gray8::new(luma * 0x11);
assert_eq!(color, Gray8::char_to_color(Gray8::color_to_char(color)));
}
}
#[test]
#[should_panic(expected = "invalid char in pattern: '4'")]
fn invalid_gray2_char_4() {
Gray2::char_to_color('4');
}
#[test]
#[should_panic(expected = "invalid char in pattern: 'A'")]
fn invalid_gray2_char_a() {
Gray2::char_to_color('A');
}
#[test]
#[should_panic(expected = "invalid char in pattern: 'G'")]
fn invalid_gray4_char_g() {
Gray2::char_to_color('G');
}
#[test]
#[should_panic(expected = "invalid char in pattern: 'G'")]
fn invalid_gray8_char_g() {
Gray8::char_to_color('G');
}
}