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

1    package ip.color; 
2     
3    import collections.sortable.Comparable2; 
4     
5    import java.awt.*; 
6     
7    public class Pixel implements Comparable2 { 
8        Color c; 
9        int n = 1; 
10    
11       Pixel(Color _c) { 
12           c = _c; 
13       } 
14    
15       //  p.equals(p1) 
16       // return true if p == p1 
17       public boolean equals(Object p) { 
18           Pixel p1 = (Pixel) p; 
19           return p1.c.equals(c); 
20       } 
21    
22       // Compute the Euclidean distance in 
23       // RGB space 
24       public double distance(Pixel p1) { 
25           int dr = c.getRed() - p1.c.getRed(); 
26           int dg = c.getGreen() - p1.c.getGreen(); 
27           int db = c.getBlue() - p1.c.getBlue(); 
28           return 
29                   Math.sqrt(dr * dr + dg * dg + db * db); 
30       } 
31    
32       //  p.isGreater(p1) 
33       // return true if p > p1 
34       public boolean isGreater(Object p) { 
35           Pixel p1 = (Pixel) p; 
36           int dr = c.getRed() - p1.c.getRed(); 
37           int dg = c.getGreen() - p1.c.getGreen(); 
38           int db = c.getBlue() - p1.c.getBlue(); 
39           int sum = dr + dg + db; 
40           return (sum > 0); 
41       } 
42    
43       public boolean isLess(Object p) { 
44           if (isGreater(p) || equals(p)) { 
45               return false; 
46           } 
47           return true; 
48       } 
49    
50       public String toString() { 
51           return c.toString() + "[n=" + n + "]"; 
52       } 
53   }