def test_contrast_ratio_against(self): color = Color((127, 127, 32)) white = Color((255, 255, 255)) expected_ratio = 4.23 ratio = color.contrast_ratio_against(white.rgb) self.assertAlmostEqual(ratio, expected_ratio, 2) ratio = color.contrast_ratio_against(white.rgb) self.assertAlmostEqual(ratio, expected_ratio, 2) ratio = color.contrast_ratio_against(white) self.assertAlmostEqual(ratio, expected_ratio, 2)
def test_find_lightness_threshold(self): yellow = Color((255, 255, 0)) orange = Color((255, 165, 0)) level = 'A' target_ratio = 3.0 new_color = yellow.find_lightness_threshold(orange, level) new_contrast_ratio = yellow.contrast_ratio_against(new_color) self.assertTrue(orange.has_higher_luminance(new_color)) self.assertGreater(new_contrast_ratio, target_ratio) self.assertAlmostEqual(new_contrast_ratio, target_ratio, 1) new_color = orange.find_lightness_threshold(orange, level) new_contrast_ratio = orange.contrast_ratio_against(new_color) self.assertTrue(orange.has_higher_luminance(new_color)) self.assertGreater(new_contrast_ratio, target_ratio) self.assertAlmostEqual(new_contrast_ratio, target_ratio, 1) level = 'AA' target_ratio = 4.5 new_color = yellow.find_lightness_threshold(orange, level) new_contrast_ratio = yellow.contrast_ratio_against(new_color) self.assertTrue(orange.has_higher_luminance(new_color)) self.assertGreater(new_contrast_ratio, target_ratio) self.assertAlmostEqual(new_contrast_ratio, target_ratio, 1) new_color = orange.find_lightness_threshold(orange, level) new_contrast_ratio = orange.contrast_ratio_against(new_color) self.assertTrue(orange.has_higher_luminance(new_color)) self.assertGreater(new_contrast_ratio, target_ratio) self.assertAlmostEqual(new_contrast_ratio, target_ratio, 1)
def test_find(self): black = Color.from_name('black') white = Color.from_name('white') orange = Color.from_name('orange') mintcream = Color.from_name('mintcream') yellow = Color.from_name('yellow') springgreen = Color.from_name('springgreen') green = Color.from_name('green') darkgreen = Color.from_name('darkgreen') blue = Color.from_name('blue') azure = Color.from_name('azure') blueviolet = Color.from_name('blueviolet') fuchsia = Color.from_name('fuchsia') new_rgb = lightness.find(fuchsia.rgb, azure.rgb, 'A') new_color = Color(new_rgb) new_contrast_ratio = new_color.contrast_ratio_against(fuchsia) self.assertTrue(azure.has_higher_luminance(fuchsia)) self.assertTrue(azure.has_higher_luminance(new_color)) self.assertEqual(new_color.hex, '#e9ffff') self.assertGreater(new_contrast_ratio, 3.0) self.assertAlmostEqual(new_contrast_ratio, 3, 1) contrast_against_white = darkgreen.contrast_ratio_against(white) contrast_against_black = darkgreen.contrast_ratio_against(black) new_rgb = lightness.find(darkgreen.rgb, darkgreen.rgb, 'A') new_color = Color(new_rgb) new_contrast_ratio = new_color.contrast_ratio_against(darkgreen) self.assertFalse(darkgreen.is_light_color()) self.assertGreater(contrast_against_white, contrast_against_black) self.assertEqual(new_color.hex, '#00c000') self.assertTrue(new_color.has_higher_luminance(darkgreen)) self.assertGreater(new_contrast_ratio, 3.0) self.assertAlmostEqual(new_contrast_ratio, 3, 1) new_rgb = lightness.find(white.rgb, orange.rgb, 'AA') new_color = Color(new_rgb) new_contrast_ratio = new_color.contrast_ratio_against(white) self.assertEqual(new_color.hex, '#a56a00') self.assertGreater(new_contrast_ratio, 4.5) self.assertAlmostEqual(new_contrast_ratio, 4.5, 1) new_rgb = lightness.find(white.rgb, green.rgb, 'AA') new_color = Color(new_rgb) new_contrast_ratio = new_color.contrast_ratio_against(white) self.assertEqual(new_color.hex, '#008a00') self.assertGreater(new_contrast_ratio, 4.5) self.assertAlmostEqual(new_contrast_ratio, 4.5, 1) new_rgb = lightness.find(blueviolet.rgb, orange.rgb, 'AA') new_color = Color(new_rgb) new_contrast_ratio = new_color.contrast_ratio_against(blueviolet) self.assertEqual(new_color.hex, '#ffdc9a') self.assertGreater(new_contrast_ratio, 4.5) self.assertAlmostEqual(new_contrast_ratio, 4.5, 1) contrast_against_white = springgreen.contrast_ratio_against(white) contrast_against_black = springgreen.contrast_ratio_against(black) new_rgb = lightness.find(springgreen.rgb, springgreen.rgb, 'AA') new_color = Color(new_rgb) new_contrast_ratio = new_color.contrast_ratio_against(springgreen) self.assertTrue(springgreen.is_light_color()) self.assertLess(contrast_against_white, contrast_against_black) self.assertEqual(new_color.hex, '#007239') self.assertFalse(new_color.has_higher_luminance(springgreen)) self.assertGreater(new_contrast_ratio, 4.5) self.assertAlmostEqual(new_contrast_ratio, 4.5, 1) new_rgb = lightness.find(orange.rgb, yellow.rgb) new_color = Color(new_rgb) self.assertTrue(new_color.is_same_color(white)) self.assertLess(new_color.contrast_ratio_against(yellow), 4.5) new_rgb = lightness.find(yellow.rgb, mintcream.rgb) new_color = Color(new_rgb) self.assertTrue(new_color.is_same_color(white)) self.assertLess(new_color.contrast_ratio_against(yellow), 4.5) new_rgb = lightness.find(white.rgb, orange.rgb, 'AAA') new_color = Color(new_rgb) new_contrast_ratio = new_color.contrast_ratio_against(white) self.assertEqual(new_color.hex, '#7b5000') self.assertGreater(new_contrast_ratio, 7.0) self.assertAlmostEqual(new_contrast_ratio, 7, 1) new_rgb = lightness.find(white.rgb, green.rgb, 'AAA') new_color = Color(new_rgb) new_contrast_ratio = new_color.contrast_ratio_against(white) self.assertEqual(new_color.hex, '#006800') self.assertGreater(new_contrast_ratio, 7.0) self.assertAlmostEqual(new_contrast_ratio, 7, 1) new_rgb = lightness.find(green.rgb, blue.rgb, 'AAA') new_color = Color(new_rgb) new_contrast_ratio = new_color.contrast_ratio_against(green) self.assertTrue(new_color.is_same_color(black)) self.assertLess(new_contrast_ratio, 7.0) new_rgb = lightness.find(white.rgb, orange.rgb, 6.5) new_color = Color(new_rgb) new_contrast_ratio = new_color.contrast_ratio_against(white) self.assertEqual(new_color.hex, '#825400') self.assertGreater(new_contrast_ratio, 6.5) self.assertAlmostEqual(new_contrast_ratio, 6.5, 1) new_rgb = lightness.find(white.rgb, green.rgb, 6.5) new_color = Color(new_rgb) new_contrast_ratio = new_color.contrast_ratio_against(white) self.assertEqual(new_color.hex, '#006e00') self.assertGreater(new_contrast_ratio, 6.5) self.assertAlmostEqual(new_contrast_ratio, 6.5, 1) new_rgb = lightness.find(green.rgb, blue.rgb, 6.5) new_color = Color(new_rgb) new_contrast_ratio = new_color.contrast_ratio_against(green) self.assertTrue(new_color.is_same_color(black)) self.assertLess(new_contrast_ratio, 6.5)