/Users/lyon/j4p/src/ip/color/Hls.java
|
1 /**
2 * Victor Silva - University of Bridgeport 20 Feb. 1997
3 * Doug Lyon - revised heavily 8/3/98.
4 *
5 * Use this class by calling HLS.fromRGB to convert an
6 * int array containing image pixels into HLS color space.
7 *
8 * HLS and RGB Converter class. This class allows conversion
9 * between RGB and HLS (Hue, Lightness and Saturation).
10 * The conversion algorithm is from Foley and van Dam
11 * "Computer Graphics".
12 // there is a bug here someplace....oops...I just have
13 // no time to fix it now....8/14/98 DL.
14 *
15 */
16 package ip.color;
17
18 import ip.gui.frames.ColorFrame;
19
20
21 public class Hls extends FloatPlane {
22
23 public Hls(ColorFrame _cf) {
24 super(_cf);
25 }
26
27
28 public void fromRgb() {
29 printStatistics();
30 normalize();
31 printStatistics();
32 float max = 0f;
33 float min = 255f;
34 float h = 0;
35 float l = 0;
36 float s = 0;
37 float delta = 0;
38
39 for (int x = 0; x < width; x++)
40 for (int y = 0; y < height; y++) {
41 max = max(r[x][y], g[x][y], b[x][y]);
42 min = min(r[x][y], g[x][y], b[x][y]);
43 l = (max + min) / 2;
44 if (max == min) {
45 s = 0; // Achromatic case
46 h = 0; // h is really undefined
47 r[x][y] = h;
48 g[x][y] = l;
49 b[x][y] = s;
50 continue;
51 }
52 delta = max - min;
53
54 // Calculate saturation
55 if (l <= 0.5)
56 s = delta / (max + min);
57 else
58 s = delta / (2 - delta);
59
60 if (r[x][y] == max)
61 h = (g[x][y] - b[x][y]) / delta;
62 else if (g[x][y] == max)
63 h = (float) (2.0 + (b[x][y] - r[x][y]) / delta);
64 else if (b[x][y] == max)
65 h = (float) (4.0 + (r[x][y] - g[x][y]) / delta);
66 h *= 60;
67 if (h < 0) h += 360; // no negative degrees
68 r[x][y] = h;
69 g[x][y] = l;
70 b[x][y] = s;
71 }
72
73 }
74
75
76
77 // assume that luminance is stored in HLS_g
78 public void autoScale() {
79 float max = getMax(g);
80 float min = getMin(g);
81 System.out.println("Max luminance = " + max);
82 System.out.println("Min luminance = " + min);
83 float sf = (float) (255.0 / (max - min));
84 System.out.println("Scaling in luminance by" + sf);
85 for (int x = 0; x < width; x++)
86 for (int y = 0; y < height; y++)
87 g[x][y] *= sf;
88 }
89
90
91 public void toRgb() {
92 int totalPix = r.length;
93
94 double m1, m2;
95 float h,l,s;
96 double R, G, B;
97
98 for (int x = 0; x < width; x++)
99 for (int y = 0; y < height; y++) {
100 h = r[x][y];
101 l = g[x][y];
102 s = b[x][y];
103 if (s == 0.0) {
104 r[x][y] = l;
105 g[x][y] = l;
106 b[x][y] = l;
107 continue;
108 }
109 m2 = (l <= 0.5) ? (l * (l + s)) : (l + s - (l * s));
110 m1 = 2.0 * l - m2;
111 r[x][y] = value(m1, m2, h + 120.0);
112 g[x][y] = value(m1, m2, h);
113 b[x][y] = value(m1, m2, h - 120.0);
114 }
115 scale(255);
116 }
117
118 float value(double n1, double n2, double hue) {
119 if (hue > 360.0)
120 hue -= 360.0;
121 else if (hue < 0.0) hue += 360.0;
122
123 if (hue < 60.0)
124 return ((float) (n1 + ((n2 - n1) * hue) / 60.0));
125
126 else {
127 if (hue < 180.0)
128 return ((float) n2);
129 else if (hue < 240.0)
130 return ((float) (n1 + ((n2 - n1) * (240.0 - hue)) / 60.0));
131 else
132 return ((float) n1);
133 }
134 }
135
136 }
137
138