ContrastChecker { public static double getConstrastRatio(final Color fgColor, final Color bgColor) { double fgLuminosity = getLuminosity(fgColor); double bgLuminosity = getLuminosity(bgColor); if (fgLuminosity > bgLuminosity) { return computeContrast(fgLuminosity, bgLuminosity); } else { return computeContrast(bgLuminosity, fgLuminosity); } } 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 testGetConstrastRatio() { System.out.println("---- getConstrastRatio --------------"); Color fgColor = new Color(70, 136, 71); Color bgColor = new Color(223, 240, 216); double result = ContrastChecker.getConstrastRatio(bgColor, fgColor); System.out.println("result :" + result); assertEquals(3.6102927, result, 0.0000001); fgColor = new Color(223, 240, 216); bgColor = new Color(70, 136, 71); result = ContrastChecker.getConstrastRatio(bgColor, fgColor); System.out.println("result :" + result); assertEquals(3.6102927, result, 0.0000001); bgColor = new Color(255, 255, 255); fgColor = new Color(0, 0, 0); result = ContrastChecker.getConstrastRatio(fgColor, bgColor); System.out.println("result :" + result); assertEquals(21.0000000, result, 0.0000001); fgColor = new Color(255, 53, 255); bgColor = new Color(18, 52, 95); result = ContrastChecker.getConstrastRatio(fgColor, bgColor); System.out.println("result :" + result); assertEquals(4.2833245, result, 0.0000001); bgColor = new Color(255, 53, 255); fgColor = new Color(18, 52, 95); result = ContrastChecker.getConstrastRatio(fgColor, bgColor); System.out.println("result :" + result); assertEquals(4.2833245, result, 0.0000001); bgColor = new Color(255, 255, 255); fgColor = new Color(255, 255, 255); result = ContrastChecker.getConstrastRatio(fgColor, bgColor); System.out.println("result :" + result); assertEquals(1.0000000, result, 0.0000001); bgColor = new Color(53, 53, 53); fgColor = new Color(53, 53, 53); result = ContrastChecker.getConstrastRatio(fgColor, bgColor); System.out.println("result :" + result); assertEquals(1.0000000, result, 0.0000001); }