 
package pl.gda.biomed.jwr.color;

import java.awt.image.*;

/**
 *
 * @author jwr
 */
public class AverageColor {
    /**
     * Calculate average color
     * @param bi
     * @return 
     */
    public int[] getHistDominantColor(BufferedImage bi){
        int []rgb=new int[3];
        int w=bi.getWidth();
        int h=bi.getHeight();
        
        int hr[]=new int [256];
        int hg[]=new int [256];
        int hb[]=new int [256];
        
        int v;
        int r;
        int g;
        int b;
        
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
              v=bi.getRGB(j, i);
              r=(v>>16)&0xff;
              g=(v>>8)&0xff;
              b=(v)&0xff;
              hr[r]++;
              hg[g]++;
              hb[b]++;
            }
        }
        
        int maxHr=0;
        int maxHg=0;
        int maxHb=0;
        
        int dr=0;
        int dg=0;
        int db=0;
               
        for(int i=0;i<256;i++){
            if(hr[i]>maxHr){
                maxHr=hr[i];
                dr=i;
            }
            if(hg[i]>maxHg){
                maxHg=hg[i];
                dg=i;
            }
            if(hb[i]>maxHb){
                maxHb=hb[i];
                db=i;
            }
        }
        rgb[0]=dr;
        rgb[1]=dg;
        rgb[2]=db;
        return rgb;
    }
}


