/Users/lyon/j4p/src/bookExamples/ch26Graphics/carl/logPolar/ColorArray.java

1    package bookExamples.ch26Graphics.carl.logPolar; 
2     
3    /*  class:              graphics.carl.ColorArray 
4     *  principal output:   colorArray[ringCount][rayCount] 
5     * 
6     *  Generates 2-D rectangular array of colors. 
7     *  Array index order is [ring][ray] = [column][row] =[x][y] 
8     *      (Cartesian order, transpose of row-column matrix order). 
9     *  Default constructor uniformly samples hue spectrum 
10    *      at max saturation & brightness, by ray 
11    *      ( = row = second index = y-coord). 
12    * 
13    *  Methods for other arrangements and shuffling of colors 
14    *  Methods for getters 
15    * 
16    *  ---------------------------------- 
17    *  Modified Oct 16 2001, Carl Weiman 
18    */ 
19    
20   //package mosaics;  //To be packaged after development 
21    
22    
23   public class ColorArray { 
24       private int ringCount, rayCount; 
25       private java.awt.Color[][] colorArray;  // Principal product of this class 
26       private java.awt.Color[] tempColors;        // Internal buffer to generate and shuffle colors 
27       private boolean radial = true;  // Determines radial vs concentric color pattern 
28    
29       //------------ 
30       // Constructor 
31       //------------ 
32    
33       public ColorArray(int _rings, int _rays, boolean _radial) { 
34           ringCount = _rings; 
35           rayCount = _rays; 
36           radial = _radial; 
37    
38           colorArray = new java.awt.Color[ringCount][rayCount]; 
39           if (radial) 
40               tempColors = new java.awt.Color[rayCount]; 
41           else 
42               tempColors = new java.awt.Color[ringCount]; 
43           rainbow();      // Populate tempColor array with rainbow spectrum 
44           assignColors(); // Distribute tempColors to colorArray 
45       } 
46    
47       //---------------------------------------------------- 
48       public void assignColors() { 
49           if (radial) { 
50               for (int i_ray = 0; i_ray < rayCount; i_ray++) { 
51                   colorArray[0][i_ray] = tempColors[i_ray]; 
52                   for (int i_ring = 1; i_ring < ringCount; i_ring++) { 
53                       colorArray[i_ring][i_ray] = colorArray[0][i_ray]; 
54                   } 
55               } 
56           } else { 
57               for (int i_ring = 0; i_ring < ringCount; i_ring++) { 
58                   colorArray[i_ring][0] = tempColors[i_ring]; 
59                   for (int i_ray = 1; i_ray < rayCount; i_ray++) { 
60                       colorArray[i_ring][i_ray] = colorArray[i_ring][0]; 
61                   } 
62               } 
63           } 
64           System.out.println("1st color:" + tempColors[0]); 
65           System.out.println("last color:" + tempColors[tempColors.length - 1]); 
66       } 
67    
68       //--------- 
69       //  Getters 
70       //--------- 
71    
72       public java.awt.Color[][] get_colorArray() { 
73           return colorArray; 
74       } 
75    
76       public java.awt.Color getColori(int _ring, int _ray) { 
77           return colorArray[_ring % ringCount][_ray % rayCount]; 
78       } 
79    
80       public boolean get_radial() { 
81           return radial; 
82       } 
83    
84       public int get_rayCount() { 
85           return rayCount; 
86       } 
87    
88       public int get_ringCount() { 
89           return ringCount; 
90       } 
91    
92       public String toString() {      // Show your colors 
93           String str; 
94           str = new String("  "); 
95           for (int i_ray = 0; i_ray < rayCount; i_ray++) { 
96               str += "\n"; 
97               for (int i_ring = 0; i_ring < ringCount; i_ring++) { 
98                   str += "  " + colorArray[i_ring][i_ray].toString(); 
99               } 
100          } 
101          return str; 
102      } 
103   
104      //-------- 
105   
106   
107      public void shuffleRays(int m) { 
108          String debug; 
109          int k; 
110          java.awt.Color holdColor1, holdColor2; 
111          for (int i_ray = 0; i_ray < rayCount; i_ray += m) { 
112              holdColor1 = colorArray[0][i_ray]; 
113              k = m * (((int) (Math.random() * (float) rayCount)) / m); 
114              holdColor2 = colorArray[0][k]; 
115   
116              for (int j = 0; j < m; j++) { 
117                  //          debug = ""; 
118                  //          debug += " m= "+m+" i_ray = "+i_ray+" k= "+k+" j= "+j; 
119                  //          debug += "  holdColor1=" + holdColor1 ; 
120                  //          System.out.println( debug ); 
121                  if ((i_ray + j) < rayCount) 
122                      for (int i_ring = 0; i_ring < ringCount; i_ring++) 
123                          colorArray[i_ring][i_ray + j] = holdColor2; 
124                  if ((k + j) < rayCount) 
125                      for (int i_ring = 0; i_ring < ringCount; i_ring++) 
126                          colorArray[i_ring][k + j] = holdColor1; 
127              } 
128          } 
129      } 
130   
131      //--------------------------------------------------// 
132      // ComputeServer interpolated colors over entire spectrum // 
133      // -------------------------------------------------// 
134      public void rainbow() { 
135          float coeff = 0; 
136          for (int i = 0; i < tempColors.length; i++) { 
137              coeff = 3.0f * (float) i / (float) tempColors.length; 
138   
139              if (coeff <= 1f) //interpolate r-g 
140                  tempColors[i] = interpRG(coeff); 
141   
142              else if (coeff <= 2f) //interpolate g-b 
143                  tempColors[i] = interpGB(coeff - 1f); 
144   
145              else //interpolate b-r 
146                  tempColors[i] = interpBR(coeff - 2f); 
147          } 
148          tempColors[0] = java.awt.Color.white; 
149          tempColors[1] = java.awt.Color.black; 
150      } // end rainbow 
151   
152      //----------------------------- 
153      // color interpolation services 
154      //----------------------------- 
155      private static java.awt.Color interpRG(float alpha) { 
156          float red, green, blue; 
157          red = (1.0f - alpha) * 255.0f; 
158          green = alpha * 255f; 
159          return new java.awt.Color((int) red, (int) green, 0); 
160      } 
161   
162      private static java.awt.Color interpGB(float alpha) { 
163          float red, green, blue; 
164          green = (1.0f - alpha) * 255.0f; 
165          blue = alpha * 255f; 
166          return new java.awt.Color(0, (int) green, (int) blue); 
167      } 
168   
169      private static java.awt.Color interpBR(float alpha) { 
170          float red, green, blue; 
171          blue = (1.0f - alpha) * 255.0f; 
172          red = alpha * 255f; 
173          return new java.awt.Color((int) red, 0, (int) blue); 
174      } 
175   
176      //-------------------------------------- 
177      // main method for testing purposes only 
178      //-------------------------------------- 
179      public static void main(String args[]) { 
180          ColorArray myLUT; 
181          String str_nc; 
182          int nc; 
183          myLUT = new ColorArray(3, 4, true); 
184          System.out.println(myLUT); 
185          myLUT.shuffleRays(3); 
186  //      System.out.println(myLUT); 
187   
188      } // end main 
189   
190      public int getRingCount() { 
191          return ringCount; 
192      } 
193   
194      public void setRingCount(int ringCount) { 
195          this.ringCount = ringCount; 
196      } 
197   
198      public int getRayCount() { 
199          return rayCount; 
200      } 
201   
202      public void setRayCount(int rayCount) { 
203          this.rayCount = rayCount; 
204      } 
205   
206      public java.awt.Color[][] getColorArray() { 
207          return colorArray; 
208      } 
209   
210      public void setColorArray(java.awt.Color[][] colorArray) { 
211          this.colorArray = colorArray; 
212      } 
213   
214      public java.awt.Color[] getTempColors() { 
215          return tempColors; 
216      } 
217   
218      public void setTempColors(java.awt.Color[] tempColors) { 
219          this.tempColors = tempColors; 
220      } 
221   
222      public boolean isRadial() { 
223          return radial; 
224      } 
225   
226      public void setRadial(boolean radial) { 
227          this.radial = radial; 
228      } 
229   
230  } // end class graphics.carl.ColorArray 
231   
232   
233   
234