Skip to content

Instantly share code, notes, and snippets.

@jittagornp
Last active July 4, 2024 00:28
Show Gist options
  • Save jittagornp/6c7fcdab388fe4863c34 to your computer and use it in GitHub Desktop.
Save jittagornp/6c7fcdab388fe4863c34 to your computer and use it in GitHub Desktop.
for find dominant color of image
package com.pamarin.api.util;
import java.awt.image.BufferedImage;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* @author jittagornp
*
* thank you
* http://stackoverflow.com/questions/10530426/how-can-i-find-dominant-color-of-an-image
*/
public class ImageDominantColor {
public static String getHexColor(BufferedImage image) {
Map<Integer, Integer> colorMap = new HashMap<>();
int height = image.getHeight();
int width = image.getWidth();
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int rgb = image.getRGB(i, j);
if (!isGray(getRGBArr(rgb))) {
Integer counter = colorMap.get(rgb);
if (counter == null) {
counter = 0;
}
colorMap.put(rgb, ++counter);
}
}
}
return getMostCommonColor(colorMap);
}
private static String getMostCommonColor(Map<Integer, Integer> map) {
List<Map.Entry<Integer, Integer>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, (Map.Entry<Integer, Integer> obj1, Map.Entry<Integer, Integer> obj2)
-> ((Comparable) obj1.getValue()).compareTo(obj2.getValue()));
Map.Entry<Integer, Integer> entry = list.get(list.size() - 1);
int[] rgb = getRGBArr(entry.getKey());
return "#" + Integer.toHexString(rgb[0])
+ Integer.toHexString(rgb[1])
+ Integer.toHexString(rgb[2]);
}
private static int[] getRGBArr(int pixel) {
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
return new int[]{red, green, blue};
}
private static boolean isGray(int[] rgbArr) {
int rgDiff = rgbArr[0] - rgbArr[1];
int rbDiff = rgbArr[0] - rgbArr[2];
// Filter out black, white and grays...... (tolerance within 10 pixels)
int tolerance = 10;
if (rgDiff > tolerance || rgDiff < -tolerance) {
if (rbDiff > tolerance || rbDiff < -tolerance) {
return false;
}
}
return true;
}
}
@Kalamii-Senpai
Copy link

big O notation for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment