I was given a data set that is essentially an image, however each pixel in the image is represented as a value from -1 to 1 inclusive. I am writing an application that needs to take these -1 to 1 grayscale values and map them to the associated RGB value for the MATLAB "Jet" color scale (red-green-blue color gradient).
I am curious if anyone knows how to take a linear value (like -1 to 1) and map it to this scale. Note that I am not actually using MATLAB for this (nor can I), I just need to take the grayscale value and put it on the Jet gradient.
Thanks, Adam
You can change the color scheme by specifying a colormap. Colormaps are three-column arrays containing RGB triplets in which each row defines a distinct color. For example, here is a surface plot with the default color scheme. f = figure; surf(peaks);
Description. c = jet returns the jet colormap as a three-column array with the same number of rows as the colormap for the current figure. If no figure exists, then the number of rows is equal to the default length of 256. Each row in the array contains the red, green, and blue intensities for a specific color.
Starting in R2019b, you can display a tiling of plots using the tiledlayout and nexttile functions. Call the tiledlayout function to create a 2-by-1 tiled chart layout. Call the nexttile function to create the axes. Then display a surface plot in each axes with a colorbar.
A colormap is an array of colors that are indexed and then mapped to values. It is represented by a [Nx3] matrix, where N is the number of colors available in it. Each color is coded with three values corresponding its relative levels of red, green and blue. In Matlab, the colors are coded between 0 and 1.
Consider the following function (written by Paul Bourke -- search for Colour Ramping for Data Visualisation
):
/* Return a RGB colour value given a scalar v in the range [vmin,vmax] In this case each colour component ranges from 0 (no contribution) to 1 (fully saturated), modifications for other ranges is trivial. The colour is clipped at the end of the scales if v is outside the range [vmin,vmax] */ typedef struct { double r,g,b; } COLOUR; COLOUR GetColour(double v,double vmin,double vmax) { COLOUR c = {1.0,1.0,1.0}; // white double dv; if (v < vmin) v = vmin; if (v > vmax) v = vmax; dv = vmax - vmin; if (v < (vmin + 0.25 * dv)) { c.r = 0; c.g = 4 * (v - vmin) / dv; } else if (v < (vmin + 0.5 * dv)) { c.r = 0; c.b = 1 + 4 * (vmin + 0.25 * dv - v) / dv; } else if (v < (vmin + 0.75 * dv)) { c.r = 4 * (v - vmin - 0.5 * dv) / dv; c.b = 0; } else { c.g = 1 + 4 * (vmin + 0.75 * dv - v) / dv; c.b = 0; } return(c); }
Which, in your case, you would use it to map values in the range [-1,1]
to colors as (it is straightforward to translate it from C code to a MATLAB function):
c = GetColour(v,-1.0,1.0);
This produces to the following "hot-to-cold" color ramp:
It basically represents a walk on the edges of the RGB color cube from blue to red (passing by cyan, green, yellow), and interpolating the values along this path.
Note this is slightly different from the "Jet" colormap used in MATLAB, which as far as I can tell, goes through the following path:
#00007F: dark blue #0000FF: blue #007FFF: azure #00FFFF: cyan #7FFF7F: light green #FFFF00: yellow #FF7F00: orange #FF0000: red #7F0000: dark red
Here is a comparison I did in MATLAB:
%# values num = 64; v = linspace(-1,1,num); %# colormaps clr1 = jet(num); clr2 = zeros(num,3); for i=1:num clr2(i,:) = GetColour(v(i), v(1), v(end)); end
Then we plot both using:
figure subplot(4,1,1), imagesc(v), colormap(clr), axis off subplot(4,1,2:4), h = plot(v,clr); axis tight set(h, {'Color'},{'r';'g';'b'}, 'LineWidth',3)
Now you can modify the C code above, and use the suggested stop points to achieve something similar to jet colormap (they all use linear interpolation over the R,G,B channels as you can see from the above plots)...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With