Digital Signal Processing Reference
In-Depth Information
12.2 Farbr aume und
Farbkonversion
static float[] RGBtoHSV (int R, int G, int B, float[] HSV) {
1
// R, G, B ∈ [0 , 255]
2
float H = 0, S = 0, V = 0;
3
Programm 12.6
RGB-HSV Konvertierung (Java-
Methode) zur Umrechnung eines ein-
zelnen Farbtupels. Die Methode ent-
spricht bzgl. Parametern, Ruckgabe-
wert und Ergebnissen der Standard-
Java-Methode Color.RGBtoHSB() .
float cMax = 255.0f;
4
int cHi = Math.max(R,Math.max(G,B)); // highest color value
5
int cLo = Math.min(R,Math.min(G,B)); // lowest color value
6
// color range
int cRng = cHi - cLo;
7
8
9
// compute value V
V = cHi / cMax;
10
11
12
// compute saturation S
if (cHi > 0)
13
S = (float) cRng / cHi;
14
15
16
// compute hue H
if (cRng > 0) { // hue is defined only for color pixels
17
float rr = (float)(cHi - R) / cRng;
18
float gg = (float)(cHi - G) / cRng;
19
float bb = (float)(cHi - B) / cRng;
20
float hh;
21
// r is highest color value
if (R == cHi)
22
hh = bb - gg;
23
else if (G == cHi)
// g is highest color value
24
hh = rr - bb + 2.0f;
25
else
// b is highest color value
26
hh = gg - rr + 4.0f;
27
if (hh < 0)
28
hh= hh + 6;
29
H=hh/6;
30
}
31
32
33
if (HSV == null) // create a new HSV array if needed
HSV = new float[3];
34
HSV[0] = H; HSV[1] = S; HSV[2] = V;
35
return HSV;
36
}
37
Java-Implementierung
In Java ist die HSV
RGB-Konversion in der Klasse java.awt.Color
durch die Klassenmethode
int HSBtoRGB (float h , float s , float v )
implementiert, die aus den drei float -Werten h , s , v
[0 , 1] einen int -
Wert mit 3
8 Bit in dem in Java ublichen RGB-Format (siehe Abb.12.6)
erzeugt. Eine mogliche Implementierung dieser Methode ist in Prog. 12.7
gezeigt.
×
Search WWH ::




Custom Search