001    /*
002     * RGBColor
003     * 
004     * Copyright (c) 2001, 2002, 2003 Marco Schmidt.
005     * All rights reserved.
006     */
007    
008    package net.sourceforge.jiu.color.quantization;
009    
010    import net.sourceforge.jiu.data.RGBIndex;
011    
012    /**
013     * Encapsulates a single color from RGB (red, green, blue) color space plus a frequency counter.
014     * Each of the three RGB samples is of type int.
015     * Also stores a counter of type int.
016     * @author Marco Schmidt
017     */
018    public class RGBColor implements RGBIndex
019    {
020            /** The intensity values that make up the color. */
021            private int[] samples;
022    
023            /** Stores how many times this colors appears in a certain image. */
024            private int counter;
025    
026            /**
027             * Creates an instance of this class and initializes it to the given
028             * intensity values.
029             * The internal color counter is set to zero.
030             */
031            public RGBColor(int red, int green, int blue)
032            {
033                    this(red, green, blue, 0);
034            }
035    
036            /**
037             * Creates an instance of this class and initializes it to the given
038             * intensity values.
039             * Also sets the internal color counter to the given parameter.
040             */
041            public RGBColor(int red, int green, int blue, int counter)
042            {
043                    samples = new int[3];
044                    samples[INDEX_RED] = red;
045                    samples[INDEX_GREEN] = green;
046                    samples[INDEX_BLUE] = blue;
047                    this.counter = counter;
048            }
049    
050            /**
051             * Compares this color to the argument color, using the sortOrder argument (which is one of the
052             * three index values defined in {@link RGBIndex}.
053             * That way, the two sample values for one component (e.g. red if sortOrder == INDEX_RED) are
054             * compared.
055             *
056             * @param c the color to which this color is compared
057             * @param sortOrder the component used for the comparison
058             * @return relation between this color and the argument color
059             */
060            public int compareTo(RGBColor c, int sortOrder)
061            {
062                    int s1 = samples[sortOrder];
063                    int s2 = c.samples[sortOrder];
064                    if (s1 < s2)
065                    {
066                            return -1;
067                    }
068                    else
069                    if (s1 == s2)
070                    {
071                            return 0;
072                    }
073                    else
074                    {
075                            return 1;
076                    }
077            }
078    
079            /**
080             * For two RGB triplets (r1, g1, b1) and (r2, g2, b2) this will return
081             * the distance between those colors in RGB color space.
082             */
083            public static double computeDistance(int r1, int g1, int b1, int r2, int g2, int b2)
084            {
085                    int r = r1 - r2;
086                    int g = g1 - g2;
087                    int b = b1 - b2;
088                    return Math.sqrt(r * r + g * g + b * b);
089            }
090    
091            /**
092             * Compares this color with another instance of RGBColor and returns true
093             * if all intensity values are equal, false otherwise.
094             */
095            public boolean equals(Object obj)
096            {
097                    RGBColor c = (RGBColor)obj;
098                    return (samples[0] == c.samples[0] && samples[1] == c.samples[1] && samples[2] == c.samples[2]);
099            }
100    
101            public int getCounter()
102            {
103                    return counter;
104            }
105    
106            public int getSample(int index)
107            {
108                    return samples[index];
109            }
110    
111            public String toString()
112            {
113                    return "(" + samples[INDEX_RED] + ", " + samples[INDEX_GREEN] + ", " + samples[INDEX_BLUE] + ")";
114            }
115    }