MATLAB: How to draw a L*a*b* colour space or HSV colour space (2D)

plot hsv colour spaceplot lab colour spacepooler

Hi
I would like to draw the LAB or HSV colour space in 2D (see below) but I don't know how to draw it.
hsl.jpg
I found some code online (see below) which do something very similar (see the graph below) but NOT the same. The main difference is the colour at the center, it should be white, and the colours should graduately getting more and more saturated instead of one same colour from center all the way to the edge. What do I need to do in order to obtain a plot like the first graph??
r = linspace(0,1,10);
theta = linspace(0, 2*pi, 100);
[rg, thg] = meshgrid(r,theta);
[x,y] = pol2cart(thg,rg);
pcolor(x,y,thg);
colormap(hsv);
shading flat;
axis equal;
untitled.jpg

Best Answer

As commented in your question, note that you're only plotting one slice of the 3D colour space.
Of course, your original code doesn't work, you end up doing
pcolor(x, y, thg);
so you're only plotting the hue (angle) against x and y. There's no saturation or value information in your plot. You then apply a colour map called hsv whose colours are derived from the hsv cylinder, but in no way does it mean you've done any conversion to/from hsv. The whole idea is flawed.
Here is how I'd do it:
%inputs:
%plotradius: the plot radius of the disk. Image will be 2*plotradius+1 x 2*plotradius+1
%plotvalue: the Value at which the slice of the HSV cylinder is to be taken. In the range 0-1
plotradius = 100;
plotvalue = 1;
[x, y] = meshgrid(-plotradius:plotradius); %get x,y coordinates of pixels in image
[hue, saturation] = cart2pol(x, y); %convert to angle, radius. Angle is the hue, radius is the saturation in the HSV cylinder
hue = (hue + pi) / (2*pi); %rescale from -pi:pi to 0:1 since matlab use 0:1 for the range of the hue
saturation = saturation / plotradius; %rescale saturation to range 0:1. Not that anything above 1 is outside the cylinder
value = ones(size(hue)) * plotvalue; %set value constant for all points in the disk
%now set points outside the disk (saturation >1) to white. That'd be a value of 1 and saturation of 0. hue doesn't matter
outsidedisk = saturation > 1;
saturation(outsidedisk) = 0;
value(outsidedisk) = 1;
%finally, convert hsv to rgb and plot
rgb = hsv2rgb(cat(3, hue, saturation, value));
imshow(rgb);