Java Color Distance colorDistance(final float[] lab1, final float[] lab2)

Here you can find the source of colorDistance(final float[] lab1, final float[] lab2)

Description

Computes the distance E (CIE 1994) between two colors in LAB color space.

License

Open Source License

Parameter

Parameter Description
lab1 The first LAB color.
lab2 The second LAB color.

Return

The CIE94 distance between the two supplied colors.

Declaration

public static float colorDistance(final float[] lab1, final float[] lab2) 

Method Source Code

//package com.java2s;
/*//from w  w  w.ja v  a  2 s  .c o m
 *    Geotoolkit.org - An Open Source Java GIS Toolkit
 *    http://www.geotoolkit.org
 *
 *    (C) 2001-2012, Open Source Geospatial Foundation (OSGeo)
 *    (C) 2009-2012, Geomatys
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library 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
 *    Lesser General Public License for more details.
 */

public class Main {
    /**
     * Computes the distance E (CIE 1994) between two colors in LAB color space.
     * <p>
     * Reference: http://www.brucelindbloom.com/index.html?ColorDifferenceCalc.html
     *
     * @param  lab1 The first LAB color.
     * @param  lab2 The second LAB color.
     * @return The CIE94 distance between the two supplied colors.
     */
    public static float colorDistance(final float[] lab1, final float[] lab2) {
        double sum;
        if (false) {
            // Computes distance using CIE94 formula.
            // NOTE: this formula sometime fails because of negative
            //       value in the first Math.sqrt(...) expression.
            final double dL = (double) lab1[0] - lab2[0];
            final double da = (double) lab1[1] - lab2[1];
            final double db = (double) lab1[2] - lab2[2];
            final double C1 = Math.hypot(lab1[1], lab1[2]);
            final double C2 = Math.hypot(lab2[1], lab2[2]);
            final double dC = C1 - C2;
            final double dH = Math.sqrt(da * da + db * db - dC * dC);
            final double sL = dL / 2;
            final double sC = dC / (1 + 0.048 * C1);
            final double sH = dH / (1 + 0.014 * C1);
            sum = sL * sL + sC * sC + sH * sH;
        } else {
            // Computes distance using delta E formula.
            sum = 0;
            for (int i = Math.min(lab1.length, lab2.length); --i >= 0;) {
                final double delta = lab1[i] - lab2[i];
                sum += delta * delta;
            }
        }
        return (float) Math.sqrt(sum);
    }
}

Related

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