javacolorseuclidean-distance

Calculating distance between two LaB colors Java


I am working on a program where I am trying to find the euclidean distance between two LaB colors. I was wondering if they way I calculate the the distance between the two is correct . Here is what I am doing:

public static void main(String[] args) {

    double[] lab = {58.974604845047, 15.037506818771362, -64.0113115310669}; //blue
    double[] lab1 = {58.701420307159424, 14.014512300491333, -64.46481943130493};//blue

    double distance = euclideanDistance(lab, lab1);
    System.out.println("Lab: " + distance); 

}

private static double euclideanDistance(double[] lab , double []lab1){
    //L = 0 - 100 range
    //A = -86.185 - 98.254 range
    //B = 107.863 - 94.482 range
    double Lmean = (lab[0] + lab1[0]) / 2;
    double L = lab[0] - lab1[0];
    double A = lab[1] - lab1[1];
    double B = lab[2] - lab1[2];
    double weightL = 2 + Lmean /100;
    double weightA = 4.0;
    double weightB = 2 + (107.863 - Lmean) / 100;

    return Math.sqrt(weightL * L * L + weightA * A * A + weightB * B * B);  
}

Solution

  • So for anyone curious to see if there is a slight difference between euclidean distance between two Lab Values there is not. As stated by wiki

    ... the relative perceptual differences between any two colors in Lab* can be approximated by treating each color as a point in a three-dimensional space (with three components: L, a, b*) and taking the Euclidean distance between them

    The solution to this problem would be this.

    private  double euclideanDistance(double[] lab , double []lab1){
        double L = lab[0] - lab1[0];
        double A = lab[1] - lab1[1];
        double B = lab[2] - lab1[2];
        return Math.sqrt((L * L) +  (A * A) +  (B * B));    
    }