Unable to create custom colormap from RGB data?

Still bogged down in the difference between an array and a matrix...
Everything was working well until I stumbled upon the following error message, at the time of attempting to create the colormap :
Colormap must be an Mx3 array of type single or double with values in the range [0,1] or an Mx3
array of type uint8.
Here's my code :
% Generate three vectors
Axe_A_B = transpose(linspace(-128, 128, 21));
Axe_La = transpose(linspace(39, 50, 21));
Axe_Lb = transpose(linspace(15,100, 21));
Lab_Axe_AB = [Axe_La, Axe_A_B, Axe_A_B]; % 21 x 3 double
RGB_Axe_AB = lab2rgb(Lab_Axe_AB)
% negatives values?
[row,col] = size(RGB_Axe_AB);
for ROW = 1:row
if RGB_Axe_AB(ROW,1) < 0
RGB_Axe_AB(ROW,1) = 0;
end
if RGB_Axe_AB(ROW,2) < 0
RGB_Axe_AB(ROW,2) = 0;
end
if RGB_Axe_AB(ROW,3) < 0
RGB_Axe_AB(ROW,3) = 0;
end
end
RGB_Axe_AB_ColorMap = colormap(RGB_Axe_AB)
I'm going in circle...
Documentation states :
% Colormap must have 3 columns: [R,G,B]
% defining a three-column matrix of values
Here's the content of my RGB_Axe_AB 'variable' :
RGB_Axe_AB =
0 0.5400 1.2184
0 0.5321 1.1330
0 0.5240 1.0486
0 0.5152 0.9653
0 0.5058 0.8831
0 0.4952 0.8020
0 0.4833 0.7220
0 0.4695 0.6432
0 0.4536 0.5654
0.2388 0.4349 0.4886
0.4126 0.4126 0.4126
0.5271 0.3857 0.3369
0.6198 0.3526 0.2607
0.7011 0.3106 0.1812
0.7756 0.2540 0.0886
0.8459 0.1660 0
0.9139 0 0
0.9810 0 0
1.0478 0 0
1.1142 0 0
1.1804 0 0
I did not bother to create a 256 size variable at this point. ...

 Accepted Answer

Oopsy, doopsy! I just realize there are values > 1.0 in those columns! Oh boy! Egg on my face...
Here's the revised code :
for ROW = 1:row
if RGB_Axe_AB(ROW,1) < 0
RGB_Axe_AB(ROW,1) = 0;
end
if RGB_Axe_AB(ROW,1) > 1
RGB_Axe_AB(ROW,1) = 1;
end
if RGB_Axe_AB(ROW,2) < 0
RGB_Axe_AB(ROW,2) = 0;
end
if RGB_Axe_AB(ROW,2) > 1
RGB_Axe_AB(ROW,2) = 1;
end
if RGB_Axe_AB(ROW,3) < 0
RGB_Axe_AB(ROW,3) = 0;
end
if RGB_Axe_AB(ROW,3) > 1
RGB_Axe_AB(ROW,3) = 1;
end
end
Naturally, everything works now. No more errors....
Is there a way to do this process more efficiently? Without resorting to a loop?
Thank you in advance for your help and patience :-)

6 Comments

Making progress... For those who are interested. The code is not elegant but it's the result I want. Right now, all I care to achieve is a colormap that goes from CIE Lab = 'the a* axis', from a* = -128 to a +127. This is my code :
% Generate three vectors
Axe_A_B = transpose(linspace(-128, 128, 21));
Axe_La = transpose(linspace(39, 50, 21));
Axe_Lb_Zero = transpose(linspace(0, 0, 21));
Axe_Lb = transpose(linspace(15,100, 21));
Lab_Axe_AB = [Axe_La, Axe_A_B, Axe_Lb_Zero]; % 21 x 3 double
RGB_Axe_AB = lab2rgb(Lab_Axe_AB)
% negatives values?
[row,col] = size(RGB_Axe_AB);
for ROW = 1:row
if RGB_Axe_AB(ROW,1) < 0
RGB_Axe_AB(ROW,1) = 0;
end
if RGB_Axe_AB(ROW,1) > 1
RGB_Axe_AB(ROW,1) = 1;
end
if RGB_Axe_AB(ROW,2) < 0
RGB_Axe_AB(ROW,2) = 0;
end
if RGB_Axe_AB(ROW,2) > 1
RGB_Axe_AB(ROW,2) = 1;
end
if RGB_Axe_AB(ROW,3) < 0
RGB_Axe_AB(ROW,3) = 0;
end
if RGB_Axe_AB(ROW,3) > 1
RGB_Axe_AB(ROW,3) = 1;
end
end
RGB_Axe_AB_ColorMap = colormap(RGB_Axe_AB)
n=21
colormap(RGB_Axe_AB_ColorMap(21))
pcolor([1:n+1;1:n+1])
And this is the image -- what a beauty! :-)
Needless to say, there's still a ton of work to do. This is very encouraging. Thank you, Mr. Matlab :-)
I owe you a beer :-)
Making progress, using this code :
% Generate three vectors
Axe_A_B = transpose(linspace(-128, 128, 21));
Axe_La = transpose(linspace(39, 50, 21));
Axe_Lb_Zero = transpose(linspace(0, 0, 21));
Axe_Lb = transpose(linspace(15,100, 21));
Lab_Axe_AB = [Axe_La, Axe_A_B, Axe_Lb_Zero]; % 21 x 3 double
RGB_Axe_AB = lab2rgb(Lab_Axe_AB)
% negatives values?
[row,col] = size(RGB_Axe_AB);
for ROW = 1:row
if RGB_Axe_AB(ROW,1) < 0
RGB_Axe_AB(ROW,1) = 0;
end
if RGB_Axe_AB(ROW,1) > 1
RGB_Axe_AB(ROW,1) = 1;
end
if RGB_Axe_AB(ROW,2) < 0
RGB_Axe_AB(ROW,2) = 0;
end
if RGB_Axe_AB(ROW,2) > 1
RGB_Axe_AB(ROW,2) = 1;
end
if RGB_Axe_AB(ROW,3) < 0
RGB_Axe_AB(ROW,3) = 0;
end
if RGB_Axe_AB(ROW,3) > 1
RGB_Axe_AB(ROW,3) = 1;
end
end
RGB_Axe_AB_ColorMap = colormap(RGB_Axe_AB)
Xcoord = linspace(-128, 128, 21);
Ycoord = linspace(0, 0, 21);
plot(Xcoord,Ycoord, 'LineWidth', 2, 'MarkerSize', 10);
axis([-128 128 -128 128]);
xticks([-128 -96 -64 -32 0 32 64 96 128]);
yticks([-128 -96 -64 -32 0 32 64 96 128]);
xs = linspace(-128, 128, 21);
hold on;
colorList = RGB_Axe_AB_ColorMap;
xAxisWidth = 10;
% k = -128 : 6.0952 : 128
xRef=-128
for k = 1 : 21
thisColor = colorList(k, :);
line([xRef, xRef + 12.1905], [0, 0], 'Color', thisColor, 'LineWidth', xAxisWidth)
xRef = xRef + 12.1905
end
And this is the result :
Almost feel like putting my feet up, now :-)
Thanks everyone for your kind and patient help!
BTW, I like the 'interactive" way of working with Matlab.
Is it ever complicated (read 'abstract') to work in 3D!
Got this encouraging result :
Using this humble code :
% Generate three vectors
Axe_A_B = transpose(linspace(-128, 128, 21));
Axe_La = transpose(linspace(39, 50, 21)); % Green to Magenta
Axe_Lb_Zero = transpose(linspace(0, 0, 21));
Axe_Lb = transpose(linspace(15,100, 21)); % Blue to Yellow
Lab_Axe_AB = [Axe_La, Axe_A_B, Axe_Lb_Zero]; % 21 x 3 double
RGB_Axe_AB = lab2rgb(Lab_Axe_AB) % Green to Magenta
Lab_Axe_BA = [Axe_La, Axe_Lb_Zero, Axe_A_B]; % 21 x 3 double
RGB_Axe_BA = lab2rgb(Lab_Axe_BA) % Yellow to Blue
% negatives values?
[row,col] = size(RGB_Axe_AB);
% Process Green/Magenta axis first
for ROW = 1:row
if RGB_Axe_AB(ROW,1) < 0
RGB_Axe_AB(ROW,1) = 0;
end
if RGB_Axe_AB(ROW,1) > 1
RGB_Axe_AB(ROW,1) = 1;
end
if RGB_Axe_AB(ROW,2) < 0
RGB_Axe_AB(ROW,2) = 0;
end
if RGB_Axe_AB(ROW,2) > 1
RGB_Axe_AB(ROW,2) = 1;
end
if RGB_Axe_AB(ROW,3) < 0
RGB_Axe_AB(ROW,3) = 0;
end
if RGB_Axe_AB(ROW,3) > 1
RGB_Axe_AB(ROW,3) = 1;
end
end
[row,col] = size(RGB_Axe_BA);
% Process Yellow/Blue axis second
for ROW = 1:row
if RGB_Axe_BA(ROW,1) < 0
RGB_Axe_BA(ROW,1) = 0;
end
if RGB_Axe_BA(ROW,1) > 1
RGB_Axe_BA(ROW,1) = 1;
end
if RGB_Axe_BA(ROW,2) < 0
RGB_Axe_BA(ROW,2) = 0;
end
if RGB_Axe_BA(ROW,2) > 1
RGB_Axe_BA(ROW,2) = 1;
end
% fprintf('RGB_Axe_BA(ROW,3) Avant = %4.2f\n', RGB_Axe_BA(ROW,3))
if RGB_Axe_BA(ROW,3) < 0
RGB_Axe_BA(ROW,3) = 0;
end
if RGB_Axe_BA(ROW,3) > 1
RGB_Axe_BA(ROW,3) = 1;
end
% fprintf('RGB_Axe_BA(ROW,3) Après = %4.2f\n', RGB_Axe_BA(ROW,3))
end
RGB_Axe_AB_ColorMap = colormap(RGB_Axe_AB) % Green to Magenta colormap
RGB_Axe_BA_ColorMap = colormap(RGB_Axe_BA) % Yellow to Blue colormap
Xcoord = linspace(-128, 128, 21); % 1 x 21
Ycoord = linspace(0, 0, 21); % 1 x 21
Zcoord = linspace(0,100, 21);
Line3D = plot3(Xcoord,Ycoord, Zcoord); grid on; hold on;
set(Line3D, 'Visible', 'off');
xlabel('X axis a* (-128 +128)');
ylabel('Y axis b* (-128 +128)');
zlabel('Z axis L* (0 100)');
axis([-128 128 -128 128 0 100]);
xticks([-128 -96 -64 -32 0 32 64 96 128]);
yticks([-128 -96 -64 -32 0 32 64 96 128]);
zticks([0 10 20 30 40 50 60 70 80 90 100]);
% plot3(-128,-128, 0, 'o')
% plot3(-96,-128, 0, 'o')
xs = linspace(-128, 128, 21);
hold on;
colorListAB = RGB_Axe_AB_ColorMap; % Green to Magenta ColorList
colorListBA = RGB_Axe_BA_ColorMap; % Blue to Yellow ColorList
xAxisWidth = 5;
xRef=-128
yRef=-128
% line([-128, 128], [-128, -128], [0,0], 'Color', thisColor, 'LineWidth', xAxisWidth)
for k = 1 : 21
thisColor = colorListAB(k, :);
line([xRef, xRef + 12.1905], [yRef, yRef], [0, 0], 'Color', thisColor, 'LineWidth', xAxisWidth)
xRef = xRef + 12.1905
end
% plot3(-128,128, 0, 'o')
% plot3(-128,96, 0, 'o')
% line([-128, -128], [128, 96], [0,0], 'Color', thisColor, 'LineWidth', xAxisWidth)
xRef=-128
yRef=128
for k = 1 : 21
thisColor = colorListBA(k, :);
line([xRef, xRef], [yRef, yRef - 12.1905], [0, 0], 'Color', thisColor, 'LineWidth', xAxisWidth)
yRef = yRef - 12.1905
end
I had to resort to plotting individual points in order to visualize the required coordinates.
There are still points which puzzles me but I know I'm in the right direction.
Thank you all for your help and patience.
"One more thing" as Steve Jobs used to say... My axes were "reversed"? So, I thought I was going to crawl under untangling the correct logic but I only changed a few lines and here is the correct result :
I had to change these lines of code :
% Generate three vectors
Axe_A_B = transpose(linspace(-128, 128, 21));
Axe_B_A = transpose(linspace(128, -128, 21));
Axe_La = transpose(linspace(39, 50, 21)); % Green to Magenta
Axe_Lb_Zero = transpose(linspace(0, 0, 21));
Axe_Lb = transpose(linspace(15,100, 21)); % Blue to Yellow
Lab_Axe_AB = [Axe_La, Axe_A_B, Axe_Lb_Zero]; % 21 x 3 double
RGB_Axe_AB = lab2rgb(Lab_Axe_AB) % Green to Magenta
Lab_Axe_BA = [Axe_La, Axe_Lb_Zero, Axe_B_A]; % 21 x 3 double
RGB_Axe_BA = lab2rgb(Lab_Axe_BA) % Yellow to Blue
Still many, many, many details to work on but, I'm so please with this result, so far.
Rome was not built in one day... I spotted an error in the appearance of yellow? This is the corrected code :
% Generate three vectors
Axe_A_B = transpose(linspace(-128, 128, 21));
Axe_B_A = transpose(linspace(-128, 128, 21));
Axe_La = transpose(linspace(39, 50, 21)); % Green to Magenta
Axe_Lb_Zero = transpose(linspace(0, 0, 21));
Axe_Lb = transpose(linspace(15,100, 21)); % Blue to Yellow
Lab_Axe_AB = [Axe_La, Axe_A_B, Axe_Lb_Zero]; % 21 x 3 double
RGB_Axe_AB = lab2rgb(Lab_Axe_AB) % Green to Magenta
Lab_Axe_BA = [Axe_Lb, Axe_Lb_Zero, Axe_B_A]; % 21 x 3 double
Flipped = flipud(Lab_Axe_BA);
% RGB_Axe_BA = lab2rgb(Lab_Axe_BA) % Yellow to Blue
RGB_Axe_BA = lab2rgb(Flipped) % Yellow to Blue
Had to flip the array upside down, to get the correct Lab (RGB) values to appear at the correct (b* = +128) end of the scale. Silly me. Now this is better :

Sign in to comment.

More Answers (0)

Categories

Find more on Display Image in Help Center and File Exchange

Products

Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!