I'm trying to generate a colormap in MATLAB given three colors, a high extreme, zero, and low extreme. My thought process has been to loop from the high extreme to the middle and store each step to a a 3xN (first column is R, second is G, and third is B)matrix. So I'm using:
%fade from high to zero
oldRed=high(1);
oldGreen=high(2);
oldBlue=high(3);
newRed=mid(1);
newGreen=mid(2);
newBlue=mid(3);
currentRed=oldRed; currentGreen=oldGreen; currentBlue=oldBlue;
for x=1:steps
currentRed=oldRed+((x*(newRed-oldRed))/(steps-1));
currentGreen=oldGreen+((x*(newRed-oldRed))/(steps-1));
currentBlue=oldBlue+((x*(newRed-oldRed))/(steps-1));
cmap=[cmap;[currentRed currentGreen currentBlue]];
end
Then I would do the same thing going from the zero value to the low extreme. However my code is not giving me any kind of useful matrix. Would someone be able to help me with how I should approach this?
You can use linear interpolation to expand the color
nCol = 256; % number of colors for the resulting map
cmap = zeros( nCol, 3 ); % pre-allocate
xi = linspace( 0, 1, nCols );
for ci=1:3 % for each channel
cmap(:,ci) = interp1( [0 .5 1], [low(ci) mid(ci) high(ci)], xi )';
end
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