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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
use std::collections::HashMap;
use std::str::FromStr;
#[derive(thiserror::Error, Debug)]
#[allow(clippy::enum_variant_names)]
pub enum ParseError {
#[error("Expected int: {0}")]
ExpectedInt(#[from] std::num::ParseIntError),
#[error("Expected float: {0}")]
ExpectedFloat(#[from] std::num::ParseFloatError),
#[error("Expected enum: {0}")]
ExpectedEnum(#[from] strum::ParseError),
}
use strum::{Display, EnumString};
use xml::attribute::OwnedAttribute;
use crate::components::colours::ColourDisplay::{Gradient, GradientMeter, Meter};
#[derive(Debug)]
pub struct ColourMap {
prefix: String,
selected: Option<u8>,
off_style: ColourOffStyle,
state: Option<ColourState>,
blink: Option<ColourState>,
velocity: Option<i8>,
colour_group: Option<String>,
colour_list: Option<Vec<Option<Colour>>>,
colour_display: Option<ColourDisplay>,
}
impl ColourMap {
pub fn new(prefix: String) -> Self {
Self {
prefix,
selected: None,
off_style: ColourOffStyle::Dimmed,
state: None,
blink: None,
velocity: None,
colour_group: None,
colour_list: None,
colour_display: None,
}
}
pub fn read_colours(&mut self, attribute: &OwnedAttribute) -> Result<bool, ParseError> {
let mut attr_key = format!("{}offStyle", &self.prefix);
if attribute.name.local_name == attr_key {
self.off_style = ColourOffStyle::from_str(&attribute.value)?;
return Ok(true);
}
attr_key = format!("{}selected", &self.prefix);
if attribute.name.local_name == attr_key {
self.selected = Option::Some(u8::from_str(attribute.value.as_str())?);
return Ok(true);
}
attr_key = format!("{}velocity", &self.prefix);
if attribute.name.local_name == attr_key {
self.velocity = Option::Some(i8::from_str(attribute.value.as_str())?);
return Ok(true);
}
attr_key = format!("{}state", &self.prefix);
if attribute.name.local_name == attr_key {
self.state = Some(ColourState::from_str(&attribute.value)?);
return Ok(true);
}
attr_key = format!("{}blink", &self.prefix);
if attribute.name.local_name == attr_key {
self.blink = Some(ColourState::from_str(&attribute.value)?);
return Ok(true);
}
if attribute.name.local_name == "colorGroup" {
self.colour_group = Option::Some(attribute.value.clone());
return Ok(true);
}
attr_key = format!("{}colour", &self.prefix);
if attribute.name.local_name.starts_with(attr_key.as_str()) {
let color_list = self.colour_list.get_or_insert_with(|| {
let mut default = Vec::new();
default.resize_with(3, || None);
default
});
if let Some(index) = attribute
.name
.local_name
.chars()
.last()
.map(|s| usize::from_str(&s.to_string()))
.transpose()?
{
color_list[index] = Option::Some(Colour::new(&attribute.value)?);
}
return Ok(true);
}
attr_key = format!("{}Display", &self.prefix);
if attribute.name.local_name == attr_key {
self.colour_display = Option::Some(ColourDisplay::from_str(&attribute.value)?);
return Ok(true);
}
Ok(false)
}
pub fn write_colours(&self, attributes: &mut HashMap<String, String>) {
let mut key = format!("{}offStyle", self.prefix);
attributes.insert(key, self.off_style.to_string());
if let Some(selected) = self.selected {
attributes.insert(format!("{}selected", self.prefix), format!("{}", selected));
}
if let Some(velocity) = &self.velocity {
key = format!("{}velocity", self.prefix);
attributes.insert(key, format!("{}", velocity));
}
if let Some(state) = &self.state {
key = format!("{}state", self.prefix);
attributes.insert(key, state.to_string());
}
if let Some(blink) = &self.blink {
key = format!("{}blink", self.prefix);
attributes.insert(key, blink.to_string());
}
if let Some(colour_group) = &self.colour_group {
let colour = colour_group.to_string();
attributes.insert("colorGroup".to_string(), colour);
}
if let Some(vector) = &self.colour_list {
for i in 0..3 {
if let Some(Some(colour)) = vector.get(i) {
key = format!("{}colour{}", self.prefix, i);
attributes.insert(key, colour.to_rgba());
}
}
}
if let Some(colour_display) = &self.colour_display {
key = format!("{}Display", self.prefix);
attributes.insert(key, colour_display.to_string());
}
}
pub fn colour(&self, index: u8) -> &Colour {
self.colour_list.as_ref().unwrap()[index as usize]
.as_ref()
.unwrap()
}
pub fn get_off_style(&self) -> &ColourOffStyle {
&self.off_style
}
pub fn is_fader_gradient(&self) -> bool {
if self.colour_display.is_none() {
return false;
}
if self.colour_display.as_ref().unwrap() == &Gradient {
return true;
}
if self.colour_display.as_ref().unwrap() == &GradientMeter {
return true;
}
return false;
}
pub fn is_fader_meter(&self) -> bool {
if self.colour_display.is_none() {
return false;
}
if self.colour_display.as_ref().unwrap() == &Meter {
return true;
}
if self.colour_display.as_ref().unwrap() == &GradientMeter {
return true;
}
return false;
}
pub fn set_fader_display(&mut self, display: ColourDisplay) {
self.colour_display = Some(display);
}
pub fn state(&self) -> &Option<ColourState> {
&self.state
}
pub fn get_state(&self) -> bool {
if let Some(state) = &self.state {
return state == &ColourState::On;
}
false
}
pub fn blink(&self) -> &Option<ColourState> {
&self.blink
}
pub fn set_state(&mut self, state: Option<ColourState>) {
self.state = state;
}
pub fn set_state_on(&mut self, on: bool) {
if on {
self.state = Some(ColourState::On);
} else {
self.state = Some(ColourState::Off);
}
}
pub fn set_blink(&mut self, blink: Option<ColourState>) {
self.blink = blink;
}
pub fn set_blink_on(&mut self, on: bool) {
if on {
self.blink = Some(ColourState::On);
} else {
self.blink = Some(ColourState::Off);
}
}
pub fn set_colour(&mut self, index: usize, input: Colour) {
if let Some(colour) = &mut self.colour_list {
colour[index] = Some(input);
} else {
let mut default = Vec::new();
default.resize_with(3, || None);
default[index] = Some(input);
self.colour_list = Some(default);
}
}
pub fn set_off_style(&mut self, off_style: ColourOffStyle) {
self.off_style = off_style;
}
}
#[derive(Debug, PartialEq, EnumString, Display)]
pub enum ColourOffStyle {
#[strum(to_string = "DIMMED")]
Dimmed,
#[strum(to_string = "COLOUR2")]
Colour2,
#[strum(to_string = "DIMMEDCOLOUR2")]
DimmedColour2,
}
#[derive(Debug, PartialEq, EnumString, Display)]
pub enum ColourDisplay {
#[strum(to_string = "GRADIENT")]
Gradient,
#[strum(to_string = "METER")]
Meter,
#[strum(to_string = "GRADIENT_METER")]
GradientMeter,
#[strum(to_string = "TWO COLOR")]
TwoColour,
}
#[derive(Debug, EnumString, PartialEq, Display)]
pub enum ColourState {
#[strum(to_string = "0")]
Off,
#[strum(to_string = "1")]
On,
}
#[derive(Debug)]
pub struct Colour {
red: u8,
green: u8,
blue: u8,
alpha: u8,
}
impl Colour {
pub fn new(rgba: &str) -> Result<Self, ParseError> {
Ok(Self {
red: u8::from_str_radix(&rgba[0..2], 16)?,
green: u8::from_str_radix(&rgba[2..4], 16)?,
blue: u8::from_str_radix(&rgba[4..6], 16)?,
alpha: u8::from_str_radix(&rgba[6..8], 16)?,
})
}
pub fn fromrgb(rgb: &str) -> Result<Self, ParseError> {
Ok(Self {
red: u8::from_str_radix("00", 16)?,
green: u8::from_str_radix(&rgb[0..2], 16)?,
blue: u8::from_str_radix(&rgb[2..4], 16)?,
alpha: u8::from_str_radix(&rgb[4..6], 16)?,
})
}
pub fn to_rgba(&self) -> String {
return format!(
"{:02X}{:02X}{:02X}{:02X}",
self.red, self.green, self.blue, self.alpha
);
}
pub fn to_reverse_bytes(&self) -> [u8; 4] {
[self.alpha, self.blue, self.green, self.red]
}
}