Java Utililty Methods Color Contrast

List of utility methods to do Color Contrast

Description

The list of methods to do Color Contrast are organized into topic(s).

Method

Colorcontrast(@Nonnull Color color)
Return the color (Black/White) that most contrasts with the specified color.
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int average = (red + green + blue) / 3;
return (average >= 128) ? Color.BLACK : Color.WHITE;
Colorcontrast(Color col)
contrast
int max = Math.max(col.getRed(), Math.max(col.getGreen(), col.getBlue()));
if (max > 128) {
    return (new Color(col.getRed() / 2, col.getGreen() / 2, col.getBlue() / 2, col.getAlpha()));
} else if (max == 0) {
    return (Color.WHITE);
} else {
    int f = 128 / max;
    return (new Color(col.getRed() * f, col.getGreen() * f, col.getBlue() * f, col.getAlpha()));
...
ColorcontrastBW(Color c)
contrast BW
if ((c.getRed() + c.getGreen() + c.getBlue()) > 200.0) {
    return (Color.black);
} else {
    return (Color.white);
ColorcontrastColorByShift(Color color, int shift)
Create a contrasting RGB color to a color by shifting the color (making it brighter or darker) so that if used as foreground/background they are different enough to be readable.
float alpha = (color == null) ? 1.0F : color.getAlpha() / 256.0F;
if (color == null) {
    color = Color.BLACK;
if (shift < 1) {
    shift = 128;
int r1 = color.getRed();
...