Skip to content

Commit 14a05b2

Browse files
committed
Rename colors to reduce redundancy and add more documentation
1 parent 235163a commit 14a05b2

File tree

5 files changed

+85
-75
lines changed

5 files changed

+85
-75
lines changed

gnuplot/examples/color.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ fn example(c: Common) {
99

1010
let mut fg = Figure::new();
1111
let ax = fg.axes2d();
12-
ax.set_title("Demo of RGBColor in various forms", &[]);
12+
ax.set_title("Demo of RGBString in various forms", &[]);
1313
ax.set_legend(Graph(0.5), Graph(0.9), &[], &[]);
1414
let colors = [
1515
Color("black"),
16-
Color(ColorType::RGBColor("black")),
16+
Color(ColorType::RGBString("black")),
1717
Color("red"),
18-
Color(ColorType::RGBColor("#ff0000")), // red using Hex coded RRGGBB
19-
Color(ColorType::RGBColor("#ff8888")), // pink using Hex coded RRGGBB
20-
Color(ColorType::RGBColor("#88ff0000")), // pink using Hex coded AARRGGBB
21-
Color(ColorType::RGBColor("#ff0000")), // red using Hex coded RRGGBB
18+
Color(ColorType::RGBString("#ff0000")), // red using Hex coded RRGGBB
19+
Color(ColorType::RGBString("#ff8888")), // pink using Hex coded RRGGBB
20+
Color(ColorType::RGBString("#88ff0000")), // pink using Hex coded AARRGGBB
21+
Color(ColorType::RGBString("#ff0000")), // red using Hex coded RRGGBB
2222
];
2323

2424
for (i, color) in colors.into_iter().enumerate() {
@@ -30,7 +30,6 @@ fn example(c: Common) {
3030
}
3131

3232
c.show(&mut fg, "rgb_color");
33-
3433
}
3534

3635
fn main() {

gnuplot/examples/example1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ fn example(c: Common)
307307
MarkerSymbol('*'),
308308
TextAlign(AlignCenter),
309309
TextOffset(0.0, -1.0),
310-
MarkerColor(RGBColor("red")),
310+
MarkerColor(RGBString("red")),
311311
MarkerSize(2.0),
312312
],
313313
);

gnuplot/examples/variable_color.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ static CANDLESTICKS_STR: &str = "1 1.5 2 2.4 4 6.
1919
10 2.7 3 3.5 4 4.3";
2020

2121
fn example(c: Common) {
22-
2322
let data: Vec<Vec<f64>> = CANDLESTICKS_STR
2423
.split("\n")
2524
.map(|line| {
@@ -63,9 +62,9 @@ fn example(c: Common) {
6362
// input value for each data point. The palette is scaled to the maximum value in the vector of `f64`s passed
6463
// to the VariablePaletteColor.
6564
//
66-
// The third color loop uses an (implicit) VariableARGBColor. The `Vec<(u8, u8, u8, u8)>` needed to constcruct the color
67-
// is calculated in this case by the `argb_formula()` closure. An explicit VariableARGBColor could also be constructed using
68-
// `ColorOpt(ColorType::VariableARGBColor(data)``. A VariableRGBColor is also defined that takes a 3-tuple of u8, rather than
65+
// The third color loop uses an (implicit) VariableARGBString. The `Vec<(u8, u8, u8, u8)>` needed to constcruct the color
66+
// is calculated in this case by the `argb_formula()` closure. An explicit VariableARGBString could also be constructed using
67+
// `ColorOpt(ColorType::VariableARGBString(data)``. A VariableRGBString is also defined that takes a 3-tuple of u8, rather than
6968
// a 4 tuple.
7069
for (color, label) in [
7170
(Color(row_index.clone()), "VariableIndexColor"),
@@ -77,7 +76,7 @@ fn example(c: Common) {
7776
),
7877
(
7978
Color(d1.iter().map(argb_formula).collect::<Vec<_>>()),
80-
"VariableARGBColor",
79+
"VariableARGBString",
8180
),
8281
] {
8382
let mut fg = Figure::new();
@@ -96,7 +95,7 @@ fn example(c: Common) {
9695
&[
9796
color.clone(),
9897
FillAlpha(0.5),
99-
BorderColor(RGBColor("black")),
98+
BorderColor(RGBString("black")),
10099
],
101100
);
102101
ax.points(&d1, iter::repeat(1), &[color.clone(), PointSymbol('D')]);
@@ -117,7 +116,7 @@ fn example(c: Common) {
117116
iter::repeat(10),
118117
d1.iter().map(by3),
119118
d1.iter().map(by4),
120-
&[color.clone(), BorderColor(RGBColor("black"))],
119+
&[color.clone(), BorderColor(RGBString("black"))],
121120
);
122121

123122
c.show(&mut fg, "variable_color");
@@ -162,7 +161,7 @@ fn example(c: Common) {
162161
&[
163162
Color(SavedColorMap("magma", color_values.clone())),
164163
FillAlpha(0.5),
165-
BorderColor(RGBColor("black")),
164+
BorderColor(RGBString("black")),
166165
],
167166
);
168167
ax.points(
@@ -199,7 +198,7 @@ fn example(c: Common) {
199198
d1.iter().map(by4),
200199
&[
201200
Color(SavedColorMap("hot", color_values.clone())),
202-
BorderColor(RGBColor("black")),
201+
BorderColor(RGBString("black")),
203202
],
204203
);
205204

gnuplot/src/axes_common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ impl AxisData
680680
w.write_str(self.axis.get_axis_str());
681681
w.write_str("zeroaxis ");
682682

683-
AxesCommonData::write_color_options(w, &self.options, Some(ColorType::RGBColor("black".into())));
683+
AxesCommonData::write_color_options(w, &self.options, Some(ColorType::RGBString("black".into())));
684684
AxesCommonData::write_line_options(w, &self.options, version);
685685
}
686686
else

gnuplot/src/color.rs

Lines changed: 69 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ pub type ARGBInts = (
2020

2121
/// Option type (for lines, axes, and text) that allows the various different gnuplot
2222
/// color formats. The gnuplot [colorspec reference](http://gnuplot.info/docs_6.0/loc3640.html)
23-
/// also explains these
23+
/// also explains these.
2424
///
2525
/// There are equivalent many ways of specifying colors, and this allows the user to chose the most convenient.
2626
/// for example, all the following will produce the same blue color:
2727
/// `RGBColor("blue")`, `RGBColor("0x0000ff")`, `RGBColor("#0000ff")`, `RGBColor("0x000000ff")`,
2828
/// `RGBColor("#000000ff")`, `RGBIntegerColor(0, 0, 255)`, `ARGBColor(0, 0, 0, 255)`,
2929
///
30-
/// See example usage of this in `color.rs` in the
30+
/// See example usages of these colors in `color.rs`, `variable_color.rs` in the
3131
/// [Examples folder](https://github.com/SiegeLord/RustGnuplot/tree/master/gnuplot/examples) on Github
3232
#[derive(Debug, Clone, PartialEq, PartialOrd)]
3333
pub enum ColorType<T = String> {
@@ -42,35 +42,47 @@ pub enum ColorType<T = String> {
4242
/// "#AARRGGBB" represents an RGB color with an alpha channel (transparency) value in the high bits.
4343
/// An alpha value of 0 represents a fully opaque color; i.e., "#00RRGGBB" is the same as "#RRGGBB".
4444
/// An alpha value of 255 (FF) represents full transparency.
45-
RGBColor(T),
45+
RGBString(T),
4646
/// tuple of u8 representing red, green and blue values as 0-255
47-
RGBIntegerColor(ColorComponent, ColorComponent, ColorComponent),
47+
RGBInteger(ColorComponent, ColorComponent, ColorComponent),
4848
/// tuple of u8 representing alpha, red, green and blue values as 0-255.
4949
/// As with `RGBColor`, an alpha value of 0 represents a fully opaque color;
5050
/// an alpha value of 255 (FF) represents full transparency.
51-
ARGBIntegerColor(
51+
ARGBInteger(
5252
ColorComponent,
5353
ColorComponent,
5454
ColorComponent,
5555
ColorComponent,
5656
),
57-
VariableRGBColor(Vec<RGBInts>),
58-
VariableARGBColor(Vec<ARGBInts>),
57+
/// Vector of tuples of `u8` (as per `RGBColor`), but instead of a single color for the whole
58+
/// plot, the vector should contain a separte color for each data point.
59+
VariableRGBIntegers(Vec<RGBInts>),
60+
/// Vector of tuples of `u8` (as per `ARGBColor`), but as with `VariableRGBColor`, a separate
61+
/// color value is given for each data point.
62+
VariableARGBIntegers(Vec<ARGBInts>),
63+
/// TODO
5964
PaletteFracColor(f32),
65+
/// TODO
6066
PaletteCBColor(f32),
67+
/// Vector of `f64` values which act as indexes into the current palette to set the color of
68+
/// each data point
6169
VariablePaletteColor(Vec<f64>),
70+
/// Similar to `VariablePaletteColor` in that it takes a `Vec<f64>` to set the indexes into the
71+
/// color map for each data point, but in addition to the color data it takes a string hold the name
72+
/// of the color map to use. This should have been previously created in the workspace using the
73+
/// `axes.create_colormap()` function.
6274
SavedColorMap(T, Vec<f64>),
6375
/// Set the color of all elements of the plot to the `n`th color in the current gnuplot color cycle.
64-
IndexColor(ColorIndex),
76+
Index(ColorIndex),
6577
/// A color type that sets the color per element using a index `n` which represents the `n`th
6678
/// color in the current gnuplot color scheme. In gnuplot this is the last element in the plot command,
6779
/// in Rust gnuplot, the color type takes a vector of u8, where each index is treated the same as the
6880
/// fixed `IndexColor`.
6981
/// This is useful for setting bars/boxes etc to be
70-
/// the same color from multiple plot commands. The `color.rs` example has examples of this usage
71-
VariableIndexColor(Vec<ColorIndex>),
72-
///
73-
BackgroundColor,
82+
/// the same color from multiple plot commands. The `color.rs` example has examples of this usage.
83+
VariableIndex(Vec<ColorIndex>),
84+
/// Set the color of the plot to the current background color.
85+
Background,
7486
/// Fixed black color
7587
Black,
7688
}
@@ -79,51 +91,51 @@ impl<T: Display> ColorType<T> {
7991
/// Returns the gnuplot string that will produce the requested color
8092
pub fn command(&self) -> String {
8193
match self {
82-
RGBColor(s) => format!(r#"rgb "{}""#, s),
83-
RGBIntegerColor(r, g, b) => format!(r#"rgb {}"#, from_argb(0, *r, *g, *b)),
84-
ARGBIntegerColor(a, r, g, b) => format!(r#"rgb {}"#, from_argb(*a, *r, *g, *b)),
85-
VariableRGBColor(_) => String::from("rgb variable"),
86-
VariableARGBColor(_) => String::from("rgb variable"),
94+
RGBString(s) => format!(r#"rgb "{}""#, s),
95+
RGBInteger(r, g, b) => format!(r#"rgb {}"#, from_argb(0, *r, *g, *b)),
96+
ARGBInteger(a, r, g, b) => format!(r#"rgb {}"#, from_argb(*a, *r, *g, *b)),
97+
VariableRGBIntegers(_) => String::from("rgb variable"),
98+
VariableARGBIntegers(_) => String::from("rgb variable"),
8799
PaletteFracColor(_) => todo!(),
88100
PaletteCBColor(_) => todo!(),
89101
VariablePaletteColor(_) => String::from("palette z"),
90102
SavedColorMap(s, _) => format!("palette {s}"),
91-
VariableIndexColor(_) => String::from("variable"),
92-
BackgroundColor => todo!(),
93-
IndexColor(n) => format!("{}", n),
103+
VariableIndex(_) => String::from("variable"),
104+
Background => todo!(),
105+
Index(n) => format!("{}", n),
94106
Black => String::from("black"),
95107
}
96108
}
97109

98110
pub fn data(&self) -> Vec<f64> {
99111
match self {
100-
RGBColor(_) => panic!("data() called on non-variable color type."),
101-
RGBIntegerColor(_, _, _) => panic!("data() called on non-variable color type."),
102-
ARGBIntegerColor(_, _, _, _) => panic!("data() called on non-variable color type."),
103-
VariableRGBColor(items) => items
112+
RGBString(_) => panic!("data() called on non-variable color type."),
113+
RGBInteger(_, _, _) => panic!("data() called on non-variable color type."),
114+
ARGBInteger(_, _, _, _) => panic!("data() called on non-variable color type."),
115+
VariableRGBIntegers(items) => items
104116
.iter()
105117
.map(|(r, g, b)| from_argb(0, *r, *g, *b) as f64)
106118
.collect(),
107-
VariableARGBColor(items) => items
119+
VariableARGBIntegers(items) => items
108120
.into_iter()
109121
.map(|(a, r, g, b)| from_argb(*a, *r, *g, *b) as f64)
110122
.collect(),
111123
PaletteFracColor(_) => panic!("data() called on non-variable color type."),
112124
PaletteCBColor(_) => panic!("data() called on non-variable color type."),
113125
VariablePaletteColor(items) => items.clone(),
114126
SavedColorMap(_, items) => items.clone(),
115-
IndexColor(_) => panic!("data() called on non-variable color type."),
116-
VariableIndexColor(items) => items.into_iter().map(|v| *v as f64).collect(),
117-
BackgroundColor => panic!("data() called on non-variable color type."),
127+
Index(_) => panic!("data() called on non-variable color type."),
128+
VariableIndex(items) => items.into_iter().map(|v| *v as f64).collect(),
129+
Background => panic!("data() called on non-variable color type."),
118130
Black => panic!("data() called on non-variable color type."),
119131
}
120132
}
121133

122134
pub fn is_variable(&self) -> bool {
123135
match self {
124-
VariableRGBColor(_)
125-
| VariableARGBColor(_)
126-
| VariableIndexColor(_)
136+
VariableRGBIntegers(_)
137+
| VariableARGBIntegers(_)
138+
| VariableIndex(_)
127139
| VariablePaletteColor(_)
128140
| SavedColorMap(_, _) => true,
129141
_ => false,
@@ -166,63 +178,63 @@ fn from_argb_floats(a: f64, r: f64, g: f64, b: f64) -> ARGBInts {
166178

167179
impl<'l> Into<ColorType<String>> for &'l str {
168180
fn into(self) -> ColorType<String> {
169-
ColorType::RGBColor(String::from(self))
181+
ColorType::RGBString(String::from(self))
170182
}
171183
}
172184

173185
impl<'l> Into<ColorType<String>> for String {
174186
fn into(self) -> ColorType<String> {
175-
ColorType::RGBColor(self)
187+
ColorType::RGBString(self)
176188
}
177189
}
178190

179191
impl<'l> Into<ColorType<&'l str>> for &'l str {
180192
fn into(self) -> ColorType<&'l str> {
181-
ColorType::RGBColor(self)
193+
ColorType::RGBString(self)
182194
}
183195
}
184196

185197
impl<T> Into<ColorType<T>> for ARGBInts {
186198
fn into(self) -> ColorType<T> {
187-
ColorType::ARGBIntegerColor(self.0, self.1, self.2, self.3)
199+
ColorType::ARGBInteger(self.0, self.1, self.2, self.3)
188200
}
189201
}
190202

191203
impl<T> Into<ColorType<T>> for RGBInts {
192204
fn into(self) -> ColorType<T> {
193-
ColorType::RGBIntegerColor(self.0, self.1, self.2)
205+
ColorType::RGBInteger(self.0, self.1, self.2)
194206
}
195207
}
196208

197209
impl<T> Into<ColorType<T>> for (f64, f64, f64) {
198210
fn into(self) -> ColorType<T> {
199211
let ints = from_rgb_floats(self.0, self.1, self.2);
200-
ColorType::RGBIntegerColor(ints.0, ints.1, ints.2)
212+
ColorType::RGBInteger(ints.0, ints.1, ints.2)
201213
}
202214
}
203215

204216
impl<T> Into<ColorType<T>> for (f64, f64, f64, f64) {
205217
fn into(self) -> ColorType<T> {
206218
let ints = from_argb_floats(self.0, self.1, self.2, self.3);
207-
ColorType::ARGBIntegerColor(ints.0, ints.1, ints.2, ints.3)
219+
ColorType::ARGBInteger(ints.0, ints.1, ints.2, ints.3)
208220
}
209221
}
210222

211223
impl<T> Into<ColorType<T>> for Vec<RGBInts> {
212224
fn into(self) -> ColorType<T> {
213-
ColorType::VariableRGBColor(self)
225+
ColorType::VariableRGBIntegers(self)
214226
}
215227
}
216228

217229
impl<T> Into<ColorType<T>> for Vec<ARGBInts> {
218230
fn into(self) -> ColorType<T> {
219-
ColorType::VariableARGBColor(self)
231+
ColorType::VariableARGBIntegers(self)
220232
}
221233
}
222234

223235
impl<T> Into<ColorType<T>> for Vec<ColorIndex> {
224236
fn into(self) -> ColorType<T> {
225-
ColorType::VariableIndexColor(self)
237+
ColorType::VariableIndex(self)
226238
}
227239
}
228240

@@ -231,39 +243,39 @@ impl<T: Display> OneWayOwned for ColorType<T> {
231243

232244
fn to_one_way_owned(&self) -> ColorType<String> {
233245
match self {
234-
RGBColor(s) => RGBColor(s.to_string()),
235-
RGBIntegerColor(r, g, b) => RGBIntegerColor(*r, *g, *b),
236-
VariableRGBColor(d) => VariableRGBColor(d.clone()),
246+
RGBString(s) => RGBString(s.to_string()),
247+
RGBInteger(r, g, b) => RGBInteger(*r, *g, *b),
248+
VariableRGBIntegers(d) => VariableRGBIntegers(d.clone()),
237249
PaletteFracColor(_) => todo!(),
238250
PaletteCBColor(_) => todo!(),
239251
VariablePaletteColor(d) => VariablePaletteColor(d.clone()),
240252
SavedColorMap(s, d) => SavedColorMap(s.to_string(), d.clone()),
241-
VariableIndexColor(d) => VariableIndexColor(d.clone()),
242-
BackgroundColor => BackgroundColor,
243-
IndexColor(n) => IndexColor(*n),
253+
VariableIndex(d) => VariableIndex(d.clone()),
254+
Background => Background,
255+
Index(n) => Index(*n),
244256
Black => Black,
245-
ARGBIntegerColor(a, r, g, b) => ARGBIntegerColor(*a, *r, *g, *b),
246-
VariableARGBColor(d) => VariableARGBColor(d.clone()),
257+
ARGBInteger(a, r, g, b) => ARGBInteger(*a, *r, *g, *b),
258+
VariableARGBIntegers(d) => VariableARGBIntegers(d.clone()),
247259
}
248260
}
249261
}
250262

251263
impl ColorType<String> {
252264
pub fn to_ref(&self) -> ColorType<&str> {
253265
match self {
254-
RGBColor(s) => RGBColor(s),
255-
RGBIntegerColor(r, g, b) => RGBIntegerColor(*r, *g, *b),
256-
VariableRGBColor(d) => VariableRGBColor(d.to_vec()),
257-
VariableARGBColor(d) => VariableARGBColor(d.to_vec()),
266+
RGBString(s) => RGBString(s),
267+
RGBInteger(r, g, b) => RGBInteger(*r, *g, *b),
268+
VariableRGBIntegers(d) => VariableRGBIntegers(d.to_vec()),
269+
VariableARGBIntegers(d) => VariableARGBIntegers(d.to_vec()),
258270
PaletteFracColor(_) => todo!(),
259271
PaletteCBColor(_) => todo!(),
260272
VariablePaletteColor(d) => VariablePaletteColor(d.to_vec()),
261273
SavedColorMap(s, d) => SavedColorMap(s, d.to_vec()),
262-
VariableIndexColor(d) => VariableIndexColor(d.to_vec()),
263-
BackgroundColor => todo!(),
264-
IndexColor(n) => IndexColor(*n),
274+
VariableIndex(d) => VariableIndex(d.to_vec()),
275+
Background => todo!(),
276+
Index(n) => Index(*n),
265277
Black => Black,
266-
ARGBIntegerColor(a, r, g, b) => ARGBIntegerColor(*a, *r, *g, *b),
278+
ARGBInteger(a, r, g, b) => ARGBInteger(*a, *r, *g, *b),
267279
}
268280
}
269281
}

0 commit comments

Comments
 (0)