mathieu@14: function rgb=hsl2rgb(hsl) mathieu@14: mathieu@14: %Converts Hue-Saturation-Luminance Color value to Red-Green-Blue Color value mathieu@14: % mathieu@14: %Usage mathieu@14: % RGB = hsl2rgb(HSL) mathieu@14: % mathieu@14: % converts HSL, a M X 3 color matrix with values between 0 and 1 mathieu@14: % into RGB, a M X 3 color matrix with values between 0 and 1 mathieu@14: % mathieu@14: %See also rgb2hsl, rgb2hsv, hsv2rgb mathieu@14: mathieu@14: %Suresh E Joel, April 26,2003 mathieu@14: mathieu@14: if nargin<1, mathieu@14: error('Too few arguements for hsl2rgb'); mathieu@14: return; mathieu@14: elseif nargin>1, mathieu@14: error('Too many arguements for hsl2rgb'); mathieu@14: return; mathieu@14: end; mathieu@14: mathieu@14: if max(max(hsl))>1 | min(min(hsl))<0, mathieu@14: error('HSL values have to be between 0 and 1'); mathieu@14: return; mathieu@14: end; mathieu@14: mathieu@14: for i=1:size(hsl,1), mathieu@14: if hsl(i,2)==0,%when sat is 0 mathieu@14: rgb(i,1:3)=hsl(i,3);% all values are same as luminance mathieu@14: end; mathieu@14: if hsl(i,3)<0.5, mathieu@14: temp2=hsl(i,3)*(1+hsl(i,2)); mathieu@14: else mathieu@14: temp2=hsl(i,3)+hsl(i,2)-hsl(i,3)*hsl(i,2); mathieu@14: end; mathieu@14: temp1=2*hsl(i,3)-temp2; mathieu@14: temp3(1)=hsl(i,1)+1/3; mathieu@14: temp3(2)=hsl(i,1); mathieu@14: temp3(3)=hsl(i,1)-1/3; mathieu@14: for j=1:3, mathieu@14: if temp3(j)>1, mathieu@14: temp3(j)=temp3(j)-1; mathieu@14: elseif temp3(j)<0, mathieu@14: temp3(j)=temp3(j)+1; mathieu@14: end; mathieu@14: if 6*temp3(j)<1, mathieu@14: rgb(i,j)=temp1+(temp2-temp1)*6*temp3(j); mathieu@14: elseif 2*temp3(j)<1, mathieu@14: rgb(i,j)=temp2; mathieu@14: elseif 3*temp3(j)<2, mathieu@14: rgb(i,j)=temp1+(temp2-temp1)*(2/3-temp3(j))*6; mathieu@14: else mathieu@14: rgb(i,j)=temp1; mathieu@14: end; mathieu@14: end; mathieu@14: end; mathieu@14: mathieu@14: rgb=round(rgb.*100000)./100000; %Sometimes the result is 1+eps instead of 1 or 0-eps instead of 0 ... so to get rid of this I am rounding to 5 decimal places)