用MATLAB编写RGB到HSL和HSL到RGB颜色空间的转换程序:rgb2hsl.m和hsl2rgb.m

来源:百度知道 编辑:UC知道 时间:2024/05/24 23:17:03
用MATLAB编写RGB到HSL和HSL到RGB颜色空间的转换程序:rgb2hsl.m和hsl2rgb.m

function hsl=rgb2hsl(rgb)

%Converts Red-Green-Blue Color value to Hue-Saturation-Luminance Color value
%
%Usage
% HSL = rgb2hsl(RGB)
%
% 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
%
%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;

if max(max(rgb))>1 | min(min(rgb))<0,
error('RGB values have to be between 0 and 1');
return;
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+