Skip to content

Commit ffd11c0

Browse files
etfrogersSiegeLord
authored andcommitted
Add a new flexible way to specify colors.
Fixes #105 #86
1 parent 3cdf52d commit ffd11c0

22 files changed

+1412
-437
lines changed

gnuplot/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ path = "examples/box_xy_error.rs"
6262

6363
[[example]]
6464

65+
name = "color"
66+
path = "examples/color.rs"
67+
68+
[[example]]
69+
70+
name = "variable_color"
71+
path = "examples/variable_color.rs"
72+
73+
[[example]]
74+
6575
name = "lines_3d"
6676
path = "examples/lines_3d.rs"
6777

gnuplot/examples/box_and_whisker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn example(c: Common)
2727
[0.5f32, 0.25, 0.125].iter(),
2828
&[
2929
WhiskerBars(0.5),
30-
Color("blue"),
30+
Color("blue".into()),
3131
LineWidth(2.0),
3232
LineStyle(SmallDot),
3333
FillAlpha(0.5),

gnuplot/examples/box_xy_error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn example(c: Common)
2525
[-1.5f32, 4.5, 3.0].iter(),
2626
[0.5f32, 4.75, 0.125].iter(),
2727
&[
28-
Color("blue"),
28+
Color("blue".into()),
2929
LineWidth(2.0),
3030
LineStyle(SmallDot),
3131
FillAlpha(0.5),

gnuplot/examples/color.rs

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
use std::{fmt::Debug, iter};
2+
3+
// This file is released into Public Domain.
4+
use crate::common::*;
5+
use gnuplot::*;
6+
7+
mod common;
8+
9+
fn color_name<T: Debug>(color: &PlotOption<T>) -> String
10+
{
11+
match color
12+
{
13+
Color(color_type) => format!("{:?}", color_type),
14+
_ => panic!(),
15+
}
16+
}
17+
18+
fn example(c: Common)
19+
{
20+
let x = 0..5;
21+
22+
let colors = [
23+
Color("black".into()), // Conversion to RGBString is implicit
24+
Color(ColorType::RGBString("black")), // Explicit use of RGBString
25+
Color("red".into()), // Conversion to RGBString is implicit
26+
Color(RGBString("#ff0000")), // red using Hex coded RRGGBB
27+
Color(RGBString("#00ff0000")), // red using Hex coded AARRGGBB
28+
Color("#ff8888".into()), // pink using Hex coded RRGGBB. Conversion to RGBString is implict
29+
Color("#88ff0000".into()), // pink using Hex coded AARRGGBB. Conversion to RGBString is implict
30+
Color(ColorType::RGBString("#ffff0000")), // transparent using Hex coded AARRGGBB
31+
Color((128, 0, 255).into()), // purple using implict RGBInteger
32+
Color(RGBInteger(128, 0, 255)), // purple using explict RGBInteger
33+
Color((0.5, 0.0, 1.0).try_into().unwrap()), // purple using implict float to int conversion
34+
Color((128, 128, 0, 255).into()), // pale purple using implict ARGBInteger
35+
Color(ARGBInteger(128, 128, 0, 255)), // pale purple using explict ARGBInteger
36+
Color((0.5, 0.5, 0.0, 1.0).try_into().unwrap()), // pale purple using implict float to int conversion
37+
];
38+
39+
let mut fg = Figure::new();
40+
let ax = fg.axes2d();
41+
ax.set_title(
42+
"Demo of RGBString in various forms\nSee code comments for how to construct the colors",
43+
&[],
44+
)
45+
.set_x_range(Fix(-9.0), Auto)
46+
.set_legend(Graph(0.5), Graph(0.9), &[], &[Font("", 14.0)]);
47+
48+
let n_colors = colors.len();
49+
for (i, color) in colors.into_iter().enumerate()
50+
{
51+
ax.box_xy_error_delta(
52+
x.clone(),
53+
iter::repeat((n_colors - 1) - i),
54+
iter::repeat(0.4),
55+
iter::repeat(0.2),
56+
&[
57+
Caption(&color_name(&color)),
58+
LineWidth(1.0),
59+
BorderColor("black".into()),
60+
color,
61+
],
62+
);
63+
}
64+
65+
// Draw line across the boxes in fixed black and background colors
66+
ax.lines(
67+
[0, 0],
68+
[0, n_colors - 1],
69+
&[
70+
LineWidth(7.0),
71+
Color(Black),
72+
Caption(&color_name::<String>(&Color(Black))),
73+
],
74+
);
75+
76+
ax.lines(
77+
[4, 4],
78+
[0, n_colors - 1],
79+
&[
80+
LineWidth(7.0),
81+
Color(Background),
82+
Caption(&color_name::<String>(&Color(Background))),
83+
],
84+
);
85+
86+
// any of the forms used for Color can also be used with TextColor and BorderColor
87+
ax.set_x_label(
88+
"Labels can be colored using the TextColor function",
89+
&[TextColor((128, 0, 255).into())],
90+
);
91+
92+
c.show(&mut fg, "rgb_color");
93+
94+
// ########################################################################
95+
96+
let mut fg = Figure::new();
97+
let ax = fg.axes2d();
98+
let max_cb = 10.0;
99+
ax.set_cb_range(Fix(0.0), Fix(max_cb));
100+
for color_value in 0..=10
101+
{
102+
let color_float = color_value as f64;
103+
let frac_color = Color(PaletteFracColor(color_float / max_cb));
104+
let cb_range_color = Color(PaletteCBColor(color_float));
105+
106+
ax.box_xy_error_delta(
107+
[color_value],
108+
[0],
109+
[0.4],
110+
[0.4],
111+
&[
112+
Caption(&color_name(&frac_color)),
113+
LineWidth(1.0),
114+
BorderColor("black".into()),
115+
frac_color,
116+
],
117+
)
118+
.box_xy_error_delta(
119+
[color_value],
120+
[1],
121+
[0.4],
122+
[0.4],
123+
&[
124+
Caption(&color_name(&cb_range_color)),
125+
LineWidth(1.0),
126+
BorderColor("black".into()),
127+
cb_range_color,
128+
],
129+
);
130+
}
131+
ax.set_x_range(Fix(-10.0), Fix(11.0))
132+
.set_y_range(Fix(-0.5), Fix(1.5))
133+
.set_legend(Graph(0.45), Graph(0.9), &[], &[Font("", 14.0)]);
134+
c.show(&mut fg, "palette_colors");
135+
}
136+
137+
fn main()
138+
{
139+
Common::new().map(|c| example(c));
140+
}

gnuplot/examples/dash_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn example(c: Common)
2222
x.clone().map(|v| v * 2 + 2 * i),
2323
&[
2424
LineWidth(2.),
25-
Color("black"),
25+
Color("black".into()),
2626
LineStyle(dt),
2727
Caption(&format!("{:?}", dt)),
2828
],

0 commit comments

Comments
 (0)