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

Declaration

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

Method Source Code

//package com.java2s;
/*/*from   w w  w .  j a  v a 2s.c o  m*/
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 * 
 *    (C) 2001-2008, Open Source Geospatial Foundation (OSGeo)
 *
 *    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.
     * Reference: http://www.brucelindbloom.com/index.html?ColorDifferenceCalc.html
     */
    public static float colorDistance(final float[] lab1, final float[] lab2) {
        //        if (false) {
        //            // Compute 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);
        //            return (float)Math.sqrt(sL*sL + sC*sC + sH*sH);
        //        } else {
        // Compute distance using delta E formula.
        double 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. 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 double r1, final double g1, final double b1, final double r2, final double g2, final double b2)
  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)
  9. distance(Color c1, Color c2)