Skip to content

Commit f8eaa1f

Browse files
committed
add documentation to relevant items
1 parent 0dcb75a commit f8eaa1f

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

plotters/src/style/colors/colormaps.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ impl<ColorType: crate::style::Color + Clone> DerivedColorMap<ColorType> {
4545
}
4646
}
4747

48+
#[macro_export]
49+
#[doc(hidden)]
4850
macro_rules! calculate_new_color_value(
4951
($relative_difference:expr, $colors:expr, $index_upper:expr, $index_lower:expr, RGBColor) => {
5052
RGBColor(
@@ -79,6 +81,20 @@ macro_rules! calculate_new_color_value(
7981
};
8082
);
8183

84+
/// Helper function to calculate the lower and upper index nearest to a provided float value.
85+
///
86+
/// Used to obtain colors from a colorscale given a value h between 0.0 and 1.0.
87+
/// It also returns the relative difference which can then be used to calculate a linear interpolation between the two nearest colors.
88+
/// ```
89+
/// # use plotters::prelude::*;
90+
/// let r = calculate_relative_difference_index_lower_upper(1.2, 1.0, 3.0, 4);
91+
/// let (relative_difference, lower_index, upper_index) = r;
92+
///
93+
/// assert_eq!(relative_difference, 0.7000000000000001);
94+
/// assert_eq!(lower_index, 0);
95+
/// assert_eq!(upper_index, 1);
96+
/// ```
97+
#[doc(hidden)]
8298
pub fn calculate_relative_difference_index_lower_upper<
8399
FloatType: num_traits::Float + num_traits::FromPrimitive + num_traits::ToPrimitive,
84100
>(
@@ -132,11 +148,33 @@ macro_rules! implement_color_scale_for_derived_color_map{
132148

133149
implement_color_scale_for_derived_color_map! {RGBAColor, RGBColor, HSLColor}
134150

151+
#[doc(inline)]
152+
pub use crate::count;
153+
154+
#[macro_export]
155+
#[doc(hidden)]
156+
/// Counts the number of arguments which are separated by spaces
157+
///
158+
/// This macro is used internally to determine the size of an array to hold all new colors.
159+
/// ```
160+
/// # use plotters::count;
161+
/// let counted = count!{Plotting is fun};
162+
/// assert_eq!(counted, 3);
163+
///
164+
/// let counted2 = count!{0_usize was my favourite 1_f64 last century};
165+
/// assert_eq!(counted2, 7);
166+
///
167+
/// let new_array = ["Hello"; count!(Plotting is fun)];
168+
/// assert_eq!(new_array, ["Hello"; 3]);
169+
/// ```
135170
macro_rules! count {
136171
() => (0usize);
137172
($x:tt $($xs:tt)* ) => (1usize + $crate::count!($($xs)*));
138173
}
139174

175+
#[macro_export]
176+
#[doc(hidden)]
177+
/// Converts a given color identifier and a sequence of colors to an array of them.
140178
macro_rules! define_colors_from_list_of_values_or_directly{
141179
($color_type:ident, $(($($color_value:expr),+)),+) => {
142180
[$($color_type($($color_value),+)),+]
@@ -209,8 +247,31 @@ macro_rules! implement_linear_interpolation_color_map {
209247
};
210248
}
211249

250+
#[doc(inline)]
251+
pub use crate::define_linear_interpolation_color_map;
252+
212253
#[macro_export]
213-
/// Macro to create a new colormap with evenly spaced colors at compile-time.
254+
#[doc(hidden)]
255+
/// Create a new colormap with evenly spaced colors and interpolates between them automatically.
256+
///
257+
/// This macro works by taking a identifier (name) for the colormap, the type of color to specify, a
258+
/// docstring and a list of colors and constructs an empty struct on which it implements the [ColorMap] trait.
259+
///
260+
/// ```
261+
/// use plotters::prelude::*;
262+
/// define_linear_interpolation_color_map! {
263+
/// BlackWhite,
264+
/// RGBColor,
265+
/// "Simple chromatic colormap from black to white.",
266+
/// ( 0, 0, 0),
267+
/// (255, 255, 255)
268+
/// }
269+
///
270+
/// assert_eq!(BlackWhite::get_color(0.0), RGBColor(0,0,0));
271+
/// ```
272+
///
273+
/// Hint: Some helper macros and functions have been deliberately hidden from end users.
274+
/// Look for them in the source code if you are interested.
214275
macro_rules! define_linear_interpolation_color_map{
215276
($color_scale_name:ident, $color_type:ident, $doc:expr, $(($($color_value:expr),+)),*) => {
216277
#[doc = $doc]

0 commit comments

Comments
 (0)