get Contrasting Colors - Java 2D Graphics

Java examples for 2D Graphics:Color Light

Description

get Contrasting Colors

Demo Code


//package com.java2s;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] argv) throws Exception {
        List colors = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(getContrastingColors(colors));
    }// ww w.j a v a 2s.c  o m

    public static List<Color> getContrastingColors(final List<Color> colors) {
        final List<Color> list = new ArrayList<Color>();

        if (colors != null) {
            for (final Color c : colors)
                list.add(getContrastingColor(c));
        }

        return list;
    }

    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
            d = 255; // dark colors - white font

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

Related Tutorials