2 * Copyright 2001 Werner Freytag - please read to the LICENSE file
5 #ifndef CONVERT_RGB_HSV_H
6 #define CONVERT_RGB_HSV_H
12 #define RETURN_HSV(h, s, v) { H = h; S = s; V = v; return; }
13 #define RETURN_RGB(r, g, b) { R = r; G = g; B = b; return; }
19 RGB_to_HSV(float R
, float G
, float B
, float& H
, float& S
, float& V
)
21 // RGB are each on [0, 1]. S and max are returned on [0, 1] and H is
22 // returned on [0, 6]. Exception: H is returned UNDEFINED if S==0.
46 RETURN_HSV(UNDEFINED
, 0, max
);
48 float dist
= max
- min
;
52 : ((G
== min
) ? B
- R
: R
- G
);
55 : ((G
== min
) ? 5 : 1);
56 float h
= i
- f
/ dist
;
61 RETURN_HSV(h
, dist
/max
, max
);
66 HSV_to_RGB(float h
, float s
, float v
, float& R
, float& G
, float& B
)
68 // H is given on [0, 6] or UNDEFINED. S and V are given on [0, 1].
69 // RGB are each returned on [0, 1].
71 int i
= (int)floor(h
);
76 f
= 1 - f
; // if i is even
78 float m
= v
* (1 - s
);
80 float n
= v
* (1 - s
* f
);
108 #endif // CONVERT_RGB_HSV_H