Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# RGB到HSL,色调计算错误_C#_Rgb_Color Space_Hsl - Fatal编程技术网

C# RGB到HSL,色调计算错误

C# RGB到HSL,色调计算错误,c#,rgb,color-space,hsl,C#,Rgb,Color Space,Hsl,我正在尝试将RGB32值转换为HSL,因为我想使用色调组件 我使用了我在网上找到的一些例子来创建这个类: public class HSLColor { public Double Hue; public Double Saturation; public Double Luminosity; public HSLColor(Double H, Double S, Double L) {

我正在尝试将RGB32值转换为HSL,因为我想使用色调组件

我使用了我在网上找到的一些例子来创建这个类:

    public class HSLColor
    {
        public Double Hue;
        public Double Saturation;
        public Double Luminosity;

        public HSLColor(Double H, Double S, Double L)
        {
            Hue = H;
            Saturation = S;
            Luminosity = L;
        }

        public static HSLColor FromRGB(Color Clr)
        {
            return FromRGB(Clr.R, Clr.G, Clr.B);
        }

        public static HSLColor FromRGB(Byte R, Byte G, Byte B)
        {
            Double _R = (R / 255d);
            Double _G = (G / 255d);
            Double _B = (B / 255d);

            Double _Min = Math.Min(Math.Min(_R, _G), _B);
            Double _Max = Math.Max(Math.Max(_R, _G), _B);
            Double _Delta = _Max - _Min;

            Double H = 0;
            Double S = 0;
            Double L = (float)((_Max + _Min) / 2.0f);

            if (_Delta != 0)
            {
                if (L < 0.5d)
                {
                    S = (float)(_Delta / (_Max + _Min));
                }
                else
                {
                    S = (float)(_Delta / (2.0f - _Max - _Min));
                }


                if (_R == _Max)
                {
                    H = (_G - _B) / _Delta;
                }
                else if (_G == _Max)
                {
                    H = 2f + (_B - _R) / _Delta;
                }
                else if (_B == _Max)
                {
                    H = 4f + (_R - _G) / _Delta;
                }
            }

            //Convert to degrees
            H = H * 60d;
            if (H < 0) H += 360;
            //Convert to percent
            S *= 100d;
            L *= 100d;

            return new HSLColor(H, S, L);
        }

        private Double Hue_2_RGB(Double v1, Double v2, Double vH)
        {
            if (vH < 0) vH += 1;
            if (vH > 1) vH -= 1;
            if ((6.0d * vH) < 1) return (v1 + (v2 - v1) * 6 * vH);
            if ((2.0d * vH) < 1) return (v2);
            if ((3.0d * vH) < 2) return (v1 + (v2 - v1) * ((2.0d / 3.0d) - vH) * 6.0d);
            return (v1);
        }

        public Color ToRGB()
        {
            Color Clr = new Color();
            Double var_1, var_2;

            if (Saturation == 0)
            {
                Clr.R = (Byte)(Luminosity * 255);
                Clr.G = (Byte)(Luminosity * 255);
                Clr.B = (Byte)(Luminosity * 255);
            }
            else
            {
                if (Luminosity < 0.5) var_2 = Luminosity * (1 + Saturation);
                else var_2 = (Luminosity + Saturation) - (Saturation * Luminosity);

                var_1 = 2 * Luminosity - var_2;

                Clr.R = (Byte)(255 * Hue_2_RGB(var_1, var_2, Hue + (1 / 3)));
                Clr.G = (Byte)(255 * Hue_2_RGB(var_1, var_2, Hue));
                Clr.B = (Byte)(255 * Hue_2_RGB(var_1, var_2, Hue - (1 / 3)));
            }



            return Clr;
        }
    }
公共类HSLColor
{
公共双色调;
公共双重饱和;
公共双光度;
公共HSL颜色(双H、双S、双L)
{
色调=H;
饱和=S;
光度=L;
}
来自RGB的公共静态HSLColor(颜色Clr)
{
从RGB返回(Clr.R、Clr.G、Clr.B);
}
来自RGB的公共静态HSLColor(字节R、字节G、字节B)
{
Double _R=(R/255d);
双G=(G/255d);
双_B=(B/255d);
Double _Min=Math.Min(Math.Min(_R,_G),_B);
Double _Max=Math.Max(Math.Max(_R,_G),_B);
双_Delta=_Max-_Min;
双H=0;
双S=0;
双L=(浮动)((最大+最小)/2.0f);
如果(_Delta!=0)
{
如果(L<0.5d)
{
S=(浮动)(_Delta/(_Max+_Min));
}
其他的
{
S=(浮动)(_Delta/(2.0f-_Max-_Min));
}
如果(\u R==\u Max)
{
H=(_G-_B)/_δ;
}
否则如果(_G==_Max)
{
H=2f+(_B-_R)/_δ;
}
否则如果(_B==_Max)
{
H=4f+(_R-_G)/_δ;
}
}
//换算成度
H=H*60d;
如果(H<0)H+=360;
//换算成百分比
S*=100d;
L*=100d;
返回新的HSL颜色(H、S、L);
}
专用双色调(双v1、双v2、双vH)
{
如果(vH<0)vH+=1;
如果(vH>1)vH-=1;
如果((6.0d*vH)<1)返回(v1+(v2-v1)*6*vH);
如果((2.0d*vH)<1)返回(v2);
如果((3.0d*vH)<2)返回(v1+(v2-v1)*((2.0d/3.0d)-vH)*6.0d);
返回(v1);
}
公共颜色ToRGB()
{
颜色Clr=新颜色();
双变量1,变量2;
如果(饱和度==0)
{
Clr.R=(字节)(亮度*255);
Clr.G=(字节)(亮度*255);
Clr.B=(字节)(亮度*255);
}
其他的
{
if(光度<0.5)var_2=光度*(1+饱和度);
else var_2=(光度+饱和度)-(饱和度*光度);
var_1=2*光度-var_2;
Clr.R=(字节)(255*Hue_2_RGB(var_1,var_2,Hue+(1/3));
Clr.G=(字节)(255*Hue_2_RGB(var_1,var_2,Hue));
Clr.B=(字节)(255*Hue_2_RGB(var_1,var_2,Hue-(1/3));
}
返回Clr;
}
}
但是它似乎不能正常工作

如果我使用的输入颜色为
(R0,G255,B193)
,例如: 我得到
Hue=0
在photoshop中,如果选择完全相同的RGB值:
Hue=165
这是正确的值

我希望色调的值介于0到360或0到240之间

有什么问题吗

参考:

这是整数除法,表示得到0或1。尝试:

(float)R / 255
以及所有其他情况

对于常量,请尝试:

( 1 / 3 ) -> ( 1.0 / 3.0 )

下面是Drupal+的开源混合,一些不同的程序员混合在一个函数中工作,拥有RGB>HSL和back。它工作完美无瑕

<?php
### RGB >> HSL
function _color_rgb2hsl($rgb) {
  $r = $rgb[0]; $g = $rgb[1]; $b = $rgb[2];
  $min = min($r, min($g, $b)); $max = max($r, max($g, $b));
  $delta = $max - $min; $l = ($min + $max) / 2; $s = 0;
  if ($l > 0 && $l < 1) {
    $s = $delta / ($l < 0.5 ? (2 * $l) : (2 - 2 * $l));
  }
  $h = 0;
  if ($delta > 0) {
    if ($max == $r && $max != $g) $h += ($g - $b) / $delta;
    if ($max == $g && $max != $b) $h += (2 + ($b - $r) / $delta);
    if ($max == $b && $max != $r) $h += (4 + ($r - $g) / $delta);
    $h /= 6;
  } return array($h, $s, $l);
}

### HSL >> RGB
function _color_hsl2rgb($hsl) {
  $h = $hsl[0]; $s = $hsl[1]; $l = $hsl[2];
  $m2 = ($l <= 0.5) ? $l * ($s + 1) : $l + $s - $l*$s;
  $m1 = $l * 2 - $m2;
  return array(_color_hue2rgb($m1, $m2, $h + 0.33333),
               _color_hue2rgb($m1, $m2, $h),
               _color_hue2rgb($m1, $m2, $h - 0.33333));
}

### Helper function for _color_hsl2rgb().
function _color_hue2rgb($m1, $m2, $h) {
  $h = ($h < 0) ? $h + 1 : (($h > 1) ? $h - 1 : $h);
  if ($h * 6 < 1) return $m1 + ($m2 - $m1) * $h * 6;
  if ($h * 2 < 1) return $m2;
  if ($h * 3 < 2) return $m1 + ($m2 - $m1) * (0.66666 - $h) * 6;
  return $m1;
}

### Convert a hex color into an RGB triplet.
function _color_unpack($hex, $normalize = false) {
  if (strlen($hex) == 4) {
    $hex = $hex[1] . $hex[1] . $hex[2] . $hex[2] . $hex[3] . $hex[3];
  } $c = hexdec($hex);
  for ($i = 16; $i >= 0; $i -= 8) {
    $out[] = (($c >> $i) & 0xFF) / ($normalize ? 255 : 1);
  } return $out;
}

### Convert an RGB triplet to a hex color.
function _color_pack($rgb, $normalize = false) {
  foreach ($rgb as $k => $v) {
    $out |= (($v * ($normalize ? 255 : 1)) << (16 - $k * 8));
  }return '#'. str_pad(dechex($out), 6, 0, STR_PAD_LEFT);
}

  print "Hex: ";

  $testhex = "#b7b700";
      print $testhex;

  $testhex2rgb = _color_unpack($testhex,true); 
      print "<br />RGB: ";

  var_dump($testhex2rgb);
      print "<br />HSL color module: ";

  $testrgb2hsl = _color_rgb2hsl($testhex2rgb); //Convert to HSL

  var_dump($testrgb2hsl);
      print "<br />RGB: ";

  $testhsl2rgb = _color_hsl2rgb($testrgb2hsl); // And back to RGB    
  var_dump($testhsl2rgb); 
      print "<br />Hex: ";

  $testrgb2hex = _color_pack($testhsl2rgb,true);
  var_dump($testrgb2hex);

?>


我在帖子中添加了一些更正,现在我得到的是Hue=03333,而不是Hue=165@Mervin用公式,在纸上计算。如果答案是预期的,那么一步一步地调试,并找出代码计算与纸面计算不同的那一行。@Mervin检查第一行。您正在对颜色进行规格化(适合范围0..1),因此,如果您希望它在0..255范围内,最终以255的倍数进行。的可能重复项
<?php
### RGB >> HSL
function _color_rgb2hsl($rgb) {
  $r = $rgb[0]; $g = $rgb[1]; $b = $rgb[2];
  $min = min($r, min($g, $b)); $max = max($r, max($g, $b));
  $delta = $max - $min; $l = ($min + $max) / 2; $s = 0;
  if ($l > 0 && $l < 1) {
    $s = $delta / ($l < 0.5 ? (2 * $l) : (2 - 2 * $l));
  }
  $h = 0;
  if ($delta > 0) {
    if ($max == $r && $max != $g) $h += ($g - $b) / $delta;
    if ($max == $g && $max != $b) $h += (2 + ($b - $r) / $delta);
    if ($max == $b && $max != $r) $h += (4 + ($r - $g) / $delta);
    $h /= 6;
  } return array($h, $s, $l);
}

### HSL >> RGB
function _color_hsl2rgb($hsl) {
  $h = $hsl[0]; $s = $hsl[1]; $l = $hsl[2];
  $m2 = ($l <= 0.5) ? $l * ($s + 1) : $l + $s - $l*$s;
  $m1 = $l * 2 - $m2;
  return array(_color_hue2rgb($m1, $m2, $h + 0.33333),
               _color_hue2rgb($m1, $m2, $h),
               _color_hue2rgb($m1, $m2, $h - 0.33333));
}

### Helper function for _color_hsl2rgb().
function _color_hue2rgb($m1, $m2, $h) {
  $h = ($h < 0) ? $h + 1 : (($h > 1) ? $h - 1 : $h);
  if ($h * 6 < 1) return $m1 + ($m2 - $m1) * $h * 6;
  if ($h * 2 < 1) return $m2;
  if ($h * 3 < 2) return $m1 + ($m2 - $m1) * (0.66666 - $h) * 6;
  return $m1;
}

### Convert a hex color into an RGB triplet.
function _color_unpack($hex, $normalize = false) {
  if (strlen($hex) == 4) {
    $hex = $hex[1] . $hex[1] . $hex[2] . $hex[2] . $hex[3] . $hex[3];
  } $c = hexdec($hex);
  for ($i = 16; $i >= 0; $i -= 8) {
    $out[] = (($c >> $i) & 0xFF) / ($normalize ? 255 : 1);
  } return $out;
}

### Convert an RGB triplet to a hex color.
function _color_pack($rgb, $normalize = false) {
  foreach ($rgb as $k => $v) {
    $out |= (($v * ($normalize ? 255 : 1)) << (16 - $k * 8));
  }return '#'. str_pad(dechex($out), 6, 0, STR_PAD_LEFT);
}

  print "Hex: ";

  $testhex = "#b7b700";
      print $testhex;

  $testhex2rgb = _color_unpack($testhex,true); 
      print "<br />RGB: ";

  var_dump($testhex2rgb);
      print "<br />HSL color module: ";

  $testrgb2hsl = _color_rgb2hsl($testhex2rgb); //Convert to HSL

  var_dump($testrgb2hsl);
      print "<br />RGB: ";

  $testhsl2rgb = _color_hsl2rgb($testrgb2hsl); // And back to RGB    
  var_dump($testhsl2rgb); 
      print "<br />Hex: ";

  $testrgb2hex = _color_pack($testhsl2rgb,true);
  var_dump($testrgb2hex);

?>