Skip to content
Snippets Groups Projects
Commit 4988327b authored by Yoann Guilhem's avatar Yoann Guilhem Committed by Nicola Vigano
Browse files

Commenting, formating and adding a variable for progress output

git-svn-id: https://svn.code.sf.net/p/dct/code/trunk@857 4c865b51-4357-4376-afb4-474e03ccb993
parent 982080c4
No related branches found
No related tags found
No related merge requests found
function rgb=hsl2rgb(hsl)
%Converts Hue-Saturation-Luminance Color value to Red-Green-Blue Color value
function rgb = hsl2rgb(hsl, progress)
% HSL2RGB Converts Hue-Saturation-Luminance color values to Red-Green-Blue
% rgb = hsl2rgb(hsl[, progress])
% -------------------------------------------------------------------------
% Converts HSL, a M X 3 color matrix with values between 0 and 1
% into RGB, a M X 3 color matrix with values between 0 and 1
%
%Usage
% RGB = hsl2rgb(HSL)
% INPUT:
% hsl = <double> Hue-Saturation-Luminance color values
%
% converts HSL, a M X 3 color matrix with values between 0 and 1
% into RGB, a M X 3 color matrix with values between 0 and 1
% OPTIONAL INPUT:
% progress = <any> Display progress if more than 1000 color values
%
%See also rgb2hsl, rgb2hsv, hsv2rgb
%Suresh E Joel, April 26,2003
if nargin<1,
error('Too few arguements for hsl2rgb');
return;
elseif nargin>1,
error('Too many arguements for hsl2rgb');
return;
end;
% OUTPUT:
% rgb = <double> Red-Green-Blue color values
%
% Note:
% See also rgb2hsl, rgb2hsv, hsv2rgb
%
%
% Version 002 16-10-2012 by YGuilhem
% Add an option to display progress report
%
% Version 001 26-04-2003 by Suresh E Joel
if max(max(hsl))>1 || min(min(hsl))<0,
error('HSL values have to be between 0 and 1');
return;
end;
todo=size(hsl, 1);
%preallocate rgb
rgb=zeros(size(hsl));
todo = size(hsl, 1);
if ~exist('progress', 'var')
progress = false;
elseif todo>1000
progress = true;
end
% preallocate rgb
rgb = zeros(size(hsl));
for i=1:size(hsl,1),
if hsl(i,2)==0,%when sat is 0
rgb(i,1:3)=hsl(i,3);% all values are same as luminance
if hsl(i,2)==0, % when sat is 0
rgb(i,1:3)=hsl(i,3); % all values are same as luminance
end;
if hsl(i,3)<0.5,
temp2=hsl(i,3)*(1+hsl(i,2));
......@@ -59,14 +68,14 @@ for i=1:size(hsl,1),
end;
end;
%give a progress report for long lists
if todo>1000
if mod(i, 1000)==0
100*i/todo
end
% Give a progress report for long lists
if progress && mod(i, 1000)==0
disp(sprintf('% 5.1f %%', 100*i/todo));
end
end
end;
% 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)
rgb = round(rgb.*100000)./100000;
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)
\ No newline at end of file
end % end of function
function hsl=rgb2hsl(rgb)
%Converts Red-Green-Blue Color value to Hue-Saturation-Luminance Color value
function hsl = rgb2hsl(rgb, progress)
% RGB2HSL Converts Red-Green-Blue color values to Hue-Saturation-Luminance
% hsl = rgb2hsl(rgb[, progress])
% -------------------------------------------------------------------------
% Converts RGB, a M X 3 color matrix with values between 0 and 1
% into HSL, a M X 3 color matrix with values between 0 and 1
%
%Usage
% HSL = rgb2hsl(RGB)
% INPUT:
% hsl = <double> Hue-Saturation-Luminance color values
%
% converts RGB, a M X 3 color matrix with values between 0 and 1
% into HSL, a M X 3 color matrix with values between 0 and 1
% OPTIONAL INPUT:
% progress = <any> Display progress if more than 1000 color values
%
%See also hsl2rgb, rgb2hsv, hsv2rgb
%Suresh E Joel, April 26,2003
if nargin<1,
error('Too few arguements for rgb2hsl');
return;
elseif nargin>1,
error('Too many arguements for rgb2hsl');
return;
end;
% OUTPUT:
% rgb = <double> Red-Green-Blue color values
%
% Note:
% See also hsl2rgb, rgb2hsv, hsv2rgb
%
%
% Version 002 16-10-2012 by YGuilhem
% Add an option to display progress report
%
% Version 001 26-04-2003 by Suresh E Joel
if max(max(rgb))>1 || min(min(rgb))<0,
error('RGB values have to be between 0 and 1');
return;
end;
todo = size(rgb, 1);
if ~exist('progress', 'var')
progress = false;
elseif todo>1000
progress = true;
end
for i=1:size(rgb,1),
mx=max(rgb(i,:));%max of the 3 colors
mn=min(rgb(i,:));%min of the 3 colors
imx=find(rgb(i,:)==mx);%which color has the max
hsl(i,3)=(mx+mn)/2;%luminance is half of max value + min value
if(mx-mn)==0,%if all three colors have same value,
hsl(i,2)=0;%then s=0 and
hsl(i,1)=0;%h is undefined but for practical reasons 0
mx=max(rgb(i,:)); % max of the 3 colors
mn=min(rgb(i,:)); % min of the 3 colors
imx=find(rgb(i,:)==mx); % which color has the max
hsl(i,3)=(mx+mn)/2; % luminance is half of max value + min value
if(mx-mn)==0, % if all three colors have same value,
hsl(i,2)=0; % then s=0 and
hsl(i,1)=0; % h is undefined but for practical reasons 0
return;
end;
if hsl(i,3)<0.5,
......@@ -40,7 +50,7 @@ for i=1:size(rgb,1),
else
hsl(i,2)=(mx-mn)/(2-(mx+mn));
end;
switch(imx(1))%if two colors have same value and be the maximum, use the first color
switch(imx(1)) % if two colors have same value and be the maximum, use the first color
case 1 %Red is the max color
hsl(i,1)=((rgb(i,2)-rgb(i,3))/(mx-mn))/6;
case 2 %Green is the max color
......@@ -48,7 +58,18 @@ for i=1:size(rgb,1),
case 3 %Blue is the max color
hsl(i,1)=(4+(rgb(i,1)-rgb(i,2))/(mx-mn))/6;
end;
if hsl(i,1)<0,hsl(i,1)=hsl(i,1)+1;end;%if hue is negative, add 1 to get it within 0 and 1
end;
if hsl(i,1)<0, % if hue is negative, add 1 to get it within 0 and 1
hsl(i,1)=hsl(i,1)+1;
end;
% Give a progress report for long lists
if progress && mod(i, 1000)==0
disp(sprintf('% 5.1f %%', 100*i/todo));
end
end
% 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)
hsl = round(hsl*100000)/100000;
hsl=round(hsl*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)
\ No newline at end of file
end % end of function
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment