Here you can find the source of colorDistance(final float[] lab1, final float[] lab2)
Parameter | Description |
---|---|
lab1 | The first LAB color. |
lab2 | The second LAB color. |
public static float colorDistance(final float[] lab1, final float[] lab2)
//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); } }