0

I want to know the range of values that can be returned by this function:

public static double colorDistance(Color color1, Color color2) {
    double rmean = ( color1.getRed() + color2.getRed() )/2;
    int r = color1.getRed() - color2.getRed();
    int g = color1.getGreen() - color2.getGreen();
    int b = color1.getBlue() - color2.getBlue();
    double weightR = 2 + rmean/256;
    double weightG = 4.0;
    double weightB = 2 + (255-rmean)/256;
    return Math.sqrt(weightR*r*r + weightG*g*g + weightB*b*b);
}

I know the smallest is 0, but I'm unsure of the largest.

I made this java code to help test:

private static List<Color> allColors = new ArrayList<Color>();

public static void main(String[] args) {

    for(int i=0; i<=255; i++) {
        for(int j=0; j<=255; j++) {
            for(int k=0; k<=255; k++) {
                allColors.add(new Color(i,j,k));
            }
        }
    }

    double max = 0;
    for(int i=0; i<allColors.size(); i++) {
        Color c1 = allColors.get(i);
        for(int j=0; j<allColors.size(); j++) {
            Color c2 = allColors.get(j);
            System.out.print(i);
            System.out.print(" - ");
            System.out.println(j);
            max = Math.max(max, colorDistance(c1, c2));
        }
    }

    System.out.print(max);
}

But its taking too long. I tried black and white, to get 765 but according to the graph on this page http://en.wikipedia.org/wiki/Color_difference there are colors whose distance is even bigger than black and white.

Does anyone know how to get the true maximum value?

Thanks

omega
  • 40,311
  • 81
  • 251
  • 474

1 Answers1

0

Ok, my fault:

For color1=white and color2=black

double rmean = ( color1.getRed() + color2.getRed() )/2; // (255 + 0) /2=255 / 2=(int)127
int r = color1.getRed() - color2.getRed(); // (255 - 0)=255
int g = color1.getGreen() - color2.getGreen(); //(255 - 0)=255
int b = color1.getBlue() - color2.getBlue(); //(255 - 0)=255
double weightR = 2 + rmean/256; = 2 + 127/256 = cca 2.49609375
double weightG = 4.0; // = 4
double weightB = 2 + (255-rmean)/256; // 2 + (255 - 127) / 256 = 2.5
return Math.sqrt(weightR*r*r + weightG*g*g + weightB*b*b);
// sqrt(2.79609375 * 255 * 255 + 4 * 255 * 255 + 2.5 * 255 * 255) =
// sqrt(8.99609375*255*255) = sqrt(584970.99609375) = cca 764.834

so this is the highest value increasing rmean wont increase the result because there is once + rmean and once - rmean
so highest value is for black and white

maskacovnik
  • 3,080
  • 5
  • 20
  • 26