According the official documentation Color.parseColor not only accepts hex values but also the color names red, blue, green, black, white, gray, cyan, magenta, yellow, lightgray, darkgray, grey, lightgrey, darkgrey, aqua, fuchsia, lime, maroon, navy, olive, purple, silver, and teal.
Are the values of those hardcoded or can I customize which kind of green gets produced?
Let's take a look at source of parseColor()
within android.graphics.Color
:
@ColorInt
public static int parseColor(@Size(min=1) String colorString) {
if (colorString.charAt(0) == '#') {
// Use a long to avoid rollovers on #ffXXXXXX
long color = Long.parseLong(colorString.substring(1), 16);
if (colorString.length() == 7) {
// Set the alpha value
color |= 0x00000000ff000000;
} else if (colorString.length() != 9) {
throw new IllegalArgumentException("Unknown color");
}
return (int)color;
} else {
Integer color = sColorNameMap.get(colorString.toLowerCase(Locale.ROOT));
if (color != null) {
return color;
}
}
throw new IllegalArgumentException("Unknown color");
}
So firstly the first character of the string is checked for a #
, symbolising if we're using an RGB or ARGB value. But we're not interested in that, so skip to the else
statement where we're looking to see if the colorString
argument matches a key within sColorNameMap
:
static {
sColorNameMap = new HashMap<>();
sColorNameMap.put("black", BLACK);
sColorNameMap.put("darkgray", DKGRAY);
sColorNameMap.put("gray", GRAY);
sColorNameMap.put("lightgray", LTGRAY);
sColorNameMap.put("white", WHITE);
sColorNameMap.put("red", RED);
sColorNameMap.put("green", GREEN);
sColorNameMap.put("blue", BLUE);
sColorNameMap.put("yellow", YELLOW);
sColorNameMap.put("cyan", CYAN);
sColorNameMap.put("magenta", MAGENTA);
sColorNameMap.put("aqua", 0xFF00FFFF);
sColorNameMap.put("fuchsia", 0xFFFF00FF);
sColorNameMap.put("darkgrey", DKGRAY);
sColorNameMap.put("grey", GRAY);
sColorNameMap.put("lightgrey", LTGRAY);
sColorNameMap.put("lime", 0xFF00FF00);
sColorNameMap.put("maroon", 0xFF800000);
sColorNameMap.put("navy", 0xFF000080);
sColorNameMap.put("olive", 0xFF808000);
sColorNameMap.put("purple", 0xFF800080);
sColorNameMap.put("silver", 0xFFC0C0C0);
sColorNameMap.put("teal", 0xFF008080);
}
So if we get a match, that colour ARGB value is returned (some named ARGB values are defined as @ColorInt int
constants at the top of the class). If not, then an IllegalArgumentException
is thrown.
So to answer your question, yes, these values are hardcoded. If you want to use your own values, define them within your app's colors.xml
file.