Inverts a color (for each i in RGB, changes i to 255-i). - Java 2D Graphics

Java examples for 2D Graphics:Color RGB

Description

Inverts a color (for each i in RGB, changes i to 255-i).

Demo Code


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

public class Main {
    /**//from   ww w  . ja v a 2s  .c  o  m
     * Inverts a color (for each i in RGB, changes i to 255-i). Keeps alpha the
     * same.
     * 
     * @param color
     *            the color to invert.
     * @return the inverted color.
     */
    public static Color invert(Color color) {
        int r = 255 - color.getRed();
        int g = 255 - color.getGreen();
        int b = 255 - color.getBlue();

        return new Color(r, g, b, color.getAlpha());
    }
}

Related Tutorials