Skip to content

Commit ab2c419

Browse files
SiegeLordExSiegeLord
authored andcommitted
Some fixes to the new color implementation.
- Polygons got accidentally switched to the wrong plot type - Invert the meaning of alpha to match the commonly used meaning (rather than the weird one used by gnuplot) - Unconditionally show the border for various plot elements - Some minor stylistic changes
1 parent ffd11c0 commit ab2c419

File tree

7 files changed

+87
-56
lines changed

7 files changed

+87
-56
lines changed

gnuplot/examples/box_and_whisker.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@ fn example(c: Common)
1010

1111
fg.axes2d()
1212
.set_title("Box and whisker", &[])
13-
.box_and_whisker(
14-
[0.0f32, 1.0, 2.0].iter(),
15-
[-1.0f32, 0.0, 1.0].iter(),
16-
[-2.0f32, -1.0, 0.0].iter(),
17-
[2.0f32, 3.0, 4.0].iter(),
18-
[1.0f32, 2.0, 3.0].iter(),
19-
&[],
20-
)
2113
.box_and_whisker_set_width(
2214
[-0.6f32, 1.5, 2.5].iter(),
2315
[-1.0f32, 0.0, 1.0].iter(),
@@ -33,6 +25,14 @@ fn example(c: Common)
3325
FillAlpha(0.5),
3426
],
3527
)
28+
.box_and_whisker(
29+
[0.0f32, 1.0, 2.0].iter(),
30+
[-1.0f32, 0.0, 1.0].iter(),
31+
[-2.0f32, -1.0, 0.0].iter(),
32+
[2.0f32, 3.0, 4.0].iter(),
33+
[1.0f32, 2.0, 3.0].iter(),
34+
&[],
35+
)
3636
.set_x_range(Fix(-1.0), Fix(3.0))
3737
.set_y_range(Fix(-3.0), Fix(5.0));
3838

gnuplot/examples/color.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ fn example(c: Common)
2323
Color("black".into()), // Conversion to RGBString is implicit
2424
Color(ColorType::RGBString("black")), // Explicit use of RGBString
2525
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
26+
Color(RGBString("#ff0000")), // Red using Hex coded RRGGBB
27+
Color(RGBString("#ffff0000")), // Red using Hex coded AARRGGBB
28+
Color("#ff8888".into()), // Pink using Hex coded RRGGBB. Conversion to RGBString is implict
29+
Color("#77ff0000".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((64, 128, 0, 255).into()), // Pale purple using implict ARGBInteger
35+
Color(ARGBInteger(64, 128, 0, 255)), // Pale purple using explict ARGBInteger
36+
Color((0.25, 0.5, 0.0, 1.0).try_into().unwrap()), // Pale purple using implict float to int conversion
3737
];
3838

3939
let mut fg = Figure::new();
@@ -43,7 +43,7 @@ fn example(c: Common)
4343
&[],
4444
)
4545
.set_x_range(Fix(-9.0), Auto)
46-
.set_legend(Graph(0.5), Graph(0.9), &[], &[Font("", 14.0)]);
46+
.set_legend(Graph(0.5), Graph(0.9), &[], &[Font("", 12.0)]);
4747

4848
let n_colors = colors.len();
4949
for (i, color) in colors.into_iter().enumerate()
@@ -85,7 +85,7 @@ fn example(c: Common)
8585

8686
// any of the forms used for Color can also be used with TextColor and BorderColor
8787
ax.set_x_label(
88-
"Labels can be colored using the TextColor function",
88+
"Labels can be colored using TextColor",
8989
&[TextColor((128, 0, 255).into())],
9090
);
9191

@@ -97,7 +97,7 @@ fn example(c: Common)
9797
let ax = fg.axes2d();
9898
let max_cb = 10.0;
9999
ax.set_cb_range(Fix(0.0), Fix(max_cb));
100-
for color_value in 0..=10
100+
for color_value in (0..=10).into_iter().step_by(2)
101101
{
102102
let color_float = color_value as f64;
103103
let frac_color = Color(PaletteFracColor(color_float / max_cb));
@@ -130,7 +130,7 @@ fn example(c: Common)
130130
}
131131
ax.set_x_range(Fix(-10.0), Fix(11.0))
132132
.set_y_range(Fix(-0.5), Fix(1.5))
133-
.set_legend(Graph(0.45), Graph(0.9), &[], &[Font("", 14.0)]);
133+
.set_legend(Graph(0.45), Graph(0.9), &[], &[Font("", 12.0)]);
134134
c.show(&mut fg, "palette_colors");
135135
}
136136

gnuplot/examples/inverse_api.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,7 @@ fn example(c: Common)
177177
//~ lines(z.clone(), y.clone()).show();
178178

179179
let mut axes = (lines(z.clone(), y.clone()), lines(z.clone(), x.clone())).to_axes2d();
180-
axes.title("Test");
181-
axes.x(axis().log_scale(Some(10.)));
180+
axes.title("Test").x(axis().log_scale(Some(10.)));
182181

183182
if !c.no_show
184183
{

gnuplot/examples/variable_color.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use gnuplot::{palettes::MAGMA, *};
77
mod common;
88

99
// https://github.com/gnuplot/gnuplot/blob/master/demo/candlesticks.dat
10-
static CANDLESTICKS_STR: &str = "1 1.5 2 2.4 4 6.
10+
#[rustfmt::skip]
11+
static CANDLESTICKS_STR: &str =
12+
"1 1.5 2 2.4 4 6.
1113
2 1.5 3 3.5 4 5.5
1214
3 4.5 5 5.5 6 6.5
1315
4 3.7 4.5 5.0 5.5 6.1
@@ -32,16 +34,14 @@ fn example(c: Common)
3234

3335
let d1 = extract_col(0);
3436
let d2 = extract_col(1);
35-
// let d3 = extract_col(2);
36-
// let d4 = extract_col(3);
3737
let d5 = extract_col(4);
3838
let d6 = extract_col(5);
3939
let row_index: Vec<_> = (1..=d1.len() as u8).collect();
4040
let by3 = |x| (((x % 3.0) + 1.0) / 6.0);
4141
let by4 = |x| (((x % 4.0) + 1.0) / 7.0);
4242

4343
let argb_formula = |x: &f64| {
44-
let a = 255.0 * (x - 5.5).abs() / 5.5;
44+
let a = 255.0 - 255.0 * (x - 5.5).abs() / 5.5;
4545
let r = x * 51.0 / 2.0;
4646
let g = (11.0 - x) * 51.0 / 2.0;
4747
let b = ((5.5 - x).abs() * 2.0 * 510.0 / 9.0).round();
@@ -63,10 +63,10 @@ fn example(c: Common)
6363
// input value for each data point. The palette is scaled to the maximum value in the `Vec<f64>` passed
6464
// to the `VariablePaletteColor`.
6565
//
66-
// The third color loop uses an (implicit) `VariableARGBString`. The `Vec<(u8, u8, u8, u8)>` needed to construct the color
67-
// is calculated in this case by the `argb_formula()` closure. An explicit `VariableARGBString` could also be constructed using
68-
// `Color(ColorType::VariableARGBString(data)`.
69-
// As an alternative, `VariableRGBString` is also defined that takes a 3-tuple of u8, rather than
66+
// The third color loop uses an (implicit) `VariableARGBInteger`. The `Vec<(u8, u8, u8, u8)>` needed to construct the color
67+
// is calculated in this case by the `argb_formula()` closure. An explicit `VariableARGBInteger` could also be constructed using
68+
// `Color(ColorType::VariableARGBInteger(data)`.
69+
// As an alternative, `VariableRGBInteger` is also defined that takes a 3-tuple of u8, rather than
7070
// a 4 tuple.
7171
for (color, label) in [
7272
(Color(row_index.clone().into()), "VariableIndexColor"),
@@ -78,7 +78,7 @@ fn example(c: Common)
7878
),
7979
(
8080
Color(d1.iter().map(argb_formula).collect::<Vec<_>>().into()),
81-
"VariableARGBString",
81+
"VariableARGBInteger",
8282
),
8383
]
8484
{

gnuplot/src/axes2d.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ impl Axes2D
662662
{
663663
let (data, num_rows, num_cols) = generate_data!(options, x, y);
664664
self.common.elems.push(PlotElement::new_plot(
665-
FillBetween,
665+
Polygons,
666666
data,
667667
num_rows,
668668
num_cols,

gnuplot/src/axes_common.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,12 @@ impl PlotElement
232232

233233
if self.plot_type.is_line()
234234
{
235+
// Write this unconditionally so the write_line_options work below.
236+
writer.write_str(" border ");
235237
first_opt! {self.options,
236238
BorderColor(ref s) =>
237239
{
238-
write!(writer, " border {}", s.command());
240+
writer.write_str(&s.command());
239241
}
240242
}
241243
}
@@ -491,7 +493,7 @@ pub fn write_out_label_options(
491493
first_opt! {options,
492494
MarkerColor(ref s) =>
493495
{
494-
write!(w, r#" lc "{}""#, s.command());
496+
write!(w, r#" lc {}"#, s.command());
495497
}
496498
}
497499

@@ -1264,22 +1266,16 @@ impl AxesCommonData
12641266
}
12651267
}
12661268
self.margins.write_out_commands(w);
1267-
self.palette.write_out_commands(w);
12681269

12691270
if !self.colormaps.is_empty()
12701271
{
1271-
// save previous palette
1272-
writeln!(w, "set colormap new __ORIGINAL_COLORMAP__");
12731272
for (name, map) in &self.colormaps
12741273
{
1275-
// set palette to the requested map
12761274
map.write_out_commands(w);
1277-
// save current palette to colormap with the requested name
12781275
writeln!(w, "set colormap new {name}");
12791276
}
1280-
// reload previous palette from saved colormap
1281-
writeln!(w, "set palette colormap __ORIGINAL_COLORMAP__");
12821277
}
1278+
self.palette.write_out_commands(w);
12831279

12841280
self.x_axis.write_out_commands(w, version);
12851281
self.y_axis.write_out_commands(w, version);

gnuplot/src/color.rs

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ pub type ARGBInts = (
1717
/// color formats. The gnuplot [colorspec reference](http://gnuplot.info/docs_6.0/loc3640.html)
1818
/// also explains these.
1919
///
20+
/// **NOTE**: Gnuplot interprets the alpha channel in an unusual way, where 0 is fully opaque and
21+
/// 255 is fully transparent. This wrapper inverts the meaning (as described below) to match the
22+
/// more common interpretation.
23+
///
2024
/// There are many equivalent ways of specifying colors, and this allows the user to chose the most convenient.
2125
/// For example, all the following will produce the same blue color:
22-
/// `RGBColor("blue".into())`, `RGBColor("0x0000ff".into())`, `RGBColor("#0000ff".into())`, `RGBColor("0x000000ff".into())`,
23-
/// `RGBColor("#000000ff".into())`, `RGBIntegerColor(0, 0, 255)`, `ARGBColor(0, 0, 0, 255)`,
26+
/// `RGBColor("blue".into())`, `RGBColor("0x0000ff".into())`, `RGBColor("#0000ff".into())`, `RGBColor("0xff0000ff".into())`,
27+
/// `RGBColor("#ff0000ff".into())`, `RGBIntegerColor(0, 0, 255)`, `ARGBColor(255, 0, 0, 255)`,
2428
///
2529
/// See example usages of these colors in `color.rs` and `variable_color.rs` in the
2630
/// [Examples folder](https://github.com/SiegeLord/RustGnuplot/tree/master/gnuplot/examples) on Github
@@ -35,15 +39,15 @@ pub enum ColorType<T = String>
3539
/// - #RRGGBB --- string containing hexadecimal in x11 format
3640
/// - #AARRGGBB --- string containing hexadecimal in x11 format
3741
///
38-
/// "#AARRGGBB" represents an RGB color with an alpha channel (transparency) value in the high bits.
39-
/// An alpha value of 0 represents a fully opaque color; i.e., "#00RRGGBB" is the same as "#RRGGBB".
40-
/// An alpha value of 255 (FF) represents full transparency.
42+
/// "#AARRGGBB" represents an RGB color with an alpha channel value in the high bits.
43+
/// An alpha value of 255 (FF) represents a fully opaque color; i.e., "#FFRRGGBB" is the same as "#RRGGBB".
44+
/// An alpha value of 0 represents full transparency.
4145
RGBString(T),
4246
/// tuple of u8 representing red, green and blue values as 0-255
4347
RGBInteger(ColorComponent, ColorComponent, ColorComponent),
4448
/// tuple of u8 representing alpha, red, green and blue values as 0-255.
45-
/// As with `RGBColor`, an alpha value of 0 represents a fully opaque color;
46-
/// an alpha value of 255 (FF) represents full transparency.
49+
/// As with `RGBColor`, an alpha value of 255 (FF) represents a fully opaque color;
50+
/// an alpha value of 0 represents full transparency.
4751
ARGBInteger(
4852
ColorComponent,
4953
ColorComponent,
@@ -102,7 +106,7 @@ pub enum ColorType<T = String>
102106
VariablePaletteColor(Vec<f64>),
103107
/// Similar to `VariablePaletteColor` in that it takes a `Vec<f64>` to set the indexes into the
104108
/// color map for each data point, but in addition to the color data it takes a string hold the name
105-
/// of the color map to use. This should have been previously created in the workspace using the
109+
/// of the colormap to use. This should have been previously created in the workspace using the
106110
/// [create_colormap()](crate::AxesCommon::create_colormap) function.
107111
SavedColorMap(T, Vec<f64>),
108112
/// Set the color of all elements of the plot to the `n`th color in the current gnuplot color cycle.
@@ -127,8 +131,8 @@ impl<T: Display + Debug> ColorType<T>
127131
{
128132
match self
129133
{
130-
RGBString(s) => format!(r#"rgb "{}""#, s),
131-
RGBInteger(r, g, b) => format!(r#"rgb {}"#, from_argb(0, *r, *g, *b)),
134+
RGBString(s) => format!(r#"rgb "{}""#, from_string(s.to_string())),
135+
RGBInteger(r, g, b) => format!(r#"rgb {}"#, from_argb(255, *r, *g, *b)),
132136
ARGBInteger(a, r, g, b) => format!(r#"rgb {}"#, from_argb(*a, *r, *g, *b)),
133137
VariableRGBInteger(_) => "rgb variable".into(),
134138
VariableARGBInteger(_) => "rgb variable".into(),
@@ -149,7 +153,7 @@ impl<T: Display + Debug> ColorType<T>
149153
{
150154
VariableRGBInteger(items) => items
151155
.iter()
152-
.map(|(r, g, b)| from_argb(0, *r, *g, *b) as f64)
156+
.map(|(r, g, b)| from_argb(255, *r, *g, *b) as f64)
153157
.collect(),
154158
VariableARGBInteger(items) => items
155159
.iter()
@@ -192,7 +196,39 @@ impl<T: Display + Debug> ColorType<T>
192196
fn from_argb(a: ColorComponent, r: ColorComponent, g: ColorComponent, b: ColorComponent)
193197
-> ColorInt
194198
{
195-
((a as ColorInt) << 24) + ((r as ColorInt) << 16) + ((g as ColorInt) << 8) + (b as ColorInt)
199+
((255 - a as ColorInt) << 24)
200+
+ ((r as ColorInt) << 16)
201+
+ ((g as ColorInt) << 8)
202+
+ (b as ColorInt)
203+
}
204+
205+
fn from_string(argb: String) -> String
206+
{
207+
if let Some(trimmed_argb) = argb.strip_prefix("0x").or_else(|| argb.strip_prefix("#"))
208+
{
209+
if trimmed_argb.len() == 8
210+
{
211+
if let Ok(argb_int) = ColorInt::from_str_radix(trimmed_argb, 16)
212+
{
213+
let a = 255 - ((argb_int >> 24) & 0xff);
214+
let argb_int = (a << 24) + (argb_int & 0xffffff);
215+
format!("#{:08x}", argb_int)
216+
}
217+
else
218+
{
219+
// Let gnuplot sort it out.
220+
argb
221+
}
222+
}
223+
else
224+
{
225+
argb
226+
}
227+
}
228+
else
229+
{
230+
argb
231+
}
196232
}
197233

198234
fn float_color_to_int(v: f64) -> Result<u8, String>

0 commit comments

Comments
 (0)