Java Color Distance getMaxDistance(final Color first, final Color second)

Here you can find the source of getMaxDistance(final Color first, final Color second)

Description

Returns the largest absolute difference between the red, green and blue values of the two colors.

License

Apache License

Parameter

Parameter Description
first The first color to test.
second The second color to test.

Return

The largest absolute difference between the red, green and blue values of the two colors, will be a value between 0 and 255.

Declaration

public static int getMaxDistance(final Color first, final Color second) 

Method Source Code


//package com.java2s;
/*// www.  ja v a 2  s.c o  m
 *  Copyright 2012 Eric F. Savage, code@efsavage.com
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

import java.awt.Color;

public class Main {
    /**
     * Returns the largest absolute difference between the red, green and blue
     * values of the two colors.
     * 
     * @param first
     *            The first color to test.
     * @param second
     *            The second color to test.
     * @return The largest absolute difference between the red, green and blue
     *         values of the two colors, will be a value between 0 and 255.
     */
    public static int getMaxDistance(final Color first, final Color second) {
        int distance = Math.abs(first.getRed() - second.getRed());
        if (Math.abs(first.getGreen() - second.getGreen()) > distance) {
            distance = Math.abs(first.getGreen() - second.getGreen());
        }
        if (Math.abs(first.getBlue() - second.getBlue()) > distance) {
            distance = Math.abs(first.getBlue() - second.getBlue());
        }
        return Math.abs(distance);
    }
}

Related

  1. distance(Color a, Color b)
  2. distance(Color c1, Color c2)
  3. distanceToColor(final int distance)
  4. drawArc(Point2D start, double distance, double startAngle, double arcAngle, boolean fill, Graphics2D g2, Color color)
  5. getDistance(int c1, Color c2)
  6. pixelDistance(final Color col1, final Color col2)