Commit da8be228 authored by Sebastien Barre's avatar Sebastien Barre
Browse files

FIX: fix HSV/RGB conversion. RGB to HSV is new/simpler, HSV to RGB is fixed (was HSI to RGB)

parent 403424d0
......@@ -18,7 +18,7 @@
#include "vtkMath.h"
#include "vtkObjectFactory.h"
vtkCxxRevisionMacro(vtkMath, "1.77");
vtkCxxRevisionMacro(vtkMath, "1.78");
vtkStandardNewMacro(vtkMath);
long vtkMath::Seed = 1177; // One authors home address
......@@ -2065,142 +2065,117 @@ void vtkMath::SingularValueDecomposition3x3(const double A[3][3],
//----------------------------------------------------------------------------
void vtkMath::RGBToHSV(float r, float g, float b, float *h, float *s, float *v)
{
float hue = 0;
float sat = 0;
float val = 0;
float lx, ly, lz;
float onethird = 1.0 / 3.0;
float onesixth = 1.0 / 6.0;
float twothird = 2.0 / 3.0;
if (r <= 0.0 && g <= 0.0 && b <= 0.0)
float cmax, cmin;
cmax = r;
cmin = r;
if (g > cmax)
{
cmax = g;
}
else if (g < cmin)
{
cmin = g;
}
if (b > cmax)
{
cmax = b;
}
else if (b < cmin)
{
cmin = b;
}
*v = cmax;
if (*v > 0.0)
{
*s = (cmax - cmin) / cmax;
}
else
{
*h = 0.0;
*s = 0.0;
*v = 0.0;
return;
}
if (r == g && g == b)
{
hue = sat = 0.0;
val = r;
}
else if (r >= g && g >= b)
{ // case 0
val = r;
lz = g;
lx = b;
sat = 1.0 - (lx/val);
hue = (0.0 + (1.0 - ((1.0 - (lz/val))/sat)))/6.0;
}
else if (g >= r && r >= b)
{ // case 1
ly = r;
val = g;
lx = b;
sat = 1.0 - (lx/val);
hue = (1.0 + ((1.0 - (ly/val))/sat))/6.0;
}
else if (g >= b && b >= r)
{ // case 2
lx = r;
val = g;
lz = b;
sat = 1.0 - (lx/val);
hue = (2.0 + (1.0 - ((1.0 - (lz/val))/sat)))/6.0;
}
else if (b >= g && g >= r)
{ // case 3
lx = r;
ly = g;
val = b;
sat = 1.0 - (lx/val);
hue = (3.0 + ((1.0 - (ly/val))/sat))/6.0;
}
else if (b >= r && r >= g)
{ // case 4
lz = r;
lx = g;
val = b;
sat = 1.0 - (lx/val);
hue = (4.0 + (1.0 - ((1.0 - (lz/val))/sat)))/6.0;
}
else if (r >= b && b >= g)
{ // case 5
val = r;
lx = g;
ly = b;
sat = 1.0 - (lx/val);
hue = (5.0 + ((1.0 - (ly/val))/sat))/6.0;
}
*h = hue;
*s = sat;
*v = val;
if (*s > 0)
{
if (r == cmax)
{
*h = onesixth * (g - b) / (cmax - cmin);
}
else if (g == cmax)
{
*h = onethird + onesixth * (b - r) / (cmax - cmin);
}
else
{
*h = twothird + onesixth * (r - g) / (cmax - cmin);
}
if (*h < 0.0)
{
*h += 1.0;
}
}
else
{
*h = 0.0;
}
}
//----------------------------------------------------------------------------
void vtkMath::HSVToRGB(float h, float s, float v, float *r, float *g, float *b)
{
float R, G, B;
float max = 1.0;
float third = max / 3.0;
float temp;
float onethird = 1.0 / 3.0;
float onesixth = 1.0 / 6.0;
float twothird = 2.0 / 3.0;
float fivesixth = 5.0 / 6.0;
// compute rgb assuming S = 1.0;
if (h >= 0.0 && h <= third) // red -> green
// compute RGB from HSV
if (h > onesixth && h <= onethird) // green/red
{
G = h/third;
R = 1.0 - G;
B = 0.0;
*g = 1.0;
*r = (onethird - h) / onesixth;
*b = 0.0;
}
else if (h >= third && h <= 2.0*third) // green -> blue
else if (h > onethird && h <= 0.5) // green/blue
{
B = (h - third)/third;
G = 1.0 - B;
R = 0.0;
*g = 1.0;
*b = (h - onethird) / onesixth;
*r = 0.0;
}
else // blue -> red
else if (h > 0.5 && h <= twothird) // blue/green
{
R = (h - 2.0 * third)/third;
B = 1.0 - R;
G = 0.0;
*b = 1.0;
*g = (twothird - h) / onesixth;
*r = 0.0;
}
// add Saturation to the equation.
s = s / max;
//R = S + (1.0 - S)*R;
//G = S + (1.0 - S)*G;
//B = S + (1.0 - S)*B;
// what happend to this?
R = s*R + (1.0 - s);
G = s*G + (1.0 - s);
B = s*B + (1.0 - s);
// Use value to get actual RGB
// normalize RGB first then apply value
temp = R + G + B;
//V = 3 * V / (temp * max);
// and what happend to this?
v = 3 * v / (temp);
R = R * v;
G = G * v;
B = B * v;
// clip below 255
//if (R > 255.0) R = max;
//if (G > 255.0) G = max;
//if (B > 255.0) B = max;
// mixed constant 255 and max ?????
if (R > max)
else if (h > twothird && h <= fivesixth) // blue/red
{
R = max;
*b = 1.0;
*r = (h - twothird) / onesixth;
*g = 0.0;
}
if (G > max)
else if (h > fivesixth && h <= 1.0) // red/blue
{
G = max;
*r = 1.0;
*b = (1.0 - h) / onesixth;
*g = 0.0;
}
if (B > max)
else // red/green
{
B = max;
*r = 1.0;
*g = h / onesixth;
*b = 0.0;
}
*r = R;
*g = G;
*b = B;
// add Saturation to the equation.
*r = (s * *r + (1.0 - s));
*g = (s * *g + (1.0 - s));
*b = (s * *b + (1.0 - s));
*r *= v;
*g *= v;
*b *= v;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment