@@ -45,6 +45,8 @@ impl<ColorType: crate::style::Color + Clone> DerivedColorMap<ColorType> {
45
45
}
46
46
}
47
47
48
+ #[ macro_export]
49
+ #[ doc( hidden) ]
48
50
macro_rules! calculate_new_color_value(
49
51
( $relative_difference: expr, $colors: expr, $index_upper: expr, $index_lower: expr, RGBColor ) => {
50
52
RGBColor (
@@ -79,6 +81,20 @@ macro_rules! calculate_new_color_value(
79
81
} ;
80
82
) ;
81
83
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) ]
82
98
pub fn calculate_relative_difference_index_lower_upper <
83
99
FloatType : num_traits:: Float + num_traits:: FromPrimitive + num_traits:: ToPrimitive ,
84
100
> (
@@ -132,11 +148,33 @@ macro_rules! implement_color_scale_for_derived_color_map{
132
148
133
149
implement_color_scale_for_derived_color_map ! { RGBAColor , RGBColor , HSLColor }
134
150
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
+ /// ```
135
170
macro_rules! count {
136
171
( ) => ( 0usize ) ;
137
172
( $x: tt $( $xs: tt) * ) => ( 1usize + $crate:: count!( $( $xs) * ) ) ;
138
173
}
139
174
175
+ #[ macro_export]
176
+ #[ doc( hidden) ]
177
+ /// Converts a given color identifier and a sequence of colors to an array of them.
140
178
macro_rules! define_colors_from_list_of_values_or_directly{
141
179
( $color_type: ident, $( ( $( $color_value: expr) ,+) ) ,+) => {
142
180
[ $( $color_type( $( $color_value) ,+) ) ,+]
@@ -209,8 +247,31 @@ macro_rules! implement_linear_interpolation_color_map {
209
247
} ;
210
248
}
211
249
250
+ #[ doc( inline) ]
251
+ pub use crate :: define_linear_interpolation_color_map;
252
+
212
253
#[ 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.
214
275
macro_rules! define_linear_interpolation_color_map{
215
276
( $color_scale_name: ident, $color_type: ident, $doc: expr, $( ( $( $color_value: expr) ,+) ) ,* ) => {
216
277
#[ doc = $doc]
0 commit comments