Get the distance between two colors - Android Graphics

Android examples for Graphics:Color

Description

Get the distance between two colors

Demo Code

/*******************************************************************************
 * Copyright (c) 2011 MadRobot.//from   ww  w.  ja va  2s.c  o m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/
//package com.java2s;

public class Main {
    /**
     * Get the distance between two colors
     * 
     * @param r1
     *            Red component of the first color
     * @param g1
     *            Green component of the first color
     * @param b1
     *            Blue component of the first color
     * @param r2
     *            Red component of the second color
     * @param g2
     *            Green component of the second color
     * @param b2
     *            Blue component of the second color
     * @return Distance bwetween colors
     */
    public static double getDistance(int r1, int g1, int b1, int r2,
            int g2, int b2) {
        double a = r2 - r1;
        double b = g2 - g1;
        double c = b2 - b1;

        return Math.sqrt(a * a + b * b + c * c);
    }
}

Related Tutorials