Java Color Distance colorDistance(final double r1, final double g1, final double b1, final double r2, final double g2, final double b2)

Here you can find the source of colorDistance(final double r1, final double g1, final double b1, final double r2, final double g2, final double b2)

Description

Return the "distance" between two colors.

License

Apache License

Return

Distance between colors.

Declaration

public static double colorDistance(final double r1, final double g1, final double b1, final double r2,
        final double g2, final double b2) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from   w ww  . j a  va2  s  .c  o m
     * Return the "distance" between two colors. The rgb entries are taken
     * to be coordinates in a 3D space [0.0-1.0], and this method returnes
     * the distance between the coordinates for the first and second color.
     *
     * @return Distance between colors.
     */
    public static double colorDistance(final double r1, final double g1, final double b1, final double r2,
            final double g2, final double b2) {
        double a = r2 - r1;
        double b = g2 - g1;
        double c = b2 - b1;

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

    /**
     * Return the "distance" between two colors.
     * 
     * @param color1 First color [r,g,b].
     * @param color2 Second color [r,g,b].
     * @return Distance bwetween colors.
     */
    public static double colorDistance(final int color1, final int color2) {
        int[] rgb1 = int2rgb(color1), rgb2 = int2rgb(color2);

        return colorDistance(rgb1[0] / 256.0d, rgb1[1] / 256.0d, rgb1[2] / 256.0d, rgb2[0] / 256.0d,
                rgb2[1] / 256.0d, rgb2[2] / 256.0d);
    }

    public static int[] int2rgb(final int color) {
        return new int[] { (color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF };
    }
}

Related

  1. colorDiff(int a, int b)
  2. colorDiffAlpha(int a, int b)
  3. colordiffsq(int rgb0, int rgb1)
  4. colorDistance(double r1, double g1, double b1, double r2, double g2, double b2)
  5. colorDistance(final float[] lab1, final float[] lab2)
  6. colorDistance(final float[] lab1, final float[] lab2)
  7. colorDistance(int r1, int g1, int b1, int r2, int g2, int b2)
  8. distance(Color a, Color b)