Colormap and color wheel from CIELAB colour space

46 views (last 30 days)
Justin
Justin on 4 Apr 2012
Moved: DGM on 29 Dec 2023
Hi there, I need to generate a CIELAB space colour map . . .which will then be used to generate a colour wheel from the CIELAB space as well. The code I currently have (left from a previous coworker) generated a colour space in HSV ( I have included this code below). I'm sorry for the rudimentary nature of this question, but I don't really understand how to go about generating the map. Any feedback here would be appreciated! Thanks in advance!
for i = 1:252
val = 1/252*i
H = val; S = 1; V = 1;
if S == 0 % HSV from 0 to 1
R = V * 255;
G = V * 255;
B = V * 255;
else
var_h = H * 6;
if ( var_h == 6 )
var_h = 0; %//H must be < 1
end
var_i = floor( var_h ); %//Or ... var_i = floor( var_h );
var_1 = V * ( 1 - S );
var_2 = V * ( 1 - S * ( var_h - var_i ) );
var_3 = V * ( 1 - S * ( 1 - ( var_h - var_i ) ) );
if ( var_i == 0 )
var_r = V;
var_g = var_3;
var_b = var_1;
elseif ( var_i == 1 )
var_r = var_2;
var_g = V;
var_b = var_1;
elseif ( var_i == 2 )
var_r = var_1;
var_g = V;
var_b = var_3;
elseif ( var_i == 3 )
var_r = var_1;
var_g = var_2;
var_b = V;
elseif ( var_i == 4 )
var_r = var_3;
var_g = var_1;
var_b = V;
else
var_r = V;
var_g = var_1;
var_b = var_2;
end
R = var_r * 255; % //RGB results from 0 to 255
G = var_g * 255;
B = var_b * 255;
end
newmap(i,1:3) = [R G B];
end

Answers (3)

Image Analyst
Image Analyst on 4 Apr 2012
See this demo for converting an image to CIELAB colorspace: http://www.mathworks.com/products/demos/image/color_seg_k/ipexhistology.html
IMPORTANT NOTE: This will not give you CIELAB colorspace unless you have a calibrated monitor. Do not underestimate the importance of that. Otherwise you have just a "book formula" that give you some kind of colors out but, as you know, they may look different on different monitors, not to mention that they won't even be the TRUE LAB colors. You NEED to calibrate your monitor if you're going to do psychophysical exeriments. Even then, you could be off by up to 5 or 10 Delta E from the "true" value.
Can a monitor display indigo or violet? No. Why not? Because the filters are red, green, and blue. Blue is a longer wavelength than indigo or violet. So try to display violet and you will get mostly blue with a very very little amount of actual violet being emitted.

Image Analyst
Image Analyst on 4 Apr 2012
There is an hsv color map you know. Just type hsv(256) on the command line and you'll see. Just make up your grayscale image and apply the hsv colormap. Will that work for you?
  1 Comment
Justin
Justin on 4 Apr 2012
Moved: DGM on 29 Dec 2023
Hi there, Thanks for your response. Unfortunately we need to program our stimuli in CIELAB colour space . . .it's for an experiment and the CIELAB colour space is based on psychophysics, as opposed to HSV which is more of a designer colour space.
Thanks,
Justin

Sign in to comment.


DGM
DGM on 22 Feb 2023
Edited: DGM on 15 May 2023
I've read this a couple times, and the task reads as if it's either silly or nontrivial, but I can't tell which.
Creating "a color wheel in LAB" on the basis that the properties of the color space are important seems like an exercise in using a good tool in a way which defeats the reasons for using it. If you are working on the assumption that the primary-secondary corners are (implicitly):
  1. uniformly spaced in CIELAB and
  2. on some continuous circular locus which maps back to sRGB without losing information,
then you'll be disappointed. Neither is the case. Those points aren't even coplanar. Trying to distort things to make that work simply undoes any benefits that the properties of the parent space gives you.
So what exactly does "color wheel" mean? Dead questions are open to interpretation, so I choose to assume that the HSV example implies the objective. Find a neutral-centered circular region in the chosen colorspace which maps back to sRGB in a way which does not cause problems. In other words, we need the circular region to lie within the sRGB gamut. The region should have a constant brightness metric (L).
I'm going to disregard details and just use lab2rgb(). With that assumption, the largest such circular region lies at approximately L = 74, with a maximum chroma of about 40.
% generate a slice at the point where the projection of sRGB
% has the largest inscribed circular disk with a neutral center
r = 500;
maxc = 80;
[A B] = meshgrid(linspace(-maxc,maxc,r*2+1));
B = flipud(B); % make sure the image is the right side up
L = 73.83*ones(size(A));
labpict = cat(3,L,A,B);
% convert to sRGB (d65, fwiw)
rgbpict = lab2rgb(labpict);
% generate some masks
oog = any(rgbpict>1 | rgbpict<0,3); % values that are OOG
circmask = hypot(A,B)>40; % effective maximum "color wheel"
% apply the masks for visualization purposes
mask = (oog+circmask)/4;
rgbpict = rgbpict.*(1-mask);
imshow(rgbpict,'border','tight')
That's an awfully small circle, and it's an awfully pastel one. Can't we include more bold colors? Note the shape of the gamut boundary and where the primary-secondary colors must lie.
Can you create a bold rainbow colormap in LAB? Yes, but it won't satisfy the arbitrary assumptions I've made. Let's start with what we have.
L = 74; C = 40; % the given circular locus of maximum chroma
N = 256; % length of color table
L = L*ones(N,1);
H = linspace(0,360,N).';
A = C*cosd(H);
B = C*sind(H);
% for a colormap to be used by any of the tools,
% its values must lie within [0 1]
CT = imclamp(lab2rgb([L A B]));
% display it as a swatch
imagesc(permute(CT,[3 1 2]))
That looks nice and smooth. The brightness appears quite uniform. Using MIMT ctpath to visualize, we see that these colors indeed lie on a circular locus in LAB:
ctpath(CT,'lab') % needs MIMT
view(245,14)
What if we increase the chroma a bit?
L = 74; C = 60;
Maybe try to catch some bold colors?
L = 32; C = 134; % get the blue corner
It might be hard to tell, but that's not a circle. So what's that look like as a swatch?
All those "perceptually-important" properties of LAB? We just threw all that away.
Alternatively, maybe you literally just want to take the HSV sweep and interpolate it so that it has linear hue in LAB. That's a different goal. To that end, see this thread and this comment.

Community Treasure Hunt

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

Start Hunting!