malat
malat

Reputation: 12490

Converting RGB to HSB (aka HSV)

I am trying to implement a simple RGB to HSB routine, I've closely followed instruction from wikipedia reference article.

The code I have written is:

#include <iostream>
#include <cmath>

struct RGB { float red, green, blue; };
struct HSB { float hue, saturation, brightness; }; // aka HSV

// https://en.wikipedia.org/wiki/HSL_and_HSV#Converting_to_RGB
static void HSBToRGB(HSB const & hsb, RGB & rgb )
{
  /*
   * Given a color with hue H ∈ [0°, 360°), saturation SHSV ∈ [0, 1], and value
   * V ∈ [0, 1], we first find chroma:
   */
  const float H = hsb.hue;
  const float S_HSV = hsb.saturation;
  const float V = hsb.brightness;
  const float C = V * S_HSV;
  /*
  * Then we can find a point (R1, G1, B1) along the bottom three faces of the RGB
  * cube, with the same hue and chroma as our color (using the intermediate value X
  * for the second largest component of this color):
  */
  const float H_prime = H / 60.;
  const float X = C * ( 1 - std::abs( (int)H_prime % 2 - 1) );
  float R1, G1, B1;
  if( isnan( H ) )
    {
    R1 = G1 = B1 = 0;
    }
  else if( 0 <= H_prime && H_prime < 1 )
    {
    R1 = C; G1 = X; B1 = 0;
    }
  else if( 1 <= H_prime && H_prime < 2 )
    {
    R1 = X; G1 = C; B1 = 0;
    }
  else if( 2 <= H_prime && H_prime < 3 )
    {
    R1 = 0; G1 = C; B1 = X;
    }
  else if( 3 <= H_prime && H_prime < 4 )
    {
    R1 = 0; G1 = X; B1 = C;
    }
  else if( 4 <= H_prime && H_prime < 5 )
    {
    R1 = X; G1 = 0; B1 = C;
    }
  else if( 5 <= H_prime && H_prime < 6 )
    {
    R1 = C; G1 = 0; B1 = X;
    }

  /*
   * Finally, we can find R, G, and B by adding the same amount to each component,
   * to match value:
   */
  const float m = V - C;
  rgb.red   = R1 + m;
  rgb.green = G1 + m;
  rgb.blue  = B1 + m;
}

int main()
{
  HSB const hsb = { 251.1f, 0.887f, 0.918f };
  RGB rgb = { 0.255f , 0.104f , 0.918f };
  std::cout << "Reference: " << rgb.red << "," << rgb.green << "," << rgb.blue << std::endl;
  HSBToRGB(hsb, rgb);
  std::cout << "Computed:  " << rgb.red << "," << rgb.green << "," << rgb.blue << std::endl;

  return 0;
}

When compiled and run on linux debian/jessie amd64 here is what I get:

$ ./ref
Reference: 0.255,0.104,0.918
Computed:  0.103734,0.103734,0.918

Now if you lookup the correct reference values (same article, line 11 in the table) it should have been the same. What I do not understand is the large difference value for Hue (0.255 != 0.103734).

I've been double checking my implementation with what the wikipedia article describe and could not spot any difference.

Using java as reference, I could check that the RGB values should be correct, eg:

import java.awt.Color;

public class HSBToRGBExample {
  public static void main(String[] args) {
    float hue = 251.1f / 360;
    float saturation = 0.887f;
    float brightness = 0.918f;
    int rgb = Color.HSBtoRGB(hue, saturation, brightness);
    int red = (rgb >> 16) & 0xFF;
    int green = (rgb >> 8) & 0xFF;
    int blue = rgb & 0xFF;
    System.out.println((float)red / 255 + " " + (float)green / 255 + " " + (float)blue / 255);
  }
}

Is there a typo in the wikipedia article ? Or what is missing in my implementation ?

Upvotes: 0

Views: 336

Answers (1)

molbdnilo
molbdnilo

Reputation: 66371

You want the floating point modulus, but you're casting to an int (possibly to silence the compiler's complaints that % only works with integers).
In C++ it would be

std::abs(std::fmod(H_prime, 2) - 1);

(You will still see discrepancies, due to floating point imprecision.)

Upvotes: 1

Related Questions