Skip to content

Commit f75f748

Browse files
committed
Implement palette fixed colors
1 parent da45b28 commit f75f748

File tree

4 files changed

+98
-29
lines changed

4 files changed

+98
-29
lines changed

gnuplot/examples/color.rs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ fn example(c: Common)
4242
ax.set_title(
4343
"Demo of RGBString in various forms\nSee code comments for how to construct the colors",
4444
&[],
45-
);
46-
ax.set_x_range(Fix(-10.0), Auto);
47-
ax.set_legend(Graph(0.6), Graph(0.8), &[], &[Font("Arial", 14.0)]);
45+
)
46+
.set_x_range(Fix(-9.0), Auto)
47+
.set_legend(Graph(0.5), Graph(0.9), &[], &[Font("", 14.0)]);
4848

4949
let n_colors = colors.len();
5050
for (i, color) in colors.into_iter().enumerate()
@@ -91,6 +91,48 @@ fn example(c: Common)
9191
);
9292

9393
c.show(&mut fg, "rgb_color");
94+
95+
// ########################################################################
96+
97+
let mut fg = Figure::new();
98+
let ax = fg.axes2d();
99+
let max_cb = 10.0;
100+
ax.set_cb_range(Fix(0.0), Fix(max_cb));
101+
for color_value in 0..=10
102+
{
103+
let color_float = color_value as f64;
104+
let frac_color = Color(PaletteFracColor(color_float / max_cb));
105+
let cb_range_color = Color(PaletteCBColor(color_float));
106+
107+
ax.box_xy_error_delta(
108+
[color_value],
109+
[0],
110+
[0.4],
111+
[0.4],
112+
&[
113+
Caption(&color_name(&frac_color)),
114+
LineWidth(1.0),
115+
BorderColor("black"),
116+
frac_color,
117+
],
118+
)
119+
.box_xy_error_delta(
120+
[color_value],
121+
[1],
122+
[0.4],
123+
[0.4],
124+
&[
125+
Caption(&color_name(&cb_range_color)),
126+
LineWidth(1.0),
127+
BorderColor("black"),
128+
cb_range_color,
129+
],
130+
);
131+
}
132+
ax.set_x_range(Fix(-10.0), Fix(11.0))
133+
.set_y_range(Fix(-0.5), Fix(1.5))
134+
.set_legend(Graph(0.45), Graph(0.9), &[], &[Font("", 14.0)]);
135+
c.show(&mut fg, "palette_colors");
94136
}
95137

96138
fn main()

gnuplot/src/axes_common.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2073,7 +2073,8 @@ pub trait AxesCommon: AxesCommonPrivate
20732073
self
20742074
}
20752075

2076-
/// Sets the palette used for 3D surface and image plots
2076+
/// Sets the palette used for 3D surface and image plots. See the [palettes][crate::palettes]
2077+
/// module for a list of predefined palettes.
20772078
///
20782079
/// # Arguments
20792080
/// * `palette` - What palette type to use

gnuplot/src/color.rs

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,37 @@ pub enum ColorType<T = String>
5959
/// Vector of tuples of `u8` (as per `ARGBColor`), but as with `VariableRGBColor`, a separate
6060
/// color value is given for each data point.
6161
VariableARGBInteger(Vec<ARGBInts>),
62-
/// TODO
63-
PaletteFracColor(f32),
64-
/// TODO
65-
PaletteCBColor(f32),
62+
/// Sets the color of the plot element to a value picked from the current palette (see
63+
/// [set_palette()](crate::AxesCommon::set_palette())). The value supplied to this color type
64+
/// selects the color within the color range of the palette: i.e. it if the color bar range had been
65+
/// set with `ax.set_cb_range(Fix(min), Fix(max))`, the value would be expected to be between
66+
/// `min` and `max`.
67+
///
68+
/// Example of usage is give in the `color` example.
69+
///
70+
/// Compare with [PaletteFracColor]
71+
PaletteFracColor(f64),
72+
/// Sets the color of the plot element to a value picked from the current palette (see
73+
/// [set_palette()](crate::AxesCommon::set_palette()) . The value supplied to this color type
74+
/// selects the color as a fraction of the current color range i.e. it is expected to be
75+
/// between `0` and `1`.
76+
///
77+
/// Example of usage is give in the `color` example.
78+
///
79+
/// Comparing with [PaletteCBColor]: given the following code
80+
/// ```
81+
/// assert!(frac <= 0.0)
82+
/// assert!(frac >= 1.0)
83+
/// ax.set_cb_range(Fix(min), Fix(max))
84+
/// let col1 = PalleteFracColor(frac)
85+
/// let cb_range = max - min;
86+
/// let col2 = PaletteCBColor(min + (frac * cb_range))
87+
/// ```
88+
/// `col1` and `col2` should give the same color.
89+
PaletteCBColor(f64),
6690
/// Vector of `f64` values which act as indexes into the current palette to set the color of
67-
/// each data point
91+
/// each data point. These variable values work in the same was as the single fixed value supplied
92+
/// to a [PaletteCBColor]
6893
VariablePaletteColor(Vec<f64>),
6994
/// Similar to `VariablePaletteColor` in that it takes a `Vec<f64>` to set the indexes into the
7095
/// color map for each data point, but in addition to the color data it takes a string hold the name
@@ -91,22 +116,23 @@ impl<T: Display> ColorType<T>
91116
/// Returns the gnuplot string that will produce the requested color
92117
pub fn command(&self) -> String
93118
{
94-
match self
119+
let str = match self
95120
{
96-
RGBString(s) => format!(r#"rgb "{}""#, s),
97-
RGBInteger(r, g, b) => format!(r#"rgb {}"#, from_argb(0, *r, *g, *b)),
98-
ARGBInteger(a, r, g, b) => format!(r#"rgb {}"#, from_argb(*a, *r, *g, *b)),
99-
VariableRGBInteger(_) => String::from("rgb variable"),
100-
VariableARGBInteger(_) => String::from("rgb variable"),
101-
PaletteFracColor(_) => todo!(),
102-
PaletteCBColor(_) => todo!(),
103-
VariablePaletteColor(_) => String::from("palette z"),
104-
SavedColorMap(s, _) => format!("palette {s}"),
105-
VariableIndex(_) => String::from("variable"),
106-
Background => String::from("background"),
107-
Index(n) => format!("{}", n),
108-
Black => String::from("black"),
109-
}
121+
RGBString(s) => &format!(r#"rgb "{}""#, s),
122+
RGBInteger(r, g, b) => &format!(r#"rgb {}"#, from_argb(0, *r, *g, *b)),
123+
ARGBInteger(a, r, g, b) => &format!(r#"rgb {}"#, from_argb(*a, *r, *g, *b)),
124+
VariableRGBInteger(_) => "rgb variable",
125+
VariableARGBInteger(_) => "rgb variable",
126+
PaletteFracColor(v) => &format!("palette frac {v}"),
127+
PaletteCBColor(v) => &format!("palette cb {v}"),
128+
VariablePaletteColor(_) => "palette z",
129+
SavedColorMap(s, _) => &format!("palette {s}"),
130+
VariableIndex(_) => "variable",
131+
Background => "background",
132+
Index(n) => &format!("{}", n),
133+
Black => "black",
134+
};
135+
String::from(str)
110136
}
111137

112138
pub fn data(&self) -> Vec<f64>
@@ -348,8 +374,8 @@ impl<T: Display> OneWayOwned for ColorType<T>
348374
RGBString(s) => RGBString(s.to_string()),
349375
RGBInteger(r, g, b) => RGBInteger(*r, *g, *b),
350376
VariableRGBInteger(d) => VariableRGBInteger(d.clone()),
351-
PaletteFracColor(_) => todo!(),
352-
PaletteCBColor(_) => todo!(),
377+
PaletteFracColor(v) => PaletteFracColor(*v),
378+
PaletteCBColor(v) => PaletteCBColor(*v),
353379
VariablePaletteColor(d) => VariablePaletteColor(d.clone()),
354380
SavedColorMap(s, d) => SavedColorMap(s.to_string(), d.clone()),
355381
VariableIndex(d) => VariableIndex(d.clone()),
@@ -372,8 +398,8 @@ impl ColorType<String>
372398
RGBInteger(r, g, b) => RGBInteger(*r, *g, *b),
373399
VariableRGBInteger(d) => VariableRGBInteger(d.to_vec()),
374400
VariableARGBInteger(d) => VariableARGBInteger(d.to_vec()),
375-
PaletteFracColor(_) => todo!(),
376-
PaletteCBColor(_) => todo!(),
401+
PaletteFracColor(v) => PaletteFracColor(*v),
402+
PaletteCBColor(v) => PaletteCBColor(*v),
377403
VariablePaletteColor(d) => VariablePaletteColor(d.to_vec()),
378404
SavedColorMap(s, d) => SavedColorMap(s, d.to_vec()),
379405
VariableIndex(d) => VariableIndex(d.to_vec()),

gnuplot/src/options.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub enum PlotOption<T>
8888
/// [ColorType::VariableRGBInteger] or [ColorType::VariableARGBInteger] respectively
8989
/// * `u8` or `Vec<u8>` produces a [ColorType::Index] or [ColorType::VariableIndex] respectively
9090
///
91-
/// See `examples/color.rs` for usage of this function
91+
/// See `examples/color.rs` for usage of this function
9292
pub fn Color<'l, T: IntoColor<&'l str>>(c: T) -> PlotOption<&'l str>
9393
{
9494
ColorOpt(c.into())

0 commit comments

Comments
 (0)