Java Color Distance pixelDistance(final Color col1, final Color col2)

Here you can find the source of pixelDistance(final Color col1, final Color col2)

Description

Calculates the distance between two colors.

License

Open Source License

Parameter

Parameter Description
col1 first color
col2 second color

Return

distance between col1 and col2

Declaration

private static double pixelDistance(final Color col1, final Color col2) 

Method Source Code

//package com.java2s;
/**//  w w  w . j av a 2 s.  c o m
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Color;

public class Main {
    /**
     * Calculates the distance between two colors.
     * @param col1 first color
     * @param col2 second color
     * @return distance between col1 and col2
     */
    private static double pixelDistance(final Color col1, final Color col2) {
        int r1 = col1.getRed();
        int g1 = col1.getGreen();
        int b1 = col1.getBlue();
        int r2 = col2.getRed();
        int g2 = col2.getGreen();
        int b2 = col2.getBlue();

        double pixelDistance = Math
                .sqrt(((r1 - r2) * (r1 - r2)) + ((g1 - g2) * (g1 - g2)) + ((b1 - b2) * (b1 - b2)));

        return pixelDistance;
    }
}

Related

  1. distance(Color c1, Color c2)
  2. distanceToColor(final int distance)
  3. drawArc(Point2D start, double distance, double startAngle, double arcAngle, boolean fill, Graphics2D g2, Color color)
  4. getDistance(int c1, Color c2)
  5. getMaxDistance(final Color first, final Color second)