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
|
use crate::dot::DotString;
use std::borrow::Cow;
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Color<'a> {
RGB {
red: u8,
green: u8,
blue: u8,
},
RGBA {
red: u8,
green: u8,
blue: u8,
alpha: u8,
},
// TODO: constrain?
// Hue-Saturation-Value (HSV) 0.0 <= H,S,V <= 1.0
HSV {
hue: f32,
saturation: f32,
value: f32,
},
Named(&'a str),
}
impl<'a> DotString<'a> for Color<'a> {
fn dot_string(&self) -> Cow<'a, str> {
match self {
Color::RGB { red, green, blue } => {
format!("#{:02x?}{:02x?}{:02x?}", red, green, blue).into()
}
Color::RGBA {
red,
green,
blue,
alpha,
} => {
format!("#{:02x?}{:02x?}{:02x?}{:02x?}", red, green, blue, alpha).into()
}
Color::HSV {
hue,
saturation,
value,
} => format!("{} {} {}", hue, saturation, value).into(),
Color::Named(color) => (*color).into(),
}
}
}
// The sum of the optional weightings must sum to at most 1.
pub struct WeightedColor<'a> {
pub color: Color<'a>,
// TODO: constrain
/// Must be in range 0 <= W <= 1.
pub weight: Option<f32>,
}
impl<'a> DotString<'a> for WeightedColor<'a> {
fn dot_string(&self) -> Cow<'a, str> {
let mut dot_string = self.color.dot_string().to_string();
if let Some(weight) = &self.weight {
dot_string.push_str(format!(";{}", weight).as_str());
}
dot_string.into()
}
}
pub struct ColorList<'a> {
pub colors: Vec<WeightedColor<'a>>,
}
impl<'a> DotString<'a> for ColorList<'a> {
/// A colon-separated list of weighted color values: WC(:WC)* where each WC has the form C(;F)?
/// Ex: fillcolor=yellow;0.3:blue
fn dot_string(&self) -> Cow<'a, str> {
let mut dot_string = String::new();
let mut iter = self.colors.iter();
let first = iter.next();
if first.is_none() {
return dot_string.into();
}
dot_string.push_str(&first.unwrap().dot_string());
for weighted_color in iter {
dot_string.push_str(":");
dot_string.push_str(&weighted_color.dot_string())
}
dot_string.into()
}
}
/// Convert an element like `(Color, Option<f32>)` into a WeightedColor
pub trait IntoWeightedColor<'a> {
fn into_weighted_color(self) -> WeightedColor<'a>;
}
impl<'a> IntoWeightedColor<'a> for &(Color<'a>, Option<f32>) {
fn into_weighted_color(self) -> WeightedColor<'a> {
let (s, t) = *self;
WeightedColor {
color: s,
weight: t,
}
}
}
#[cfg(test)]
mod test {
use crate::attributes::{Color, ColorList, WeightedColor};
use crate::DotString;
#[test]
fn colorlist_dot_string() {
let yellow = WeightedColor {
color: Color::Named("yellow"),
weight: Some(0.3),
};
let blue = WeightedColor {
color: Color::Named("blue"),
weight: None,
};
let color_list = ColorList {
colors: vec![yellow, blue],
};
let dot_string = color_list.dot_string();
assert_eq!("yellow;0.3:blue", dot_string);
}
#[test]
fn color_rbg_dot_string() {
let color = Color::RGB {
red: 160,
green: 82,
blue: 45,
};
assert_eq!("#a0522d", color.dot_string());
}
#[test]
fn color_rbga_dot_string() {
let color = Color::RGBA {
red: 160,
green: 82,
blue: 45,
alpha: 10,
};
assert_eq!("#a0522d0a", color.dot_string());
}
#[test]
fn color_hsv_dot_string() {
let color = Color::HSV {
hue: 0.051,
saturation: 0.718,
value: 0.627,
};
assert_eq!("0.051 0.718 0.627", color.dot_string());
}
}
|