Java RGB Color Create rgbDistance(int color1, int color2)

Here you can find the source of rgbDistance(int color1, int color2)

Description

Get the Euclidean distance between two colors.

License

Mozilla Public License

Parameter

Parameter Description
color1 a parameter
color2 a parameter

Declaration

private static double rgbDistance(int color1, int color2) 

Method Source Code

//package com.java2s;
/*  //from  w  ww .  ja v a2s  . c om
 * SMART FP7 - Search engine for MultimediA enviRonment generated contenT
 * Webpage: http://smartfp7.eu
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 
 * 
 * The Original Code is Copyright (c) 2012-2013 the University of Glasgow
 * All Rights Reserved
 * 
 * Contributor(s):
 *  Dyaa Albakour <dyaa.albakour@glasgow.ac.uk>
 */

public class Main {
    /**
     * Get the Euclidean distance between two colors.
     * 
     * @param color1
     * @param color2
     * @return
     */
    private static double rgbDistance(int color1, int color2) {
        int[] c1, c2;
        c1 = decode(color1);
        c2 = decode(color2);

        return Math.pow(c1[0] - c2[0], 2) + Math.pow(c1[1] - c2[1], 2) + Math.pow(c1[2] - c2[2], 2);
    }

    /**
     *  Decode a color into its RGB components. 
    * @param color
    * @return
    */
    public static int[] decode(int color) {

        int r = color / (256 * 256);
        int g = color / (256);
        int b = color % 256;
        return new int[] { r, g, b };
    }
}

Related

  1. RGBAequals(float[] rgba1, float[] rgba2, float eps)
  2. RGBAFromHEX(String stringValue)
  3. RGBAFromHSLA(float h, float s, float l, float a)
  4. rgbClamp(float c)
  5. rgbComponents(int argb)
  6. rgbFromColorInt(int c)
  7. rgbFromGrayscale(int grayscale)
  8. toRGB(byte r, byte g, byte b, byte a)
  9. toRGB(final int r, final int g, final int b, final int a)