Java Color Contrast contrast(@Nonnull Color color)

Here you can find the source of contrast(@Nonnull Color color)

Description

Return the color (Black/White) that most contrasts with the specified color.

License

Open Source License

Parameter

Parameter Description
color the source color

Return

the contrasting color

Declaration

public static Color contrast(@Nonnull Color color) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Color;

import javax.annotation.Nonnull;

public class Main {
    /**/*from   ww  w  . jav  a 2  s.c o m*/
     * Return the color (Black/White) that most contrasts with the specified
     * color.
     *
     * @param color the source color
     * @return the contrasting color
     */
    public static Color contrast(@Nonnull Color 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;
    }
}

Related

  1. contrast(Color col)
  2. contrastBW(Color c)
  3. contrastColorByShift(Color color, int shift)