Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Java class to look up a color name from the rgb values.
import java.util.ArrayList;
public class ColorNameLookup {
public class ColorName {
public int r,g,b;
public String name;
public ColorName(String name, int r, int g, int b) {
this.r = r;
this.g = g;
this.b = b;
this.name = name;
}
public computeMSE(int pixR, int pixG, int pixB) {
return ((pixR-r)*(pixR-r) + (pixG-g)*(pixG-g) + (pixB-b)*(pixB-b))/3);
}
public int getR() {
return r;
}
public int getG() {
return g;
}
public int getB() {
return b;
}
public String getName() {
return name;
}
}
ArrayList<ColorName> colorList;
public class ColorNameLookup() {
colorList.add("AliceBlue",0xF0,0xF8,0xFF);
colorList.add("AntiqueWhite",0xFA,0xEB,0xD7);
colorList.add("Aqua",0x00,0xFF,0xFF);
colorList.add("Aquamarine",0x7F,0xFF,0xD4);
colorList.add("Azure",0xF0,0xFF,0xFF);
colorList.add("Beige",0xF5,0xF5,0xDC);
colorList.add("Bisque",0xFF,0xE4,0xC4);
colorList.add("Black",0x00,0x00,0x00);
colorList.add("BlanchedAlmond",0xFF,0xEB,0xCD);
colorList.add("Blue",0x00,0x00,0xFF);
colorList.add("BlueViolet",0x8A,0x2B,0xE2);
colorList.add("Brown",0xA5,0x2A,0x2A);
colorList.add("BurlyWood",0xDE,0xB8,0x87);
colorList.add("CadetBlue",0x5F,0x9E,0xA0);
colorList.add("Chartreuse",0x7F,0xFF,0x00);
colorList.add("Chocolate",0xD2,0x69,0x1E);
colorList.add("Coral",0xFF,0x7F,0x50);
colorList.add("CornflowerBlue",0x64,0x95,0xED);
colorList.add("Cornsilk",0xFF,0xF8,0xDC);
colorList.add("Crimson",0xDC,0x14,0x3C);
colorList.add("Cyan",0x00,0xFF,0xFF);
colorList.add("DarkBlue",0x00,0x00,0x8B);
colorList.add("DarkCyan",0x00,0x8B,0x8B);
colorList.add("DarkGoldenRod",0xB8,0x86,0x0B);
colorList.add("DarkGray",0xA9,0xA9,0xA9);
colorList.add("DarkGreen",0x00,0x64,0x00);
colorList.add("DarkKhaki",0xBD,0xB7,0x6B);
colorList.add("DarkMagenta",0x8B,0x00,0x8B);
colorList.add("DarkOliveGreen",0x55,0x6B,0x2F);
colorList.add("DarkOrange",0xFF,0x8C,0x00);
colorList.add("DarkOrchid",0x99,0x32,0xCC);
colorList.add("DarkRed",0x8B,0x00,0x00);
colorList.add("DarkSalmon",0xE9,0x96,0x7A);
colorList.add("DarkSeaGreen",0x8F,0xBC,0x8F);
colorList.add("DarkSlateBlue",0x48,0x3D,0x8B);
colorList.add("DarkSlateGray",0x2F,0x4F,0x4F);
colorList.add("DarkTurquoise",0x00,0xCE,0xD1);
colorList.add("DarkViolet",0x94,0x00,0xD3);
colorList.add("DeepPink",0xFF,0x14,0x93);
colorList.add("DeepSkyBlue",0x00,0xBF,0xFF);
colorList.add("DimGray",0x69,0x69,0x69);
colorList.add("DodgerBlue",0x1E,0x90,0xFF);
colorList.add("FireBrick",0xB2,0x22,0x22);
colorList.add("FloralWhite",0xFF,0xFA,0xF0);
colorList.add("ForestGreen",0x22,0x8B,0x22);
colorList.add("Fuchsia",0xFF,0x00,0xFF);
colorList.add("Gainsboro",0xDC,0xDC,0xDC);
colorList.add("GhostWhite",0xF8,0xF8,0xFF);
colorList.add("Gold",0xFF,0xD7,0x00);
colorList.add("GoldenRod",0xDA,0xA5,0x20);
colorList.add("Gray",0x80,0x80,0x80);
colorList.add("Green",0x00,0x80,0x00);
colorList.add("GreenYellow",0xAD,0xFF,0x2F);
colorList.add("HoneyDew",0xF0,0xFF,0xF0);
colorList.add("HotPink",0xFF,0x69,0xB4);
colorList.add("IndianRed",0xCD,0x5C,0x5C);
colorList.add("Indigo",0x4B,0x00,0x82);
colorList.add("Ivory",0xFF,0xFF,0xF0);
colorList.add("Khaki",0xF0,0xE6,0x8C);
colorList.add("Lavender",0xE6,0xE6,0xFA);
colorList.add("LavenderBlush",0xFF,0xF0,0xF5);
colorList.add("LawnGreen",0x7C,0xFC,0x00);
colorList.add("LemonChiffon",0xFF,0xFA,0xCD);
colorList.add("LightBlue",0xAD,0xD8,0xE6);
colorList.add("LightCoral",0xF0,0x80,0x80);
colorList.add("LightCyan",0xE0,0xFF,0xFF);
colorList.add("LightGoldenRodYellow",0xFA,0xFA,0xD2);
colorList.add("LightGray",0xD3,0xD3,0xD3);
colorList.add("LightGreen",0x90,0xEE,0x90);
colorList.add("LightPink",0xFF,0xB6,0xC1);
colorList.add("LightSalmon",0xFF,0xA0,0x7A);
colorList.add("LightSeaGreen",0x20,0xB2,0xAA);
colorList.add("LightSkyBlue",0x87,0xCE,0xFA);
colorList.add("LightSlateGray",0x77,0x88,0x99);
colorList.add("LightSteelBlue",0xB0,0xC4,0xDE);
colorList.add("LightYellow",0xFF,0xFF,0xE0);
colorList.add("Lime",0x00,0xFF,0x00);
colorList.add("LimeGreen",0x32,0xCD,0x32);
colorList.add("Linen",0xFA,0xF0,0xE6);
colorList.add("Magenta",0xFF,0x00,0xFF);
colorList.add("Maroon",0x80,0x00,0x00);
colorList.add("MediumAquaMarine",0x66,0xCD,0xAA);
colorList.add("MediumBlue",0x00,0x00,0xCD);
colorList.add("MediumOrchid",0xBA,0x55,0xD3);
colorList.add("MediumPurple",0x93,0x70,0xDB);
colorList.add("MediumSeaGreen",0x3C,0xB3,0x71);
colorList.add("MediumSlateBlue",0x7B,0x68,0xEE);
colorList.add("MediumSpringGreen",0x00,0xFA,0x9A);
colorList.add("MediumTurquoise",0x48,0xD1,0xCC);
colorList.add("MediumVioletRed",0xC7,0x15,0x85);
colorList.add("MidnightBlue",0x19,0x19,0x70);
colorList.add("MintCream",0xF5,0xFF,0xFA);
colorList.add("MistyRose",0xFF,0xE4,0xE1);
colorList.add("Moccasin",0xFF,0xE4,0xB5);
colorList.add("NavajoWhite",0xFF,0xDE,0xAD);
colorList.add("Navy",0x00,0x00,0x80);
colorList.add("OldLace",0xFD,0xF5,0xE6);
colorList.add("Olive",0x80,0x80,0x00);
colorList.add("OliveDrab",0x6B,0x8E,0x23);
colorList.add("Orange",0xFF,0xA5,0x00);
colorList.add("OrangeRed",0xFF,0x45,0x00);
colorList.add("Orchid",0xDA,0x70,0xD6);
colorList.add("PaleGoldenRod",0xEE,0xE8,0xAA);
colorList.add("PaleGreen",0x98,0xFB,0x98);
colorList.add("PaleTurquoise",0xAF,0xEE,0xEE);
colorList.add("PaleVioletRed",0xDB,0x70,0x93);
colorList.add("PapayaWhip",0xFF,0xEF,0xD5);
colorList.add("PeachPuff",0xFF,0xDA,0xB9);
colorList.add("Peru",0xCD,0x85,0x3F);
colorList.add("Pink",0xFF,0xC0,0xCB);
colorList.add("Plum",0xDD,0xA0,0xDD);
colorList.add("PowderBlue",0xB0,0xE0,0xE6);
colorList.add("Purple",0x80,0x00,0x80);
colorList.add("Red",0xFF,0x00,0x00);
colorList.add("RosyBrown",0xBC,0x8F,0x8F);
colorList.add("RoyalBlue",0x41,0x69,0xE1);
colorList.add("SaddleBrown",0x8B,0x45,0x13);
colorList.add("Salmon",0xFA,0x80,0x72);
colorList.add("SandyBrown",0xF4,0xA4,0x60);
colorList.add("SeaGreen",0x2E,0x8B,0x57);
colorList.add("SeaShell",0xFF,0xF5,0xEE);
colorList.add("Sienna",0xA0,0x52,0x2D);
colorList.add("Silver",0xC0,0xC0,0xC0);
colorList.add("SkyBlue",0x87,0xCE,0xEB);
colorList.add("SlateBlue",0x6A,0x5A,0xCD);
colorList.add("SlateGray",0x70,0x80,0x90);
colorList.add("Snow",0xFF,0xFA,0xFA);
colorList.add("SpringGreen",0x00,0xFF,0x7F);
colorList.add("SteelBlue",0x46,0x82,0xB4);
colorList.add("Tan",0xD2,0xB4,0x8C);
colorList.add("Teal",0x00,0x80,0x80);
colorList.add("Thistle",0xD8,0xBF,0xD8);
colorList.add("Tomato",0xFF,0x63,0x47);
colorList.add("Turquoise",0x40,0xE0,0xD0);
colorList.add("Violet",0xEE,0x82,0xEE);
colorList.add("Wheat",0xF5,0xDE,0xB3);
colorList.add("White",0xFF,0xFF,0xFF);
colorList.add("WhiteSmoke",0xF5,0xF5,0xF5);
colorList.add("Yellow",0xFF,0xFF,0x00);
colorList.add("YellowGreen",0x9A,0xCD,0x32);
}
public String getColorName(int r, int g, int b) {
ColorName closestMatch = null;
int minMSE = Integer.MAX_VALUE;
int mse;
for (ColorName c : colorList) {
mse = c.computeMSE(r, g, b);
if (mse < minMSE) {
minMSE = mse;
closestMatch = c;
}
}
if (closestMatch != null) {
return closestMatch.getName();
} else {
return null;
}
}
}
@treolandix

This comment has been minimized.

Copy link

commented Oct 31, 2015

Hey, do you know which color format this is and how to convert it to a hex code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.