Skip to content

Commit 9be4064

Browse files
committed
Complete color example and modify common code to allow transpareny to work
1 parent 14a05b2 commit 9be4064

File tree

4 files changed

+108
-29
lines changed

4 files changed

+108
-29
lines changed

gnuplot/examples/color.rs

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,87 @@
1+
use std::{fmt::Debug, iter};
2+
13
// This file is released into Public Domain.
24
use crate::common::*;
35
use gnuplot::*;
46

57
mod common;
68

9+
fn color_name<T: Debug>(color: &PlotOption<T>) -> String{
10+
let s = format!("{:?}", color).replace("ColorOpt(", "");
11+
let mut chars = s.chars();
12+
chars.next_back();
13+
chars.as_str().to_string()
14+
}
15+
716
fn example(c: Common) {
8-
let x = 0..10;
17+
let x = 0..5;
918

10-
let mut fg = Figure::new();
11-
let ax = fg.axes2d();
12-
ax.set_title("Demo of RGBString in various forms", &[]);
13-
ax.set_legend(Graph(0.5), Graph(0.9), &[], &[]);
1419
let colors = [
15-
Color("black"),
16-
Color(ColorType::RGBString("black")),
17-
Color("red"),
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
20+
Color("black"), // Conversion to RGBString is implicit
21+
Color(ColorType::RGBString("black")), // Explicit use of RGBString
22+
Color("red"), // Conversion to RGBString is implicit
23+
Color(RGBString("#ff0000")), // red using Hex coded RRGGBB
24+
Color(RGBString("#00ff0000")), // red using Hex coded AARRGGBB
25+
Color("#ff8888"), // pink using Hex coded RRGGBB. Conversion to RGBString is implict
26+
Color("#88ff0000"), // pink using Hex coded AARRGGBB. Conversion to RGBString is implict
27+
Color(ColorType::RGBString("#ffff0000")), // transparent using Hex coded AARRGGBB
28+
Color((128, 0, 255)), // purple using implict RGBInteger
29+
Color(RGBInteger(128, 0, 255)), // purple using explict RGBInteger
30+
Color((0.5, 0.0, 1.0)), // purple using implict float to int conversion
31+
Color(floats_to_rgb(0.5, 0.0, 1.0)), // purple using explicit float to int conversion
32+
Color((128, 128, 0, 255)), // pale purple using implict ARGBInteger
33+
Color(ARGBInteger(128, 128, 0, 255)), // pale purple using explict ARGBInteger
34+
Color((0.5, 0.5, 0.0, 1.0)), // pale purple using implict float to int conversion
35+
Color(floats_to_argb(0.5, 0.5, 0.0, 1.0)), // pale purple using explicit float to int conversion
2236
];
2337

38+
let mut fg = Figure::new();
39+
let ax = fg.axes2d();
40+
ax.set_title(
41+
"Demo of RGBString in various forms\nSee code comments for how to construct the colors",
42+
&[],
43+
);
44+
ax.set_x_range(Fix(-10.0), Auto);
45+
ax.set_legend(Graph(0.6), Graph(0.8), &[], &[Font("Arial", 14.0)]);
46+
47+
48+
let n_colors = colors.len();
2449
for (i, color) in colors.into_iter().enumerate() {
25-
ax.lines_points(
50+
ax.box_xy_error_delta(
2651
x.clone(),
27-
x.clone().map(|v| v * 2 + i),
28-
&[Caption(&format!("{}: {:?}", i, color)), color],
52+
iter::repeat((n_colors - 1) - i),
53+
iter::repeat(0.4),
54+
iter::repeat(0.2), //x.clone().map(|x| (((3 + x) as f64) / 3.0).rem(0.8)),
55+
&[
56+
Caption(&color_name(&color)),
57+
LineWidth(1.0),
58+
BorderColor("black"),
59+
color,
60+
],
2961
);
3062
}
3163

64+
// Draw line across the boxes in fixed black and background colors
65+
ax.lines(
66+
[0, 0],
67+
[0, n_colors - 1],
68+
&[
69+
LineWidth(7.0),
70+
Color(Black),
71+
Caption(&color_name(&Color(Black))),
72+
],
73+
);
74+
75+
ax.lines(
76+
[4, 4],
77+
[0, n_colors - 1],
78+
&[
79+
LineWidth(7.0),
80+
Color(Background),
81+
Caption(&color_name(&Color(Background))),
82+
],
83+
);
84+
3285
c.show(&mut fg, "rgb_color");
3386
}
3487

gnuplot/src/axes2d.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl ArrowData
191191
}
192192
w.write_str(",12");
193193

194-
AxesCommonData::write_color_options(w, &self.plot_options, Some(ColorType::Black));
194+
AxesCommonData::write_color_options(w, &self.plot_options, false, Some(ColorType::Black));
195195
AxesCommonData::write_line_options(
196196
w,
197197
&self.plot_options,
@@ -236,7 +236,7 @@ impl BorderOptions
236236
write!(writer, "{}", f);
237237
writer.write_str(if self.front { " front " } else { " back " });
238238

239-
AxesCommonData::write_color_options(writer, &self.options, Some(ColorType::Black));
239+
AxesCommonData::write_color_options(writer, &self.options, false, Some(ColorType::Black));
240240
AxesCommonData::write_line_options(writer, &self.options, version);
241241

242242
writer.write_str("\n");

gnuplot/src/axes_common.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,16 @@ impl PlotElement
207207

208208
if !is_pattern
209209
{
210-
writer.write_str("transparent solid");
210+
let mut color_has_alpha = false;
211+
first_opt! {self.options,
212+
ColorOpt(ref c) => {
213+
color_has_alpha = c.has_alpha()
214+
}
215+
}
216+
if !color_has_alpha{
217+
writer.write_str("transparent ");
218+
}
219+
writer.write_str("solid");
211220
let mut alpha = 1.;
212221
first_opt! {self.options,
213222
FillAlpha(a) =>
@@ -255,7 +264,7 @@ impl PlotElement
255264
}
256265
}
257266

258-
AxesCommonData::write_color_options(writer, &self.options, None);
267+
AxesCommonData::write_color_options(writer, &self.options, self.plot_type.is_fill(), None);
259268

260269
writer.write_str(" t \"");
261270
first_opt! {self.options,
@@ -680,7 +689,7 @@ impl AxisData
680689
w.write_str(self.axis.get_axis_str());
681690
w.write_str("zeroaxis ");
682691

683-
AxesCommonData::write_color_options(w, &self.options, Some(ColorType::RGBString("black".into())));
692+
AxesCommonData::write_color_options(w, &self.options, false, Some(ColorType::RGBString("black".into())));
684693
AxesCommonData::write_line_options(w, &self.options, version);
685694
}
686695
else
@@ -1141,10 +1150,10 @@ impl AxesCommonData
11411150
}
11421151

11431152
AxesCommonData::write_line_options(c, &self.grid_options, version);
1144-
AxesCommonData::write_color_options(c, &self.grid_options, None);
1153+
AxesCommonData::write_color_options(c, &self.grid_options, false, None);
11451154
c.write_str(", ");
11461155
AxesCommonData::write_line_options(c, &self.minor_grid_options, version);
1147-
AxesCommonData::write_color_options(c, &self.minor_grid_options, None);
1156+
AxesCommonData::write_color_options(c, &self.minor_grid_options, false, None);
11481157
c.write_str("\n");
11491158
}
11501159
}
@@ -1188,7 +1197,7 @@ impl AxesCommonData
11881197
}
11891198

11901199
pub fn write_color_options(
1191-
c: &mut dyn Writer, options: &[PlotOption<String>], default: Option<ColorType>,
1200+
c: &mut dyn Writer, options: &[PlotOption<String>], is_fill: bool, default: Option<ColorType>,
11921201
)
11931202
{
11941203
let mut col = default.as_ref();
@@ -1200,7 +1209,7 @@ impl AxesCommonData
12001209
}
12011210
if let Some(s) = col
12021211
{
1203-
write!(c, " lc {}", s.command());
1212+
write!(c, " {main_type} {}", s.command());
12041213
}
12051214
}
12061215

gnuplot/src/color.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl<T: Display> ColorType<T> {
101101
VariablePaletteColor(_) => String::from("palette z"),
102102
SavedColorMap(s, _) => format!("palette {s}"),
103103
VariableIndex(_) => String::from("variable"),
104-
Background => todo!(),
104+
Background => String::from("background"),
105105
Index(n) => format!("{}", n),
106106
Black => String::from("black"),
107107
}
@@ -141,6 +141,23 @@ impl<T: Display> ColorType<T> {
141141
_ => false,
142142
}
143143
}
144+
145+
pub fn has_alpha(&self) -> bool {
146+
match self {
147+
RGBString(s) => {
148+
let s = s.to_string();
149+
if s.starts_with("0x") && s.chars().count() == 10 {
150+
true
151+
} else if s.starts_with("#") && s.chars().count() == 9 {
152+
true
153+
} else {
154+
false
155+
}
156+
}
157+
ARGBInteger(_, _, _, _) | VariableARGBIntegers(_) => true,
158+
_ => false,
159+
}
160+
}
144161
}
145162

146163
fn from_argb(
@@ -159,15 +176,15 @@ fn float_color_to_int(v: f64) -> u8 {
159176
((v * 255.0).round()) as u8
160177
}
161178

162-
fn from_rgb_floats(r: f64, g: f64, b: f64) -> RGBInts {
179+
pub fn floats_to_rgb(r: f64, g: f64, b: f64) -> RGBInts {
163180
(
164181
float_color_to_int(r),
165182
float_color_to_int(g),
166183
float_color_to_int(b),
167184
)
168185
}
169186

170-
fn from_argb_floats(a: f64, r: f64, g: f64, b: f64) -> ARGBInts {
187+
pub fn floats_to_argb(a: f64, r: f64, g: f64, b: f64) -> ARGBInts {
171188
(
172189
float_color_to_int(a),
173190
float_color_to_int(r),
@@ -208,14 +225,14 @@ impl<T> Into<ColorType<T>> for RGBInts {
208225

209226
impl<T> Into<ColorType<T>> for (f64, f64, f64) {
210227
fn into(self) -> ColorType<T> {
211-
let ints = from_rgb_floats(self.0, self.1, self.2);
228+
let ints = floats_to_rgb(self.0, self.1, self.2);
212229
ColorType::RGBInteger(ints.0, ints.1, ints.2)
213230
}
214231
}
215232

216233
impl<T> Into<ColorType<T>> for (f64, f64, f64, f64) {
217234
fn into(self) -> ColorType<T> {
218-
let ints = from_argb_floats(self.0, self.1, self.2, self.3);
235+
let ints = floats_to_argb(self.0, self.1, self.2, self.3);
219236
ColorType::ARGBInteger(ints.0, ints.1, ints.2, ints.3)
220237
}
221238
}

0 commit comments

Comments
 (0)