Here you can find the source of colorDistance(final float[] lab1, final float[] lab2)
public static float colorDistance(final float[] lab1, final float[] lab2)
//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); // } } }