Inspired by @Shai's answer, here is a small twist on his solution (which I prefer - it is more flexible and avoids using a for loop).
The cmap form you want is an nx3 array. Next, you say that you have three colors that you want to represent with three βbreakpointsβ on your curve. It screams "interpolation"!
% set the "breakpoints" for the color curve: lowValue = 0; midValue = 128; highValue = 255; % pick "any" three colors to correspond to the breakpoints: lowColor = [255 0 0]; midColor = [40 40 40]; highColor = [0 255 255]; % create the colormap: myMap = interp1( [lowValue midValue highValue], ... [lowColor; midColor; highColor]/255, ... linspace(lowValue, highValue, 256));
This provides a 256-color card that runs smoothly from lowColor with the lowest value (index 1 in the color palette) to highColor with the highest value (index 255 in the color palette).
I believe that this is exactly what you are looking for. And "look ma, don't loop!".
source share