are Colors Within Tolerance - Java 2D Graphics

Java examples for 2D Graphics:Color

Description

are Colors Within Tolerance

Demo Code


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

public class Main {
    /**/*from   w ww. j  ava2s. c om*/
     * 
     * @param color1
     *            the first color
     * @param color2
     *            the second color param t - the rgb values which can vary. If c
     *            = new Color(200,200,200) Such as tolerance = 10) it could
     *            return anywhere from (190,190,190) - (210,210,210)
     * @param t tolerance
     * @return true if the colors are within the tolerance
     */
    public static boolean areColorsWithinTolerance(Color color1,
            Color color2, int t) {
        Color tolerance = new Color(t, t, t);
        return (color1.getRed() - color2.getRed() < tolerance.getRed() && color1
                .getRed() - color2.getRed() > -tolerance.getRed())
                && (color1.getBlue() - color2.getBlue() < tolerance
                        .getBlue() && color1.getBlue() - color2.getBlue() > -tolerance
                        .getBlue())
                && (color1.getGreen() - color2.getGreen() < tolerance
                        .getGreen() && color1.getGreen()
                        - color2.getGreen() > -tolerance.getGreen());
    }

    /**
     * 
     * @param color1
     *            the first color
     * @param color2
     *            the second color param tolerance - the rgb values which can
     *            vary. If c = new Color(200,200,200) Such as tolerance = new
     *            Color(11,3,4) it could return anywhere from (189,197,196) -
     *            (211,203,204)
     * @return true if the colors are within the tolerance
     */
    public static boolean areColorsWithinTolerance(Color color1,
            Color color2, Color tolerance) {
        return (color1.getRed() - color2.getRed() < tolerance.getRed() && color1
                .getRed() - color2.getRed() > -tolerance.getRed())
                && (color1.getBlue() - color2.getBlue() < tolerance
                        .getBlue() && color1.getBlue() - color2.getBlue() > -tolerance
                        .getBlue())
                && (color1.getGreen() - color2.getGreen() < tolerance
                        .getGreen() && color1.getGreen()
                        - color2.getGreen() > -tolerance.getGreen());
    }
}

Related Tutorials