2

a noob question here.

  1. let's say I have RGB values of: R:53,G:37 and B:11
  2. so i set a background color of a rectangle with:

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

  3. now I do a RGB to HSL conversion that yields: H:0.10,is S:0.16 and L:0.13

  4. so i set a background color of the same rectangle with:

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

the thing is that the HSL color looks nothing like the RGB color. I compared my conversion result against an online converter and it looks OK as far as I can tell.

I am most likely interpreting something incorrectly.

EarlGrey
  • 2,514
  • 4
  • 34
  • 63
  • Show your code where you set the background colour. You may also want to post a screenshot since this is a visual bug. You can inline the image into your question as a direct upload. – Shaggy Frog Dec 11 '10 at 00:59

3 Answers3

4

The UIColor function applies HSB which is different to HSL.

Dominik Seibold
  • 2,439
  • 1
  • 23
  • 29
  • oh drat! looks like i mixed up HSL vs HSV/HSB. oh man, hours wasted converting to the wrong format. thank you! – EarlGrey Dec 11 '10 at 01:07
2

As Dominik pointed out, you mixed up HSB/HSV and HSL.

Here you'll find as Category for UIImage for converting to HSB

edit
The link now directs to a gist I created from that code. I found it in another github project.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • thanks. I was looking at that, but couldn't figure out how to actually get the HSB values from a UIColor. yes I am a BIG noob :-). So I decided to write my own conversion method. Which went OK, except the HSB - HSL faux pas – EarlGrey Dec 11 '10 at 01:30
  • Does anyone have a non dead link for that code? the site almost looks like it's been squatted. – Diziet Apr 03 '12 at 10:03
0

Here is an open source mixture of Drupals color functions + some various programmers work mixed into one single function boasting RGB > HSL and back. It works flawlessly.

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

?>
Sam
  • 15,254
  • 25
  • 90
  • 145