Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.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
Objective c RGB到HSL的转换似乎可以,但在视觉上不可以_Objective C_Ios_Rgb_Uicolor_Hsl - Fatal编程技术网

Objective c RGB到HSL的转换似乎可以,但在视觉上不可以

Objective c RGB到HSL的转换似乎可以,但在视觉上不可以,objective-c,ios,rgb,uicolor,hsl,Objective C,Ios,Rgb,Uicolor,Hsl,这里有个问题 假设我的RGB值为:R:53,G:37和B:11 因此,我设置了一个矩形的背景色: [UIColor color with red:0.53绿色:0.37蓝色:0.11 alpha:1.00] 现在我做一个RGB到HSL的转换,得到:H:0.10,S:0.16和L:0.13 因此,我将同一矩形的背景色设置为: [UIColor colorWithHue:0.10饱和度:0.16亮度:0.13 alpha:1.00] 问题是HSL颜色看起来不像RGB颜色。我将我的转换结果与在线转换器

这里有个问题

  • 假设我的RGB值为:R:53,G:37和B:11
  • 因此,我设置了一个矩形的背景色:

    [UIColor color with red:0.53绿色:0.37蓝色:0.11 alpha:1.00]

  • 现在我做一个RGB到HSL的转换,得到:H:0.10,S:0.16和L:0.13

  • 因此,我将同一矩形的背景色设置为:

    [UIColor colorWithHue:0.10饱和度:0.16亮度:0.13 alpha:1.00]

  • 问题是HSL颜色看起来不像RGB颜色。我将我的转换结果与在线转换器进行了比较,就我所知,它看起来还不错


    我很可能对某些事情的解释不正确

    UIColor函数应用HSB,这与HSL不同。

    正如Dominik指出的,您将HSB/HSV和HSL混为一谈

    用于将UIImage转换为HSB

    编辑

    现在,链接指向我根据该代码创建的要点。我在另一个函数中找到了它。

    这里是Drupals颜色函数的开源混合+一些不同的程序员混合在一个函数中工作,拥有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);
    }
    
    /* $testrgb = array(0.2,0.75,0.4); //RGB to start with
    print_r($testrgb); */
          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); //Converteren naar HSL
    
      var_dump($testrgb2hsl);
          print "<br />RGB: ";
    
      $testhsl2rgb = _color_hsl2rgb($testrgb2hsl); // En weer terug naar RGB    
      var_dump($testhsl2rgb); 
          print "<br />Hex: ";
    
      $testrgb2hex = _color_pack($testhsl2rgb,true);
      var_dump($testrgb2hex);
    
    ?>
    
    
    
    在设置背景色的位置显示代码。您可能还想发布一个屏幕截图,因为这是一个视觉缺陷。你可以直接上传图片到你的问题中。噢,该死!看来我把HSL和HSV/HSB搞混了。哦,天哪,转换成错误格式浪费了很多时间。非常感谢。谢谢我在看这个,但不知道如何从UIColor中获取HSB值。是的,我是一个大傻瓜:-)。所以我决定写我自己的转换方法。除了HSB-HSL人造密码外,哪一个还可以呢?有没有人对该代码有一个非死链接?这个地方看起来像是被人占用了。