66import java .awt .geom .AffineTransform ;
77import java .util .ArrayList ;
88
9+ import processing .mode .experimental .TextAreaPainter ;
10+
911public class ColorControlBox {
10-
12+
13+ public boolean visible ;
14+
1115 ArrayList <Handle > handles ;
1216 ColorMode colorMode ;
1317 Color color ;
1418 boolean ilegalColor = false ;
1519 boolean isBW ;
1620 boolean isHex ;
17-
21+
1822 String drawContext ;
19-
23+
2024 // interface
2125 int x , y , width , height ;
22- TweakTextAreaPainter painter ;
23- boolean visible ;
24-
26+ TextAreaPainter painter ;
27+
2528 public ColorControlBox (String context , ColorMode mode , ArrayList <Handle > handles )
2629 {
2730 this .drawContext = context ;
2831 this .colorMode = mode ;
2932 this .handles = handles ;
30-
33+
3134 // add this box to the handles so they can update this color on change
3235 for (Handle h : handles ) {
3336 h .setColorBox (this );
3437 }
35-
38+
3639 isBW = isGrayScale ();
3740 isHex = isHexColor ();
3841 color = getCurrentColor ();
39-
42+
4043 visible = Settings .alwaysShowColorBoxes ;
4144 }
42-
43- public void initInterface (TweakTextAreaPainter painter , int x , int y , int w , int h )
45+
46+ public void initInterface (TextAreaPainter textAreaPainter , int x , int y , int w , int h )
4447 {
45- this .painter = painter ;
48+ this .painter = textAreaPainter ;
4649 this .x = x ;
4750 this .y = y ;
4851 this .width = w ;
4952 this .height = h ;
5053 }
51-
54+
5255 public void setPos (int x , int y )
5356 {
5457 this .x = x ;
5558 this .y = y ;
5659 }
57-
60+
5861 public void draw (Graphics2D g2d )
5962 {
6063 if (!visible ) {
6164 return ;
6265 }
63-
66+
6467 AffineTransform trans = g2d .getTransform ();
6568 g2d .translate (x , y );
6669
@@ -72,16 +75,16 @@ public void draw(Graphics2D g2d)
7275 g2d .setStroke (new BasicStroke (1 ));
7376 g2d .setColor (Color .BLACK );
7477 g2d .drawRoundRect (0 , 0 , width , height , 5 , 5 );
75-
78+
7679 if (ilegalColor ) {
7780 g2d .setColor (Color .RED );
7881 g2d .setStroke (new BasicStroke (2 ));
7982 g2d .drawLine (width -3 , 3 , 3 , height -3 );
8083 }
81-
84+
8285 g2d .setTransform (trans );
8386 }
84-
87+
8588 public boolean isGrayScale ()
8689 {
8790 if (handles .size () <= 2 ) {
@@ -90,10 +93,10 @@ public boolean isGrayScale()
9093 return true ;
9194 }
9295 }
93-
96+
9497 return false ;
9598 }
96-
99+
97100 /**
98101 * Check if color is hex or webcolor
99102 * @return
@@ -107,10 +110,10 @@ private boolean isHexColor()
107110 return true ;
108111 }
109112 }
110-
113+
111114 return false ;
112115 }
113-
116+
114117 public Color getCurrentColor ()
115118 {
116119 try {
@@ -119,7 +122,7 @@ public Color getCurrentColor()
119122 if (isBW ) {
120123 // treat as color(gray)
121124 float gray = handles .get (0 ).newValue .floatValue ();
122- return verifiedGrayColor (gray );
125+ return verifiedGrayColor (gray );
123126 }
124127 else {
125128 // treat as color(argb)
@@ -132,13 +135,13 @@ else if (handles.size() == 2)
132135 if (isBW ) {
133136 // color(gray, alpha)
134137 float gray = handles .get (0 ).newValue .floatValue ();
135- return verifiedGrayColor (gray );
138+ return verifiedGrayColor (gray );
136139 }
137140 else {
138141 // treat as color(argb, a)
139142 int argb = handles .get (0 ).newValue .intValue ();
140143 float a = handles .get (1 ).newValue .floatValue ();
141- return verifiedHexColor (argb , a );
144+ return verifiedHexColor (argb , a );
142145 }
143146 }
144147 else if (handles .size () == 3 )
@@ -173,36 +176,36 @@ else if (handles.size() == 4)
173176 }
174177 catch (Exception e ) {
175178 System .out .println ("error parsing color value: " + e .toString ());
176- ilegalColor = true ;
179+ ilegalColor = true ;
177180 return Color .WHITE ;
178181 }
179-
182+
180183 // couldn't figure out this color, return WHITE color
181184 ilegalColor = true ;
182185 return Color .WHITE ;
183186 }
184-
187+
185188 private Color verifiedGrayColor (float gray )
186189 {
187190 if (gray < 0 || gray > colorMode .v1Max ) {
188191 return colorError ();
189192 }
190-
193+
191194 ilegalColor = false ;
192195 gray = gray /colorMode .v1Max * 255 ;
193196 return new Color ((int )gray , (int )gray , (int )gray , 255 );
194197 }
195-
198+
196199 private Color verifiedHexColor (int argb )
197200 {
198201 int r = (argb >>16 )&0xff ;
199202 int g = (argb >>8 )&0xff ;
200203 int b = (argb &0xff );
201-
204+
202205 ilegalColor = false ;
203- return new Color (r , g , b , 255 );
206+ return new Color (r , g , b , 255 );
204207 }
205-
208+
206209 private Color verifiedHexColor (int argb , float alpha )
207210 {
208211 int r = (argb >>16 )&0xff ;
@@ -212,10 +215,10 @@ private Color verifiedHexColor(int argb, float alpha)
212215 ilegalColor = false ;
213216 return new Color (r , g , b , 255 );
214217 }
215-
218+
216219 public Color verifiedRGBColor (float r , float g , float b , float a )
217220 {
218- if (r < 0 || r > colorMode .v1Max ||
221+ if (r < 0 || r > colorMode .v1Max ||
219222 g < 0 || g > colorMode .v2Max ||
220223 b < 0 || b > colorMode .v3Max ) {
221224 return colorError ();
@@ -230,7 +233,7 @@ public Color verifiedRGBColor(float r, float g, float b, float a)
230233
231234 public Color verifiedHSBColor (float h , float s , float b , float a )
232235 {
233- if (h < 0 || h > colorMode .v1Max ||
236+ if (h < 0 || h > colorMode .v1Max ||
234237 s < 0 || s > colorMode .v2Max ||
235238 b < 0 || b > colorMode .v3Max ) {
236239 return colorError ();
@@ -240,7 +243,7 @@ public Color verifiedHSBColor(float h, float s, float b, float a)
240243 Color c = Color .getHSBColor (h /colorMode .v1Max , s /colorMode .v2Max , b /colorMode .v3Max );
241244 return new Color (c .getRed (), c .getGreen (), c .getBlue (), 255 );
242245 }
243-
246+
244247 private Color colorError ()
245248 {
246249 ilegalColor = true ;
@@ -251,47 +254,47 @@ public void colorChanged()
251254 {
252255 color = getCurrentColor ();
253256 }
254-
257+
255258 public int getTabIndex ()
256259 {
257260 return handles .get (0 ).tabIndex ;
258261 }
259-
262+
260263 public int getLine ()
261264 {
262265 return handles .get (0 ).line ;
263266 }
264-
267+
265268 public int getCharIndex ()
266269 {
267270 int lastHandle = handles .size ()-1 ;
268271 return handles .get (lastHandle ).newEndChar + 2 ;
269272 }
270273
271274 /* Check if the point is in the box
272- *
275+ *
273276 */
274277 public boolean pick (int mx , int my )
275278 {
276279 if (!visible ) {
277280 return false ;
278281 }
279-
282+
280283 if (mx >x && mx < x +width && my >y && my <y +height ) {
281284 return true ;
282285 }
283-
286+
284287 return false ;
285288 }
286-
289+
287290 /* Only show the color box if mouse is on the same line
288- *
291+ *
289292 * return true if there was change
290293 */
291294 public boolean setMouseY (int my )
292295 {
293296 boolean change = false ;
294-
297+
295298 if (my >y && my <y +height ) {
296299 if (!visible ) {
297300 change = true ;
@@ -304,13 +307,13 @@ public boolean setMouseY(int my)
304307 }
305308 visible = false ;
306309 }
307-
310+
308311 return change ;
309312 }
310-
311- /* Update the color numbers with the new values that were selected
313+
314+ /* Update the color numbers with the new values that were selected
312315 * in the color selector
313- *
316+ *
314317 * hue, saturation and brightness parameters are always 0-255
315318 */
316319 public void selectorChanged (int hue , int saturation , int brightness )
@@ -343,20 +346,20 @@ else if (handles.size() == 3 || handles.size() == 4) {
343346 // RGB
344347 Color c = Color .getHSBColor ((float )hue /255 , (float )saturation /255 , (float )brightness /255 );
345348 handles .get (0 ).setValue ((float )c .getRed ()/255 *colorMode .v1Max );
346- handles .get (1 ).setValue ((float )c .getGreen ()/255 *colorMode .v2Max );
349+ handles .get (1 ).setValue ((float )c .getGreen ()/255 *colorMode .v2Max );
347350 handles .get (2 ).setValue ((float )c .getBlue ()/255 *colorMode .v3Max );
348351 }
349352 }
350353 }
351-
354+
352355 // update our own color
353356 color = getCurrentColor ();
354-
357+
355358 // update code text painter so the user will see the changes
356359 painter .updateCodeText ();
357360 painter .repaint ();
358361 }
359-
362+
360363 public String toString ()
361364 {
362365 return handles .size () + " handles, color mode: " + colorMode .toString ();
0 commit comments