【问题标题】:RGB to HSL conversion seems OK value-vise, but not visuallyRGB 到 HSL 的转换看起来不错,但视觉上不行
【发布时间】:2010-12-11 00:48:23
【问题描述】:

这里是一个菜鸟问题。

  1. 假设我的 RGB 值为:R:53,G:37 和 B:11
  2. 所以我设置了一个矩形的背景颜色:

    [UIColor colorWithRed:0.53 green:0.37 blue:0.11 alpha:1.00];

  3. 现在我进行 RGB 到 HSL 的转换,得到:H:0.10,is S:0.16 and L:0.13

  4. 所以我设置了相同矩形的背景颜色:

    [UIColor colorWithHue:0.10 saturation:0.16 brightness:0.13 alpha:1.00];

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

我很可能会错误地解释某些内容。

【问题讨论】:

  • 在您设置背景颜色的位置显示您的代码。您可能还想发布屏幕截图,因为这是一个视觉错误。您可以将图片作为直接上传的方式内嵌到您的问题中。

标签: objective-c ios rgb uicolor hsl


【解决方案1】:

UIColor 函数应用了不同于 HSL 的 HSB。

【讨论】:

  • 天哪!看起来我混淆了 HSL 与 HSV/HSB。哦,伙计,浪费了数小时转换为错误的格式。谢谢!
【解决方案2】:

正如 Dominik 指出的那样,您混淆了 HSB/HSV 和 HSL。

Here you'll find as Category 用于 UIImage 转换为 HSB

编辑
该链接现在指向我从该代码创建的要点。我在另一个github project找到它。

【讨论】:

  • 谢谢。我正在查看,但无法弄清楚如何从 UIColor 中实际获取 HSB 值。是的,我是一个大菜鸟:-)。所以我决定编写自己的转换方法。一切正常,除了 HSB - HSL 失礼
  • 是否有人有该代码的非死链接?该网站几乎看起来像是被占用了。
【解决方案3】:

这里是 Drupals 颜色函数 + 一些不同程序员工作的开源组合,混合成一个拥有 RGB > HSL 和返回的单一函数。它完美无缺。

<?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);

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-15
    • 2011-03-29
    • 2011-01-22
    • 2012-07-28
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    相关资源
    最近更新 更多