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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用MATLAB编写RGB到HSL和HSL到RGB颜色空间的转换程序:rgb2hsl.m和hsl2rgb.m相关的知识,希望对你有一定的参考价值。

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

参考技术A 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+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,
hsl(i,2)=(mx-mn)/(mx+mn);
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
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
hsl(i,1)=(2+(rgb(i,3)-rgb(i,1))/(mx-mn))/6;
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;

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)本回答被提问者采纳

AS3-HSL到RGB类

This requires the [RGB](http://snipplr.com/view/34818/as3--rgb-class-for-rgb-to-hex/) class I posted as well
  1. /**
  2.  * @author Rivaledsouls
  3.  * @version 1.0
  4.  * Feel free to use this code as you wish as long as you cite
  5.  * me, Rivaledsouls, for use of the code in any projects where
  6.  * you share the source.
  7.  * (c) 2010
  8.  */
  9.  
  10. package utils.color{
  11. public class HSL {
  12. private var luminance:Number;
  13. private var saturation:Number;
  14. private var hue:Number;
  15.  
  16. /**
  17. * Contructs an HSL object for containing Hue Saturation and Luminance values, and
  18. * converting them to an RGB or Hex color
  19. * @param h Hue (0-360)
  20. * @param s Saturation(0-1)
  21. * @param l Luminance(0-1)
  22. */
  23. public function HSL(h:Number=0, s:Number=1, l:Number=.5) {
  24. hue = h;
  25. saturation = s;
  26. luminance = l;
  27. }
  28.  
  29. /*
  30. * Getters for each channel
  31. */
  32. public function get Hue():Number {
  33. return hue;
  34. }
  35. public function get Saturation():Number {
  36. return saturation;
  37. }
  38. public function get Luminance():Number {
  39. return luminance;
  40. }
  41. /*
  42. *Setters for each channel
  43. */
  44. public function set Hue(Value:Number):void {
  45. hue = (Value>360)? Value-360 : ((Value<0)?Value+360:Value);
  46. }
  47. public function set Saturation(Value:Number):void {
  48. saturation = (Value>1)? 1 : ((Value<0)?0:Value);
  49. }
  50. public function set Luminance(Value:Number):void {
  51. luminance = (Value>1)? 1 : ((Value<0)?0:Value);
  52. }
  53. /**
  54. * Converts the HSL object to an RGB object defining the same color
  55. * @return RGB object
  56. */
  57. public function toRGB():RGB{
  58. return getRGB(hue, saturation, luminance);
  59. }
  60. /**
  61. * Converts the HSL object to a Hex value defining the same color
  62. * @return Hex value of HSL color
  63. */
  64. public function toHex():uint {
  65. return toRGB().Hex;
  66. }
  67.  
  68. /**
  69. * Static method for directly converting HSL values to Hex
  70. * @param h Hue (0-360)
  71. * @param s Saturation(0-1)
  72. * @param l Luminance(0-1)
  73. * @return a Hex value represnting the given HSL values
  74. */
  75. public static function getHex(h:Number, s:Number, l:Number):uint{
  76. return getRGB(h, s, l).Hex;
  77. }
  78. /**
  79. * Static method for directly converting HSL values to
  80. * an RGB object
  81. * @param h Hue (0-360)
  82. * @param s Saturation(0-1)
  83. * @param l Luminance(0-1)
  84. * @return an RGB object represnting the given HSL values
  85. */
  86. public static function getRGB(h:Number, s:Number, l:Number):RGB{
  87. h = h / 360;
  88. var r:Number;
  89. var g:Number;
  90. var b:Number;
  91.  
  92. if(l==0)
  93. {
  94. r=g=b=0;
  95. }
  96. else
  97. {
  98. if(s == 0)
  99. {
  100. r=g=b=l;
  101. }
  102. else
  103. {
  104. var t2:Number = (l<=0.5)? l*(1+s):l+s-(l*s);
  105. var t1:Number = 2*l-t2;
  106. var t3:Vector.<Number> = new Vector.<Number>();
  107. t3.push(h+1/3);
  108. t3.push(h);
  109. t3.push(h-1/3);
  110. var clr:Vector.<Number> = new Vector.<Number>();
  111. clr.push(0);
  112. clr.push(0);
  113. clr.push(0);
  114. for(var i:int=0;i<3;i++)
  115. {
  116. if(t3[i]<0)
  117. t3[i]+=1;
  118. if(t3[i]>1)
  119. t3[i]-=1;
  120.  
  121. if(6*t3[i] < 1)
  122. clr[i]=t1+(t2-t1)*t3[i]*6;
  123. else if(2*t3[i]<1)
  124. clr[i]=t2;
  125. else if(3*t3[i]<2)
  126. clr[i]=(t1+(t2-t1)*((2/3)-t3[i])*6);
  127. else
  128. clr[i]=t1;
  129. }
  130. r=clr[0];
  131. g=clr[1];
  132. b=clr[2];
  133. }
  134. }
  135. return new RGB(int(r*255),int(g*255),int(b*255));
  136. }
  137. }
  138.  
  139. }

以上是关于用MATLAB编写RGB到HSL和HSL到RGB颜色空间的转换程序:rgb2hsl.m和hsl2rgb.m的主要内容,如果未能解决你的问题,请参考以下文章

RGB 到 HSL 函数中的饱和度计算不正确

AS3-HSL到RGB类

ActionScript 3 AS3 - HSL到RGB类

rgb和hsl转换(js)

将 Hsl 转换为 rgb 和 hex

VB中HSL怎么转换为RGB?