/Users/lyon/j4p/src/ip/color/Hsb.java

1    package ip.color; 
2     
3    import ip.gui.frames.ColorFrame; 
4     
5     
6    public class Hsb extends FloatPlane { 
7     
8        public Hsb(ColorFrame _cf) { 
9            super(_cf); 
10       } 
11    
12       private float hsbArray[] = new float[3]; 
13    
14       public void fromRgb() { 
15           for (int x = 0; x < width; x++) 
16               for (int y = 0; y < height; y++) { 
17                   rgbtohsb(r[x][y], g[x][y], b[x][y], hsbArray); 
18                   r[x][y] = hsbArray[0]; 
19                   g[x][y] = hsbArray[1]; 
20                   b[x][y] = hsbArray[2]; 
21               } 
22    
23       } 
24    
25       public void toRgb() { 
26           for (int x = 0; x < width; x++) 
27               for (int y = 0; y < height; y++) { 
28                   hsbtorgb(r[x][y], g[x][y], b[x][y], hsbArray); 
29                   r[x][y] = hsbArray[0]; 
30                   g[x][y] = hsbArray[1]; 
31                   b[x][y] = hsbArray[2]; 
32               } 
33    
34       } 
35    
36       public void hsbtorgb(float hue, float saturation, float brightness, float triplet[]) { 
37           int r = 0, g = 0, b = 0; 
38           if (saturation == 0) { 
39               r = g = b = (int) (brightness * 255); 
40           } else { 
41               double h = (hue - Math.floor(hue)) * 6.0; 
42               double f = h - Math.floor(h); 
43               double p = brightness * (1.0 - saturation); 
44               double q = brightness * (1.0 - saturation * f); 
45               double t = brightness * (1.0 - (saturation * (1.0 - f))); 
46               switch ((int) h) { 
47                   case 0: 
48                       r = (int) (brightness * 255); 
49                       g = (int) (t * 255); 
50                       b = (int) (p * 255); 
51                       break; 
52                   case 1: 
53                       r = (int) (q * 255); 
54                       g = (int) (brightness * 255); 
55                       b = (int) (p * 255); 
56                       break; 
57                   case 2: 
58                       r = (int) (p * 255); 
59                       g = (int) (brightness * 255); 
60                       b = (int) (t * 255); 
61                       break; 
62                   case 3: 
63                       r = (int) (p * 255); 
64                       g = (int) (q * 255); 
65                       b = (int) (brightness * 255); 
66                       break; 
67                   case 4: 
68                       r = (int) (t * 255); 
69                       g = (int) (p * 255); 
70                       b = (int) (brightness * 255); 
71                       break; 
72                   case 5: 
73                       r = (int) (brightness * 255); 
74                       g = (int) (p * 255); 
75                       b = (int) (q * 255); 
76                       break; 
77               } 
78           } 
79           triplet[0] = r; 
80           triplet[1] = g; 
81           triplet[2] = b; 
82       } 
83    
84       public void rgbtohsb(float r, float g, float b, float[] hsbvals) { 
85           float hue, saturation, brightness; 
86           float cmax = (r > g) ? r : g; 
87           if (b > cmax) cmax = b; 
88           float cmin = (r < g) ? r : g; 
89           if (b < cmin) cmin = b; 
90    
91           brightness = ((float) cmax) / 255.0f; 
92           if (cmax != 0) 
93               saturation = ((float) (cmax - cmin)) / ((float) cmax); 
94           else 
95               saturation = 0; 
96           if (saturation == 0) 
97               hue = 0; 
98           else { 
99               float redc = ((float) (cmax - r)) / ((float) (cmax - cmin)); 
100              float greenc = ((float) (cmax - g)) / ((float) (cmax - cmin)); 
101              float bluec = ((float) (cmax - b)) / ((float) (cmax - cmin)); 
102              if (r == cmax) 
103                  hue = bluec - greenc; 
104              else if (g == cmax) 
105                  hue = 2.0f + redc - bluec; 
106              else 
107                  hue = 4.0f + greenc - redc; 
108              hue = hue / 6.0f; 
109              if (hue < 0) 
110                  hue = hue + 1.0f; 
111          } 
112          hsbvals[0] = hue; 
113          hsbvals[1] = saturation; 
114          hsbvals[2] = brightness; 
115      } 
116   
117   
118  } 
119   
120