Java BufferedImage Color getUniqueColor(BufferedImage origImg, boolean findLightColor)

Here you can find the source of getUniqueColor(BufferedImage origImg, boolean findLightColor)

Description

get Unique Color

License

Apache License

Declaration

public static Color getUniqueColor(BufferedImage origImg, boolean findLightColor) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.Color;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;

import java.awt.Transparency;

import java.awt.image.BufferedImage;

import java.awt.image.ColorModel;
import java.awt.image.DataBufferInt;

import java.awt.image.PixelGrabber;

import java.util.Arrays;

import javax.swing.ImageIcon;

public class Main {
    public static Color getUniqueColor(BufferedImage origImg, boolean findLightColor) {
        BufferedImage img = createImageCopy(origImg);
        Color pixelColor = Color.WHITE;
        Color uniqueColor = Color.WHITE;
        int[] pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
        Arrays.sort(pixels);// ww w.ja  v a2  s.c  om
        int iter = 0;
        // System.out.println("Pixel Length: "+pixels.length);
        for (int i = 0; i < pixels.length; i++) {
            iter++;
            int pixel = pixels[i];
            int alpha = pixel >> 24 & 0xFF; // Alpha
            if (alpha < 200)
                continue;
            if (findLightColor) {
                if (isLightColor(pixel))
                    continue;
            } else if (isDarkColor(pixel))
                continue;
            pixelColor = new Color(pixel);
            uniqueColor = getInverseColor(pixelColor);
            if (findLightColor) {
                if (isDarkColor(uniqueColor.getRGB()))
                    continue;
            } else if (isLightColor(uniqueColor.getRGB()))
                continue;
            int index = Arrays.binarySearch(pixels, uniqueColor.getRGB());
            if (index < 0)
                break;
        }
        // System.out.println("Unique Color RGB: "+getColor(uniqueColor.getRGB()));
        // System.out.println("Pixel Color: "+pixelColor+" Found Unique Color: "+
        // uniqueColor+" after iter: "+iter);
        return uniqueColor;
    }

    public static BufferedImage createImageCopy(Image image) {
        return createImageCopy(image, false);
    }

    public static BufferedImage createImageCopy(Image image, boolean isTranslucent) {
        // if (image instanceof BufferedImage) return (BufferedImage)image;
        // This code ensures that all the pixels in the image are loaded
        image = new ImageIcon(image).getImage();

        // Determine if the image has transparent pixels; for this method's
        // implementation, see Determining If an Image Has Transparent Pixels
        boolean hasAlpha = hasAlpha(image);

        // System.out.println("Image Has Alpha: "+hasAlpha);
        // Create a buffered image with a format that's compatible with the
        // screen
        BufferedImage bimage = createImage(image.getWidth(null), image.getHeight(null), hasAlpha, isTranslucent);

        // Copy image to buffered image
        Graphics g = bimage.createGraphics();

        // Paint the image onto the buffered image
        g.drawImage(image, 0, 0, null);
        g.dispose();

        return bimage;
    }

    public static boolean isLightColor(int pixel) {
        int red = pixel >> 16 & 0xFF; // Red
        int green = pixel >> 8 & 0xFF; // Green
        int blue = pixel & 0xFF; // blue
        if (red > 200 && blue > 200)
            return true;
        if (red > 200 && green > 200)
            return true;
        if (blue > 200 && green > 200)
            return true;
        return false;
    }

    public static boolean isDarkColor(int pixel) {
        int red = pixel >> 16 & 0xFF; // Red
        int green = pixel >> 8 & 0xFF; // Green
        int blue = pixel & 0xFF; // blue
        if (red < 100)
            return true;
        if (green < 100)
            return true;
        if (blue < 100)
            return true;
        return false;
    }

    public static Color getInverseColor(Color color) {
        int red = color.getRed();
        int green = color.getGreen();
        int blue = color.getBlue();
        int alpha = color.getAlpha();
        return new Color(255 - red, 255 - green, 255 - blue, alpha);
    }

    public static boolean hasAlpha(Image image) {
        // Get the image's color model
        ColorModel cm = getColorModel(image);
        // System.out.println("Color Model Class: "+(cm.getClass().getName())+" Data: "+cm);
        return cm.hasAlpha();
    }

    public static BufferedImage createImage(int imgWt, int imgHt, boolean hasAlpha) {
        return createImage(imgWt, imgHt, hasAlpha, false);
    }

    public static BufferedImage createImage(int imgWt, int imgHt, boolean hasAlpha, boolean translucent) {
        // Create a buffered image with a format that's compatible with the
        // screen
        BufferedImage bimage = null;
        try {
            // Determine the type of transparency of the new buffered image
            int transparency = Transparency.OPAQUE;
            if (hasAlpha)
                transparency = Transparency.BITMASK;
            if (translucent)
                transparency = Transparency.TRANSLUCENT;
            // Create the buffered image
            GraphicsConfiguration gc = getDefaultConfiguration();
            bimage = gc.createCompatibleImage(imgWt, imgHt, transparency);
            // RGPTLogger.logToFile("Created Image using GraphicsConfiguration");
        } catch (HeadlessException e) {
            e.printStackTrace();
        }

        if (bimage == null) {
            // Create a buffered image using the default color model
            int type = BufferedImage.TYPE_INT_RGB;
            if (hasAlpha) {
                type = BufferedImage.TYPE_INT_ARGB;
            }
            bimage = new BufferedImage(imgWt, imgHt, type);
        }
        return bimage;
    }

    public static void drawImage(BufferedImage srcImg, BufferedImage img2Draw, int w, int h) {
        if (w == -1)
            w = (int) (srcImg.getWidth() / 2);
        if (h == -1)
            h = (int) (srcImg.getHeight() / 2);
        System.out.println("AWT Image Wt: " + w + " And Ht: " + h);
        Graphics2D g2 = srcImg.createGraphics();
        g2.drawImage(img2Draw, w, h, null);
        g2.dispose();
    }

    public static ColorModel getColorModel(Image image) {
        // If buffered image, the color model is readily available
        if (image instanceof BufferedImage) {
            BufferedImage bimage = (BufferedImage) image;
            return bimage.getColorModel();
        }

        // Use a pixel grabber to retrieve the image's color model, grabbing a
        // single
        // pixel is usually sufficient
        PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
        try {
            pg.grabPixels();
        } catch (InterruptedException e) {
        }

        // Get the image's color model
        ColorModel cm = pg.getColorModel();
        return cm;
    }

    public static GraphicsConfiguration getDefaultConfiguration() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        return gd.getDefaultConfiguration();
    }
}

Related

  1. makeColorTransparent(BufferedImage image, Color color)