ContrastChecker { public static double distanceColor(final Color fgColor, final Color bgColor) { int redFg = fgColor.getRed(); int redBg = bgColor.getRed(); int greenBg = bgColor.getGreen(); int greenFg = fgColor.getGreen(); int blueFg = fgColor.getBlue(); int blueBg = bgColor.getBlue(); return (Math.sqrt(Math.pow(redFg - redBg, 2) + Math.pow(greenFg - greenBg, 2) + Math.pow(blueFg - blueBg, 2))); } private  ContrastChecker(); static double distanceColor(final Color fgColor, final Color bgColor); static boolean isContrastValid(final Color fgColor, final Color bgColor, Float coefficientLevel); static double getConstrastRatio(final Color fgColor, final Color bgColor); static double getConstrastRatio5DigitRound(final Color fgColor, final Color bgColor); static double getConstrastRatio2DigitRound(final Color fgColor, final Color bgColor); static double getLuminosity(Color color);  }
@Test public void testDistanceColor() { Color colorA = new Color(70, 136, 71); Color colorB = new Color(66, 118, 69); float result = (float) ContrastChecker.distanceColor(colorA, colorB); System.out.println("DistanceColor : [#468847] and [#DFF0D8] = ["+ result +"] "); assertEquals(18.547237, result, 0.000001); result = (float) ContrastChecker.distanceColor(colorB, colorA); System.out.println("DistanceColor : [#468847] and [#DFF0D8] = ["+ result +"] "); assertEquals(18.547237, result, 0.000001); colorA = Color.WHITE; colorB = new Color(192,192,192); result = (float) ContrastChecker.distanceColor(colorB, colorA); System.out.println("DistanceColor : [WHITE] and [silver] = ["+ result +"] "); assertEquals(109.119201, result, 0.000001); colorA = Color.BLACK; colorB = Color.WHITE; result = (float) ContrastChecker.distanceColor(colorB, colorA); System.out.println("DistanceColor : [BLACK] and [WHITE] = ["+ result +"] "); assertEquals(441.672943, result, 0.000001); }