get Contrasting Color - Java 2D Graphics

Java examples for 2D Graphics:Color Light

Description

get Contrasting Color

Demo Code


//package com.java2s;
import java.awt.Color;

public class Main {
    public static Color getContrastingColor(final Color color) {
        int d = 0;
        // Counting the perceptive luminance - human eye favors green color...
        final double a = 1 - (0.299 * color.getRed() + 0.587
                * color.getGreen() + 0.114 * color.getBlue()) / 255;

        if (a < 0.5)
            d = 0; // bright colors - black font
        else/*from  w  w  w .  j  ava  2  s .c o m*/
            d = 255; // dark colors - white font

        return new Color(d, d, d);
    }
}

Related Tutorials