* fix non-word in comment that seems to be the result of a search'n'replace
* style adjustments git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@38277 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -1,32 +1,33 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2001 Werner Freytag - please read to the LICENSE file
|
* Copyright 2001 Werner Freytag - please read to the LICENSE file
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef CONVERT_RGB_HSV_H
|
#ifndef CONVERT_RGB_HSV_H
|
||||||
#define CONVERT_RGB_HSV_H
|
#define CONVERT_RGB_HSV_H
|
||||||
|
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
|
||||||
#define RETURN_HSV(h, s, v) { H = h; S = s; V = v; return; }
|
#define RETURN_HSV(h, s, v) { H = h; S = s; V = v; return; }
|
||||||
#define RETURN_RGB(r, g, b) { R = r; G = g; B = b; return; }
|
#define RETURN_RGB(r, g, b) { R = r; G = g; B = b; return; }
|
||||||
|
|
||||||
#include <math.h>
|
|
||||||
|
|
||||||
#define UNDEFINED 0
|
#define UNDEFINED 0
|
||||||
|
|
||||||
|
|
||||||
inline void
|
inline void
|
||||||
RGB_to_HSV(float R, float G, float B, float& H, float& S, float& V)
|
RGB_to_HSV(float R, float G, float B, float& H, float& S, float& V)
|
||||||
{
|
{
|
||||||
// RGB are each on [0, 1]. S and max are returned on [0, 1] and H is
|
// RGB are each on [0, 1]. S and max are returned on [0, 1] and H is
|
||||||
// returned on [0, 6]. Eminception: H is returned UNDEFINED if S==0.
|
// returned on [0, 6]. Exception: H is returned UNDEFINED if S==0.
|
||||||
|
|
||||||
float min, max;
|
float min, max;
|
||||||
|
|
||||||
if (R > G) {
|
if (R > G) {
|
||||||
if (R > B) {
|
if (R > B) {
|
||||||
max = R;
|
max = R;
|
||||||
min = G > B ? B : G;
|
min = G > B ? B : G;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
max = B;
|
max = B;
|
||||||
min = G;
|
min = G;
|
||||||
}
|
}
|
||||||
@@ -35,60 +36,70 @@ RGB_to_HSV(float R, float G, float B, float& H, float& S, float& V)
|
|||||||
if (G > B) {
|
if (G > B) {
|
||||||
max = G;
|
max = G;
|
||||||
min = R > B ? B : R;
|
min = R > B ? B : R;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
max = B;
|
max = B;
|
||||||
min = R;
|
min = R;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (max == min) RETURN_HSV(UNDEFINED, 0, max);
|
if (max == min)
|
||||||
|
RETURN_HSV(UNDEFINED, 0, max);
|
||||||
|
|
||||||
float dist = max - min;
|
float dist = max - min;
|
||||||
|
|
||||||
float f = (R == min) ? G - B : ((G == min) ? B - R : R - G);
|
float f = (R == min)
|
||||||
float i = (R == min) ? 3 : ((G == min) ? 5 : 1);
|
? G - B
|
||||||
|
: ((G == min) ? B - R : R - G);
|
||||||
|
float i = (R == min)
|
||||||
|
? 3
|
||||||
|
: ((G == min) ? 5 : 1);
|
||||||
float h = i - f / dist;
|
float h = i - f / dist;
|
||||||
|
|
||||||
while (h >= 6.0) h -= 6.0;
|
while (h >= 6.0)
|
||||||
|
h -= 6.0;
|
||||||
RETURN_HSV(h, dist/max, max);
|
|
||||||
|
|
||||||
|
RETURN_HSV(h, dist/max, max);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline void
|
inline void
|
||||||
HSV_to_RGB(float h, float s, float v, float& R, float& G, float& B)
|
HSV_to_RGB(float h, float s, float v, float& R, float& G, float& B)
|
||||||
{
|
{
|
||||||
// H is given on [0, 6] or UNDEFINED. S and V are given on [0, 1].
|
// H is given on [0, 6] or UNDEFINED. S and V are given on [0, 1].
|
||||||
// RGB are each returned on [0, 1].
|
// RGB are each returned on [0, 1].
|
||||||
|
|
||||||
int i = (int)floor(h);
|
int i = (int)floor(h);
|
||||||
|
|
||||||
float f = h - i;
|
float f = h - i;
|
||||||
|
|
||||||
if ( !(i & 1) ) f = 1 - f; // if i is even
|
if ( !(i & 1) )
|
||||||
|
f = 1 - f; // if i is even
|
||||||
|
|
||||||
float m = v * (1 - s);
|
float m = v * (1 - s);
|
||||||
|
|
||||||
float n = v * (1 - s * f);
|
float n = v * (1 - s * f);
|
||||||
|
|
||||||
switch (i) {
|
switch (i) {
|
||||||
|
|
||||||
case 6:
|
case 6:
|
||||||
|
case 0:
|
||||||
case 0: RETURN_RGB(v, n, m);
|
RETURN_RGB(v, n, m);
|
||||||
|
|
||||||
case 1: RETURN_RGB(n, v, m);
|
case 1:
|
||||||
|
RETURN_RGB(n, v, m);
|
||||||
case 2: RETURN_RGB(m, v, n)
|
|
||||||
|
case 2:
|
||||||
case 3: RETURN_RGB(m, n, v);
|
RETURN_RGB(m, v, n)
|
||||||
|
|
||||||
case 4: RETURN_RGB(n, m, v);
|
case 3:
|
||||||
|
RETURN_RGB(m, n, v);
|
||||||
case 5: RETURN_RGB(v, m, n);
|
|
||||||
|
case 4:
|
||||||
|
RETURN_RGB(n, m, v);
|
||||||
|
|
||||||
|
case 5:
|
||||||
|
RETURN_RGB(v, m, n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif // CONVERT_RGB_HSV_H
|
#endif // CONVERT_RGB_HSV_H
|
||||||
|
|||||||
Reference in New Issue
Block a user