I need to select a color given a position within a range.
Using the "bwr" colormap shown below, I want to return a blue color for a value at the beginning of the range, a red color for a value at the end of the range, and a suitable interpolated value for values in between.
The lower and upper ends of the range can be anything. From negative to positive numbers, for example [-0.5,0.5] or [0,1] or [-0.2 to 0.6].
Here is my code:
#include <opencv2/core/core.hpp>
cv::Scalar get_colour(double v, double vmin, double vmax)
{
double r, g, b = 0;
double dv;
if (v < vmin)
v = vmin;
if (v > vmax)
v = vmax;
dv = vmax - vmin;
if (v < (vmin + 0.5 * dv)) {
r = 2 * (v - vmin) / dv;
g = 2 * (v - vmin) / dv;
b = 1;
}
else if (v >= (vmin + 0.5 * dv)) {
b = 1 + 2 * (vmin + 0.5 * dv - v) / dv;
g = 1 + 2 * (vmin + 0.5 * dv - v) / dv;
r = 1;
}
return cv::Scalar(b * 255, g * 255, r * 255);
}
Can you check if this function always work good, no matter what range and value I choose, and what can I write better?
#include
lines, and amain()
that shows how to call your function. It's not mandatory, but it really helps! \$\endgroup\$