Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB: generating a colormap given three colors

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?

like image 297
Sam Avatar asked Jan 13 '23 01:01

Sam


1 Answers

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
like image 97
Shai Avatar answered Jan 21 '23 21:01

Shai