File Exchange

image thumbnail

RGB to HSL

version 1.0.0.0 (1.58 KB) by Suresh Joel
Convert from RGB to HSL and HSL to RGB.

10 Downloads

Updated 31 Mar 2016

View License

Functions to convert RGB colorspace to HSL colorspace and vice-versa.

Cite As

Suresh Joel (2021). RGB to HSL (https://www.mathworks.com/matlabcentral/fileexchange/3360-rgb-to-hsl), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (5)

yun l

不能用于三维数组的图片,还要把图片转成二维,37行的return要改成continue,不然结果差值;最好对hsl提前分配空间

Baptiste

"RGB values have to be between 0 and 1"

This file look like a bit out of date now :s

Daniel

Remove the second last line in both functions to increase the precision (`rgb=round...`)

David Holdaway

without the change it returns whenever you have a 1,1,1 vector. With the change suggested it works ok. I don't like the treatment of all the same colour (e.g. [a,a,a]) though, it should interpolate the change in other colours rather than just turning it to [0,0,0]. I added the following block
if(mx-mn)==0,%if all three colors have same value,
if i==1
hsl(i,2)=0;%then s=0 and
hsl(i,1)=0;%h is undefined but for practical reasons 0
elseif i==2
hsl(i,2)=hsl(i-1,2);%take previous value
hsl(i,1)=hsl(i-1,1);%take previous value
else
hsl(i,2)=2*hsl(i-1,2)-hsl(i-2,2);%interpolate
if hsl(i,2) > 1
hsl(i,2) =1;
elseif hsl(i,2) < 0
hsl(i,2) =0;
end
hsl(i,1)=2*hsl(i-1,1)-hsl(i-2,1);%interpolate
if hsl(i,1) > 1
hsl(i,1) =1;
elseif hsl(i,1) < 0
hsl(i,1) =0;
end
end
continue;
end;

daniel weiler

BUG-REPORT!

Please replace the last return statement with continue... or there will be bad surprises

MATLAB Release Compatibility
Created with R11.1
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired: hsl2rgb and rgb2hsl conversion

Community Treasure Hunt

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

Start Hunting!