Skip to content

Commit c5422c1

Browse files
committed
encapsulate common functionality in private function
1 parent b8f5dc8 commit c5422c1

File tree

1 file changed

+51
-28
lines changed

1 file changed

+51
-28
lines changed

plotters/src/style/colors/colormaps.rs

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,43 @@ macro_rules! calculate_new_color_value(
7575
};
7676
);
7777

78+
fn calculate_relative_difference_index_lower_upper<
79+
FloatType: Float + FromPrimitive + ToPrimitive,
80+
>(
81+
h: FloatType,
82+
min: FloatType,
83+
max: FloatType,
84+
n_colors: usize,
85+
) -> (FloatType, usize, usize) {
86+
// Ensure that we do have a value in bounds
87+
let h = num_traits::clamp(h, min, max);
88+
// Next calculate a normalized value between 0.0 and 1.0
89+
let t = (h - min) / (max - min);
90+
let approximate_index =
91+
t * (FloatType::from_usize(n_colors).unwrap() - FloatType::one()).max(FloatType::zero());
92+
// Calculate which index are the two most nearest of the supplied value
93+
let index_lower = approximate_index.floor().to_usize().unwrap();
94+
let index_upper = approximate_index.ceil().to_usize().unwrap();
95+
// Calculate the relative difference, ie. is the actual value more towards the color of index_upper or index_lower?
96+
let relative_difference = approximate_index.ceil() - approximate_index;
97+
return (relative_difference, index_lower, index_upper);
98+
}
7899

79100
macro_rules! implement_color_scale_for_derived_ColorMap{
80101
($($color_type:tt),+) => {
81102
$(
82103
impl<FloatType: Float + FromPrimitive + ToPrimitive> ColorMap<$color_type, FloatType> for DerivedColorMap<$color_type> {
83104
fn get_color_normalized(&self, h: FloatType, min: FloatType, max: FloatType) -> $color_type {
84-
// Ensure that we do have a value in bounds
85-
let h = h.max(min).min(max);
86-
// Make sure that we really have a minimal value which is smaller than the maximal value
87-
assert_eq!(min<max, true);
88-
// Next calculate a normalized value between 0.0 and 1.0
89-
let t = (h - min)/(max-min);
90-
let approximate_index = t * (FloatType::from_usize(self.colors.len()).unwrap() - FloatType::one()).max(FloatType::zero());
91-
// Calculate which index are the two most nearest of the supplied value
92-
let index_lower = approximate_index.floor().to_usize().unwrap();
93-
let index_upper = approximate_index.ceil().to_usize().unwrap();
94-
// Calculate the relative difference, ie. is the actual value more towards the color of index_upper or index_lower?
95-
let relative_difference = approximate_index.ceil() - approximate_index;
105+
let (
106+
relative_difference,
107+
index_lower,
108+
index_upper
109+
) = calculate_relative_difference_index_lower_upper(
110+
h,
111+
min,
112+
max,
113+
self.colors.len()
114+
);
96115
// Interpolate the final color linearly
97116
calculate_new_color_value!(relative_difference, self.colors, index_upper, index_lower, $color_type)
98117
}
@@ -119,23 +138,27 @@ macro_rules! define_colors_from_list_of_values_or_directly{
119138
};
120139
}
121140

122-
123-
macro_rules! implement_linear_interpolation_color_map{
141+
macro_rules! implement_linear_interpolation_color_map {
124142
($color_scale_name:ident, $color_type:tt) => {
125-
impl<FloatType: std::fmt::Debug + Float + FromPrimitive + ToPrimitive> ColorMap<$color_type, FloatType> for $color_scale_name {
126-
fn get_color_normalized(&self, h: FloatType, min: FloatType, max: FloatType) -> $color_type {
127-
// Ensure that we do have a value in bounds
128-
let h = h.max(min).min(max);
129-
// Make sure that we really have a minimal value which is smaller than the maximal value
130-
assert_eq!(min<max, true);
131-
// Next calculate a normalized value between 0.0 and 1.0
132-
let t = (h - min)/(max-min);
133-
let approximate_index = t * (FloatType::from_usize(Self::COLORS.len()).unwrap() - FloatType::one()).max(FloatType::zero());
134-
// Calculate which index are the two most nearest of the supplied value
135-
let index_lower = approximate_index.floor().to_usize().unwrap();
136-
let index_upper = approximate_index.ceil().to_usize().unwrap();
137-
// Calculate the relative difference, ie. is the actual value more towards the color of index_upper or index_lower?
138-
let relative_difference = approximate_index.ceil() - approximate_index;
143+
impl<FloatType: std::fmt::Debug + Float + FromPrimitive + ToPrimitive>
144+
ColorMap<$color_type, FloatType> for $color_scale_name
145+
{
146+
fn get_color_normalized(
147+
&self,
148+
h: FloatType,
149+
min: FloatType,
150+
max: FloatType,
151+
) -> $color_type {
152+
let (
153+
relative_difference,
154+
index_lower,
155+
index_upper
156+
) = calculate_relative_difference_index_lower_upper(
157+
h,
158+
min,
159+
max,
160+
Self::COLORS.len()
161+
);
139162
// Interpolate the final color linearly
140163
calculate_new_color_value!(relative_difference, Self::COLORS, index_upper, index_lower, $color_type)
141164
}

0 commit comments

Comments
 (0)