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

1    package ip.color; 
2     
3    import collections.sortable.QuickSort; 
4     
5    import java.awt.*; 
6    import java.util.Enumeration; 
7    import java.util.Hashtable; 
8    import java.util.Vector; 
9     
10   public class ColorHash extends Hashtable { 
11    
12       static Color colors[] = { 
13           new Color(24, 10, 38), 
14           new Color(255, 128, 100), 
15           new Color(233, 100, 200), 
16           new Color(24, 10, 38), 
17           new Color(233, 100, 200), 
18           new Color(24, 10, 38), 
19           new Color(233, 100, 200), 
20           new Color(24, 10, 38), 
21           new Color(233, 100, 200), 
22           new Color(24, 10, 38), 
23           new Color(10, 10, 10) 
24    
25       }; 
26    
27       public void addShortArrays(short r[][], short g[][], short b[][]) { 
28           for (int x = 0; x < r.length; x++) { 
29               for (int y = 0; y < r[0].length; y++) { 
30                   try { 
31                       Color c = new Color(r[x][y], g[x][y], b[x][y]); 
32                       put(c, c); 
33                   } catch (Exception e) { 
34                       Color c = new Color(clip(r[x][y]), clip(g[x][y]), clip(b[x][y])); 
35                       put(c, c); 
36                   } 
37               } 
38           } 
39       } 
40    
41       private int clip(int i) { 
42           if (i > 255) return 255; 
43           if (i < 0) return 0; 
44           return i; 
45       } 
46    
47       public int countColors() { 
48           int numberOfColors = 0; 
49           for (Enumeration e = this.elements(); 
50                e.hasMoreElements(); 
51                e.nextElement()) { 
52               numberOfColors++; 
53           } 
54           return numberOfColors; 
55       } 
56    
57       public void printColors() { 
58           Vector v = makeVector(); 
59           for (int i = 0; i < v.size(); i++) 
60               System.out.println("Vec:" + i + " " + v.elementAt(i)); 
61       } 
62       /** 
63        * Convert the internally held Hash table into 
64        * a vector of colors 
65        * @return color vector 
66        */ 
67       public Vector makeVector() { 
68           Vector v = new Vector(); 
69           for (Enumeration e = elements(); e.hasMoreElements();) { 
70               v.addElement(new Pixel((Color) e.nextElement())); 
71           } 
72           return v; 
73       } 
74    
75    
76       public static void main(String args[]) { 
77           Color c = new Color(255, 128, 100); 
78           ColorHash ch = new ColorHash(); 
79           Object o; 
80           for (int i = 0; i < colors.length; i++) { 
81               Color p = colors[i]; 
82               ch.put(p, p); 
83           } 
84           System.out.println( 
85                   "ColorHash contains :" 
86                   + ch.countColors()); 
87           ch.printColors(); 
88    
89           // sort the colors here! 
90           Vector vb = new Vector(); 
91           Vector v = ch.makeVector(); 
92           QuickSort.sort(v, vb, 0, v.size(), true); 
93    
94           for (int i = 0; i < v.size(); i++) { 
95               System.out.println("Sorted Vec:" + i + " " + v.elementAt(i)); 
96           } 
97       } 
98    
99   } 
100   
101   
102   
103   
104