Skip to content

Commit da45b28

Browse files
committed
Improve documentation
1 parent 102dd89 commit da45b28

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed

gnuplot/examples/variable_color.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,14 @@ fn example(c: Common)
5959
// but make them align for multiple plot items on the same axis. This is implicity constructed from a Vec<u8> using
6060
// the `Color` function but could equivalently be created explicitly using `ColorOpt(ColorType::VariableIndexColor(row_index.clone()))`
6161
//
62-
// The second color loop uses a VariablePaletteColor: this selects the color based on the current color palette and the
63-
// input value for each data point. The palette is scaled to the maximum value in the vector of `f64`s passed
64-
// to the VariablePaletteColor.
62+
// The second color loop uses a `VariablePaletteColor`: this selects the color based on the current color palette and the
63+
// input value for each data point. The palette is scaled to the maximum value in the `Vec<f64>` passed
64+
// to the `VariablePaletteColor`.
6565
//
66-
// The third color loop uses an (implicit) VariableARGBString. The `Vec<(u8, u8, u8, u8)>` needed to constcruct the color
67-
// is calculated in this case by the `argb_formula()` closure. An explicit VariableARGBString could also be constructed using
68-
// `ColorOpt(ColorType::VariableARGBString(data)``. A VariableRGBString is also defined that takes a 3-tuple of u8, rather than
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+
// `ColorOpt(ColorType::VariableARGBString(data)`.
69+
// As an alternative, `VariableRGBString` is also defined that takes a 3-tuple of u8, rather than
6970
// a 4 tuple.
7071
for (color, label) in [
7172
(Color(row_index.clone()), "VariableIndexColor"),
@@ -126,23 +127,23 @@ fn example(c: Common)
126127

127128
// #####################################################################
128129
// The example below shows the same graphs as in the loop, but using a set of saved colormaps
129-
// similar to palette in gnuplot terms, but the current palette is applied to all plots by default, and
130-
// multiple named colormaps can be created).
130+
// similar to palette in gnuplot terms, but the a single (current) palette is applied to all plots by default.
131+
// By contrast, you can create multiple named colormaps.
131132
//
132-
// As with VariablePaletteColor, this Color takes a vector of f64 that says which point in the colormap to use,
133+
// As with `VariablePaletteColor``, this Color takes a `Vec<f64>` that says which point in the colormap to use,
133134
// but it also takes a the name of the colormap from which to draw the colors.
134135
//
135136
// Note that the Color range appears to be shared across plots: i.e. if one plot has
136-
// color data (the Vec<f64>) in the range 0-1, and another in the range 1-100, all the
137-
// colors in the first plot will be right at the bottom end of it's colormap, even if that's
137+
// color data (the `Vec<f64>`) in the range 0-1, and another in the range 1-100, all the
138+
// colors in the first plot will be right at the bottom end of its colormap, even if that's
138139
// a different colormap to the one used in the second plot.
139140
let mut fg = Figure::new();
140141
let ax = fg.axes2d();
141142

142143
// First create the colormaps we will later refer to
143-
// MAGMA is one of the colormaps provide with rust gnuplot
144+
// MAGMA is one of the colormaps provided with rust gnuplot
144145
ax.create_colormap("magma", MAGMA);
145-
// HOT is one of the colormaps provide with rust gnuplot
146+
// HOT is one of the colormaps provided with rust gnuplot
146147
ax.create_colormap("hot", HOT);
147148
// ocean (green-blue-white) as per the gnuplot documentation
148149
ax.create_colormap("ocean", PaletteType::Formula(23, 28, 3));

gnuplot/src/axes_common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2086,7 +2086,7 @@ pub trait AxesCommon: AxesCommonPrivate
20862086
/// Creates and saves a colormap in the gnuplot environment that can be used for
20872087
/// later plots (see examples/color_variable.rs for example usage)
20882088
///
2089-
/// /// # Arguments
2089+
/// # Arguments
20902090
/// * `name` - The name with which to save the colormap
20912091
/// * `palette` - What palette type to use
20922092
fn create_colormap(

gnuplot/src/color.rs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//! TODO
2-
31
pub use self::ColorType::*;
42
use crate::util::OneWayOwned;
53
use std::fmt::Display;
@@ -18,16 +16,16 @@ pub type ARGBInts = (
1816
ColorComponent,
1917
);
2018

21-
/// Option type (for lines, axes, and text) that allows the various different gnuplot
19+
/// Option type (for plots, borders, and text) that allows the various different gnuplot
2220
/// color formats. The gnuplot [colorspec reference](http://gnuplot.info/docs_6.0/loc3640.html)
2321
/// also explains these.
2422
///
25-
/// There are equivalent many ways of specifying colors, and this allows the user to chose the most convenient.
26-
/// for example, all the following will produce the same blue color:
23+
/// There are many equivalent ways of specifying colors, and this allows the user to chose the most convenient.
24+
/// For example, all the following will produce the same blue color:
2725
/// `RGBColor("blue")`, `RGBColor("0x0000ff")`, `RGBColor("#0000ff")`, `RGBColor("0x000000ff")`,
2826
/// `RGBColor("#000000ff")`, `RGBIntegerColor(0, 0, 255)`, `ARGBColor(0, 0, 0, 255)`,
2927
///
30-
/// See example usages of these colors in `color.rs`, `variable_color.rs` in the
28+
/// See example usages of these colors in `color.rs` and `variable_color.rs` in the
3129
/// [Examples folder](https://github.com/SiegeLord/RustGnuplot/tree/master/gnuplot/examples) on Github
3230
#[derive(Debug, Clone, PartialEq, PartialOrd)]
3331
pub enum ColorType<T = String>
@@ -71,7 +69,7 @@ pub enum ColorType<T = String>
7169
/// Similar to `VariablePaletteColor` in that it takes a `Vec<f64>` to set the indexes into the
7270
/// color map for each data point, but in addition to the color data it takes a string hold the name
7371
/// of the color map to use. This should have been previously created in the workspace using the
74-
/// `axes.create_colormap()` function.
72+
/// (create_colormap())[crate::AxesCommon::create_colormap()] function.
7573
SavedColorMap(T, Vec<f64>),
7674
/// Set the color of all elements of the plot to the `n`th color in the current gnuplot color cycle.
7775
Index(ColorIndex),
@@ -80,7 +78,7 @@ pub enum ColorType<T = String>
8078
/// in Rust gnuplot, the color type takes a vector of u8, where each index is treated the same as the
8179
/// fixed `IndexColor`.
8280
/// This is useful for setting bars/boxes etc to be
83-
/// the same color from multiple plot commands. The `color.rs` example has examples of this usage.
81+
/// the same color from multiple plot commands. The `variable_color` example has examples of this usage.
8482
VariableIndex(Vec<ColorIndex>),
8583
/// Set the color of the plot to the current background color.
8684
Background,
@@ -194,6 +192,17 @@ fn float_color_to_int(v: f64) -> u8
194192
((v * 255.0).round()) as u8
195193
}
196194

195+
/// Converts a set of `f64` red, green and blue values in the range `0 <= x <= 1` to a 3-tuple of `u8` suitable for use as
196+
/// an [RGBInteger]
197+
///
198+
/// Panics if any of the arguments are not in the range `0 <= x <= 1`
199+
///
200+
/// Ses also [floats_to_argb]
201+
///
202+
/// # Arguments
203+
/// * r - red. 0: no red, 1: fully red
204+
/// * g - green. 0: no green, 1: fully green
205+
/// * b - blue. 0: no blue, 1: fully blue
197206
pub fn floats_to_rgb(r: f64, g: f64, b: f64) -> RGBInts
198207
{
199208
(
@@ -203,6 +212,18 @@ pub fn floats_to_rgb(r: f64, g: f64, b: f64) -> RGBInts
203212
)
204213
}
205214

215+
/// Converts a set of `f64` red, green and blue values in the range `0 <= x <= 1` to a 3-tuple of `u8` suitable for use as
216+
/// an [ARGBInteger]
217+
///
218+
/// Panics if any of the arguments are not in the range `0 <= x <= 1`
219+
///
220+
/// Ses also [floats_to_rgb]
221+
///
222+
/// # Arguments
223+
/// * a - alpha (transparency) value. 0: completely opaque, 1: completely transparent.
224+
/// * r - red. 0: no red, 1: fully red
225+
/// * g - green. 0: no green, 1: fully green
226+
/// * b - blue. 0: no blue, 1: fully blue
206227
pub fn floats_to_argb(a: f64, r: f64, g: f64, b: f64) -> ARGBInts
207228
{
208229
(
@@ -215,6 +236,7 @@ pub fn floats_to_argb(a: f64, r: f64, g: f64, b: f64) -> ARGBInts
215236

216237
impl<'l> Into<ColorType<String>> for &'l str
217238
{
239+
/// Converts `&str` into [RGBString]
218240
fn into(self) -> ColorType<String>
219241
{
220242
ColorType::RGBString(String::from(self))
@@ -223,6 +245,7 @@ impl<'l> Into<ColorType<String>> for &'l str
223245

224246
impl<'l> Into<ColorType<String>> for String
225247
{
248+
/// Converts `String` into [RGBString]
226249
fn into(self) -> ColorType<String>
227250
{
228251
ColorType::RGBString(self)
@@ -231,6 +254,7 @@ impl<'l> Into<ColorType<String>> for String
231254

232255
impl<'l> Into<ColorType<&'l str>> for &'l str
233256
{
257+
/// Converts `&str` into [RGBString]
234258
fn into(self) -> ColorType<&'l str>
235259
{
236260
ColorType::RGBString(self)
@@ -239,6 +263,7 @@ impl<'l> Into<ColorType<&'l str>> for &'l str
239263

240264
impl<T> Into<ColorType<T>> for ARGBInts
241265
{
266+
/// Converts `(u8, u8, u8, u8)` into [ARGBInteger]
242267
fn into(self) -> ColorType<T>
243268
{
244269
ColorType::ARGBInteger(self.0, self.1, self.2, self.3)
@@ -247,6 +272,7 @@ impl<T> Into<ColorType<T>> for ARGBInts
247272

248273
impl<T> Into<ColorType<T>> for RGBInts
249274
{
275+
/// Converts `(u8, u8, u8)` into [RGBInteger]
250276
fn into(self) -> ColorType<T>
251277
{
252278
ColorType::RGBInteger(self.0, self.1, self.2)
@@ -255,6 +281,8 @@ impl<T> Into<ColorType<T>> for RGBInts
255281

256282
impl<T> Into<ColorType<T>> for (f64, f64, f64)
257283
{
284+
/// Converts `(f64, f64, f64)` into [RGBInteger].
285+
/// All values must be in the range 0-1, or the function will panic.
258286
fn into(self) -> ColorType<T>
259287
{
260288
let ints = floats_to_rgb(self.0, self.1, self.2);
@@ -264,6 +292,8 @@ impl<T> Into<ColorType<T>> for (f64, f64, f64)
264292

265293
impl<T> Into<ColorType<T>> for (f64, f64, f64, f64)
266294
{
295+
/// Converts `(f64, f64, f64, f64)` into [ARGBInteger].
296+
/// All values must be in the range 0-1, or the function will panic.
267297
fn into(self) -> ColorType<T>
268298
{
269299
let ints = floats_to_argb(self.0, self.1, self.2, self.3);
@@ -273,6 +303,7 @@ impl<T> Into<ColorType<T>> for (f64, f64, f64, f64)
273303

274304
impl<T> Into<ColorType<T>> for Vec<RGBInts>
275305
{
306+
/// Converts `Vec<(u8, u8, u8)>` into [VariableRGBInteger]
276307
fn into(self) -> ColorType<T>
277308
{
278309
ColorType::VariableRGBInteger(self)
@@ -281,6 +312,7 @@ impl<T> Into<ColorType<T>> for Vec<RGBInts>
281312

282313
impl<T> Into<ColorType<T>> for Vec<ARGBInts>
283314
{
315+
/// Converts `Vec<(u8, u8, u8, u8)>` into [VariableARGBInteger]
284316
fn into(self) -> ColorType<T>
285317
{
286318
ColorType::VariableARGBInteger(self)
@@ -289,6 +321,7 @@ impl<T> Into<ColorType<T>> for Vec<ARGBInts>
289321

290322
impl<T> Into<ColorType<T>> for ColorIndex
291323
{
324+
/// Converts `u8` into [Index]
292325
fn into(self) -> ColorType<T>
293326
{
294327
ColorType::Index(self)
@@ -297,6 +330,7 @@ impl<T> Into<ColorType<T>> for ColorIndex
297330

298331
impl<T> Into<ColorType<T>> for Vec<ColorIndex>
299332
{
333+
/// Converts `Vec<u8>` into [VariableIndex]
300334
fn into(self) -> ColorType<T>
301335
{
302336
ColorType::VariableIndex(self)

0 commit comments

Comments
 (0)