def test_propertyies(self): yellow_rgb = (255, 255, 0) yellow_hex = '#ffff00' yellow_short_hex = '#ff0' yellow_name = 'yellow' yellow_hsl = (60, 100, 50) unnamed_rgb = (123, 234, 123) unnamed_hex = '#7bea7b' yellow = Color(yellow_rgb, yellow_name) self.assertEqual(yellow.rgb, yellow_rgb) self.assertEqual(yellow.hex, yellow_hex) self.assertEqual(yellow.name, yellow_name) self.assertAlmostEqual(yellow.relative_luminance, 0.9278) yellow = Color(yellow_hex, yellow_name) yellow_short = Color(yellow_short_hex, yellow_name) self.assertEqual(yellow.rgb, yellow_rgb) self.assertEqual(yellow.hex, yellow_hex) self.assertAlmostEqual(yellow.relative_luminance, 0.9278) self.assertEqual(yellow_short.rgb, yellow_rgb) self.assertEqual(yellow_short.hex, yellow_hex) self.assertAlmostEqual(yellow_short.relative_luminance, 0.9278) yellow = Color(yellow_rgb) self.assertEqual(yellow.rgb, yellow_rgb) self.assertEqual(yellow.hex, yellow_hex) self.assertEqual(yellow.name, yellow_name) tmp_color = Color(unnamed_rgb) self.assertEqual(tmp_color.rgb, unnamed_rgb) self.assertEqual(tmp_color.hex, unnamed_hex) self.assertEqual(tmp_color.name, unnamed_hex)
def test_with_invert(self): yellow = Color((255, 255, 0)) orange = Color((255, 165, 0)) blue = Color((0, 0, 255)) royalblue = Color((65,105, 225)) gray = Color((128, 128, 128)) self.assertEqual(yellow.with_invert(0).rgb, yellow.rgb) self.assertEqual(orange.with_invert(0).rgb, orange.rgb) self.assertEqual(blue.with_invert(0).rgb, blue.rgb) self.assertEqual(royalblue.with_invert(0).rgb, royalblue.rgb) self.assertEqual(gray.with_invert(0).rgb, gray.rgb) self.assertEqual(yellow.with_invert().rgb, blue.rgb) self.assertEqual(yellow.with_invert(100).rgb, blue.rgb) self.assertEqual(blue.with_invert(100).rgb, yellow.rgb) self.assertEqual(orange.with_invert(100).rgb, (0, 90, 255)) self.assertEqual(royalblue.with_invert(100).rgb, (190, 150, 30)) self.assertEqual(yellow.with_invert(50).rgb, gray.rgb) self.assertEqual(orange.with_invert(50).rgb, gray.rgb) self.assertEqual(blue.with_invert(50).rgb, gray.rgb) self.assertEqual(royalblue.with_invert(50).rgb, gray.rgb) self.assertEqual(gray.with_invert(50).rgb, gray.rgb)
def test_has_higher_luminance(self): yellow = Color((255, 255, 0)) orange = Color((255, 165, 0)) self.assertTrue(yellow.has_higher_luminance(orange)) self.assertFalse(orange.has_higher_luminance(yellow)) self.assertFalse(orange.has_higher_luminance(orange))
def test_contrast_level(self): white = Color((255, 255, 255)) black = Color((0, 0, 0)) orange = Color((255, 165, 0)) royalblue = Color((65,105, 225)) steelblue = Color((70, 130, 180)) self.assertEqual(white.contrast_level(black), 'AAA') self.assertEqual(royalblue.contrast_level(white), 'AA') self.assertEqual(steelblue.contrast_level(white), 'A') self.assertEqual(orange.contrast_level(white), '-')
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_with_saturate(self): red = Color((255, 0, 0)) orange = Color((255, 165, 0)) yellow = Color((255, 255, 0)) blue = Color((0, 0, 255)) self.assertEqual(orange.with_saturate(100).rgb, orange.rgb) self.assertEqual(yellow.with_saturate(100).rgb, yellow.rgb) self.assertEqual(blue.with_saturate(100).rgb, blue.rgb) self.assertEqual(orange.with_saturate(0).rgb, (172, 172, 172)) self.assertEqual(yellow.with_saturate(0).rgb, (237, 237, 237)) self.assertEqual(blue.with_saturate(0).rgb, (18, 18, 18)) self.assertEqual(orange.with_saturate(2357).rgb, red.rgb) self.assertEqual(orange.with_saturate(3000).rgb, red.rgb)
def test_hsl(self): yellow_rgb = (255, 255, 0) yellow_hsl = (60, 100, 50) yellow = Color(yellow_rgb) for i, c in enumerate(yellow_hsl): self.assertAlmostEqual(yellow.hsl[i], c)
def test_with_contrast(self): yellow = Color((255, 255, 0)) orange = Color((255, 165, 0)) lime = Color((0, 255, 0)) blue = Color((0, 0, 255)) white = Color((255, 255, 255)) black = Color((0, 0, 0)) neutral_gray = Color((118, 118, 118)) gray_rgb = (128, 128, 128) self.assertEqual(yellow.with_contrast(100).rgb, yellow.rgb) self.assertEqual(orange.with_contrast(100).rgb, orange.rgb) self.assertEqual(lime.with_contrast(100).rgb, lime.rgb) self.assertEqual(blue.with_contrast(100).rgb, blue.rgb) self.assertEqual(yellow.with_contrast(0).rgb, gray_rgb) self.assertEqual(orange.with_contrast(0).rgb, gray_rgb) self.assertEqual(lime.with_contrast(0).rgb, gray_rgb) self.assertEqual(blue.with_contrast(0).rgb, gray_rgb) self.assertEqual(white.with_contrast(0).rgb, gray_rgb) self.assertEqual(black.with_contrast(0).rgb, gray_rgb) self.assertEqual(neutral_gray.with_contrast(0).rgb, gray_rgb) self.assertEqual(orange.with_contrast(60).rgb, (204, 150, 51)) self.assertEqual(orange.with_contrast(120).rgb, (255, 173, 0))
def test_with_grayscale(self): orange = Color((255, 165, 0)) self.assertEqual(orange.with_grayscale(0).rgb, orange.rgb) self.assertEqual(orange.with_grayscale().rgb, (172, 172, 172)) self.assertEqual(orange.with_grayscale(100).rgb, (172, 172, 172)) self.assertEqual(orange.with_grayscale(50).rgb, (214,169, 86))
def test_with_hue_rotate(self): yellow = Color((255, 255, 0)) orange = Color((255, 165, 0)) blue = Color((0, 0, 255)) self.assertEqual(yellow.with_hue_rotate(0).rgb, yellow.rgb) self.assertEqual(orange.with_hue_rotate(0).rgb, orange.rgb) self.assertEqual(blue.with_hue_rotate(0).rgb, blue.rgb) self.assertEqual(yellow.with_hue_rotate(360).rgb, yellow.rgb) self.assertEqual(orange.with_hue_rotate(360).rgb, orange.rgb) self.assertEqual(blue.with_hue_rotate(360).rgb, blue.rgb) self.assertEqual(yellow.with_hue_rotate(180).rgb, (218, 218, 255)) self.assertEqual(orange.with_hue_rotate(180).rgb, (90, 180, 255)) self.assertEqual(blue.with_hue_rotate(180).rgb, (37, 37, 0)) self.assertEqual(yellow.with_hue_rotate(90).rgb, (0, 255, 218)) self.assertEqual(orange.with_hue_rotate(90).rgb, (0, 232, 90)) self.assertEqual(blue.with_hue_rotate(90).rgb, (255, 0, 37))
def test_calc_upper_ratio_limit(self): color = Color.from_name('black') self.assertEqual(brightness.calc_upper_ratio_limit(color.rgb), 100) color = color.from_name('orange') self.assertEqual(brightness.calc_upper_ratio_limit(color.rgb), 155) color = color.from_name('blueviolet') self.assertEqual(brightness.calc_upper_ratio_limit(color.rgb), 594) color = Color((0, 180, 0)) self.assertEqual(brightness.calc_upper_ratio_limit(color.rgb), 142)
def test_is_same_color(self): yellow_rgb = (255, 255, 0) yellow_hex = '#ffff00' yellow_short_hex = '#ff0' white_rgb = (255, 255, 255) yellow = Color(yellow_rgb, 'yellow') yellow2 = Color(yellow_rgb, 'yellow2') white = Color(white_rgb) self.assertEqual(yellow.hex, yellow2.hex) self.assertTrue(yellow.is_same_color(yellow2)) self.assertNotEqual(yellow.hex, white.hex) self.assertFalse(yellow.is_same_color(white)) self.assertTrue(yellow.is_same_color(yellow_hex)) self.assertTrue(yellow.is_same_color(yellow_short_hex)) self.assertFalse(white.is_same_color(yellow_short_hex)) self.assertFalse(white.is_same_color(yellow_short_hex)) self.assertTrue(yellow.is_same_color(yellow_rgb)) self.assertFalse(white.is_same_color(yellow_rgb))
def test_has_sufficient_contrast(self): black = Color((0, 0, 0)) white = Color((255, 255, 255)) orange = Color((255, 165, 0)) blueviolet = Color((138, 43, 226)) self.assertTrue(black.has_sufficient_contrast(white)) self.assertTrue(black.has_sufficient_contrast(white, 'A')) self.assertTrue(black.has_sufficient_contrast(white, 'AA')) self.assertTrue(black.has_sufficient_contrast(white, 'AAA')) self.assertFalse(orange.has_sufficient_contrast(white)) self.assertFalse(orange.has_sufficient_contrast(white, 'A')) self.assertFalse(orange.has_sufficient_contrast(white, 'AA')) self.assertFalse(orange.has_sufficient_contrast(white, 'AAA')) self.assertTrue(orange.has_sufficient_contrast(blueviolet, 'A')) self.assertFalse(orange.has_sufficient_contrast(blueviolet)) self.assertFalse(orange.has_sufficient_contrast(blueviolet, 'AA')) self.assertFalse(orange.has_sufficient_contrast(blueviolet, 'AAA')) self.assertTrue(white.has_sufficient_contrast(blueviolet)) self.assertTrue(white.has_sufficient_contrast(blueviolet, 'AA')) self.assertFalse(white.has_sufficient_contrast(blueviolet, 'AAA'))
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_compose_key_function(self): hsl = Color.from_hsl((20, 80, 50)) rgb = Color((10, 165, 70)) hsl_func = sorter.compile_color_sort_key_function('lHs') rgb_func = sorter.compile_color_sort_key_function('bRG') key_func = sorter.compose_key_function(hsl_func) for k, h in zip(key_func(hsl), (50, -20, 80)): self.assertAlmostEqual(k, h, 0) key_func = sorter.compose_key_function(hsl_func, operator.itemgetter(0)) for k, h in zip(key_func([hsl]), (50, -20, 80)): self.assertAlmostEqual(k, h, 0) key_func = sorter.compose_key_function(rgb_func) for k, h in zip(key_func(rgb), (70, -10, -165)): self.assertAlmostEqual(k, h, 0) key_func = sorter.compose_key_function(rgb_func, operator.itemgetter(0)) for k, h in zip(key_func([rgb]), (70, -10, -165)): self.assertAlmostEqual(k, h, 0)
def test_guess(self): color = Color((255, 255, 0)) rgb = (255, 255, 0) hsl = (60, 100, 50) hex = '#ffff00' colors = [color, rgb, hsl, hex] self.assertEqual(key_types.guess(color), key_types.COLOR) self.assertEqual(key_types.guess(colors, operator.itemgetter(0)), key_types.COLOR) self.assertEqual(key_types.guess(rgb), key_types.COMPONENTS) self.assertEqual(key_types.guess(colors, operator.itemgetter(1)), key_types.COMPONENTS) self.assertEqual(key_types.guess(hsl), key_types.COMPONENTS) self.assertEqual(key_types.guess(colors, operator.itemgetter(2)), key_types.COMPONENTS) self.assertEqual(key_types.guess(hex), key_types.HEX) self.assertEqual(key_types.guess(colors, operator.itemgetter(3)), key_types.HEX)
def test_compile_color_sort_key_function(self): hsl = Color.from_hsl((20, 80, 50)) rgb = Color((10, 165, 70)) key_func = sorter.compile_color_sort_key_function('hsl') for k, h in zip(key_func(hsl), (20, 80, 50)): self.assertAlmostEqual(k, h, 0) key_func = sorter.compile_color_sort_key_function('HSL') for k, h in zip(key_func(hsl), (-20, -80, -50)): self.assertAlmostEqual(k, h, 0) key_func = sorter.compile_color_sort_key_function('lHs') for k, h in zip(key_func(hsl), (50, -20, 80)): self.assertAlmostEqual(k, h, 0) key_func = sorter.compile_color_sort_key_function('rgb') for k, h in zip(key_func(rgb), (10, 165, 70)): self.assertEqual(k, h, 0) key_func = sorter.compile_color_sort_key_function('bRG') for k, h in zip(key_func(rgb), (70, -10, -165)): self.assertEqual(k, h, 0)
def test_with_brightness(self): yellow = Color((255, 255, 0)) orange = Color((255, 165, 0)) lime = Color((0, 255, 0)) blue = Color((0, 0, 255)) white = Color((255, 255, 255)) black = Color((0, 0, 0)) self.assertEqual(yellow.with_brightness(100).rgb, yellow.rgb) self.assertEqual(orange.with_brightness(100).rgb, orange.rgb) self.assertEqual(lime.with_brightness(100).rgb, lime.rgb) self.assertEqual(blue.with_brightness(100).rgb, blue.rgb) self.assertEqual(yellow.with_brightness(0).rgb, black.rgb) self.assertEqual(orange.with_brightness(0).rgb, black.rgb) self.assertEqual(lime.with_brightness(0).rgb, black.rgb) self.assertEqual(blue.with_brightness(0).rgb, black.rgb) self.assertEqual(white.with_brightness(120).rgb, white.rgb) self.assertEqual(yellow.with_brightness(120).rgb, yellow.rgb)
def test_key_function_for_color(self): rgb = Color((255, 165, 128)) key_func = sorter.compile_sort_key_function('bRg', sorter.key_types.COLOR) self.assertEqual(key_func(rgb), (128, -255, 165))
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)
def test_rgb_code(self): yellow = Color((255, 255, 0)) yellow_rgb_code = 'rgb(255,255,0)' self.assertEqual(yellow.rgb_code, yellow_rgb_code)
def test_find(self): black = Color.from_name('black') white = Color.from_name('white') brown = Color.from_name('brown') 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') fixed_color = orange new_rgb = brightness.find(fixed_color.rgb, fixed_color.rgb) new_color = Color(new_rgb) new_contrast_ratio = fixed_color.contrast_ratio_against(new_color) self.assertLess(fixed_color.contrast_ratio_against(fixed_color), 4.5) self.assertGreater(new_contrast_ratio, 4.5) self.assertAlmostEqual(new_contrast_ratio, 4.5, 1) self.assertEqual(new_color.hex, '#674200') fixed_color = orange new_rgb = brightness.find(fixed_color.rgb, blueviolet.rgb) new_color = Color(new_rgb) new_contrast_ratio = fixed_color.contrast_ratio_against(new_color) self.assertGreater(new_contrast_ratio, 4.5) self.assertAlmostEqual(new_contrast_ratio, 4.5, 1) self.assertEqual(new_color.hex, '#6720a9') fixed_color = blue new_rgb = brightness.find(fixed_color.rgb, orange.rgb) new_color = Color(new_rgb) new_contrast_ratio = fixed_color.contrast_ratio_against(new_color) self.assertGreater(new_contrast_ratio, 4.5) self.assertAlmostEqual(new_contrast_ratio, 4.5, 1) self.assertEqual(new_color.hex, '#ffaa00') fixed_color = blueviolet new_rgb = brightness.find(fixed_color.rgb, orange.rgb) new_color = Color(new_rgb) new_contrast_ratio = fixed_color.contrast_ratio_against(new_color) self.assertGreater(new_contrast_ratio, 4.5) self.assertAlmostEqual(new_contrast_ratio, 4.5, 1) self.assertEqual(new_color.hex, '#ffe000') fixed_color = brown new_rgb = brightness.find(fixed_color.rgb, fixed_color.rgb) new_color = Color(new_rgb) new_contrast_ratio = fixed_color.contrast_ratio_against(new_color) self.assertEqual(brown.hex, '#a52a2a') self.assertGreater(new_contrast_ratio, 4.5) self.assertAlmostEqual(new_contrast_ratio, 4.5, 1) self.assertEqual(new_color.hex, '#ffbebe') new_rgb = brightness.find(white.rgb, darkgreen.rgb) new_color = Color(new_rgb) new_contrast_ratio = white.contrast_ratio_against(new_color) self.assertGreater(new_contrast_ratio, 4.5) self.assertAlmostEqual(new_contrast_ratio, 4.5, 1) new_rgb = brightness.find(white.rgb, darkgreen.rgb, 'AAA') new_color = Color(new_rgb) new_contrast_ratio = white.contrast_ratio_against(new_color) self.assertGreater(new_contrast_ratio, 7) self.assertAlmostEqual(new_contrast_ratio, 7, 1) new_rgb = brightness.find(green.rgb, blue.rgb) new_color = Color(new_rgb) self.assertTrue(new_color.is_same_color(black)) self.assertTrue(mintcream.has_higher_luminance(yellow)) new_color = mintcream.with_brightness(105) self.assertEqual(brightness.calc_upper_ratio_limit(mintcream.rgb), 105) self.assertTrue(new_color.is_same_color(white)) new_rgb = brightness.find(yellow.rgb, mintcream.rgb, 'A') new_color = Color(new_rgb) self.assertTrue(new_color.is_same_color(white)) new_rgb = brightness.find(yellow.rgb, mintcream.rgb, 'AA') new_color = Color(new_rgb) self.assertTrue(new_color.is_same_color(white)) new_rgb = brightness.find(yellow.rgb, mintcream.rgb, 'AAA') self.assertTrue(new_color.is_same_color(white))
def test_common_name(self): yellow = Color('#ff0') self.assertEqual(yellow.common_name, 'yellow') unnamed_hex = '#123456' self.assertEqual(Color(unnamed_hex).common_name, unnamed_hex)
def test_str(self): yellow = Color((255, 255, 0), 'yellow') yellow_rgb = '#ffff00' self.assertEqual(str(yellow), yellow_rgb)
def test_has_max_contrast(self): self.assertTrue(Color((255, 255, 0)).has_max_contrast()) self.assertFalse(Color((255, 165, 0)).has_max_contrast())
def test_has_min_contrast(self): gray = Color((128, 128, 128)) orange = Color((255, 165, 0)) self.assertTrue(gray.has_min_contrast()) self.assertFalse(orange.has_min_contrast())
def test_is_light_color(self): self.assertTrue(Color((118, 118, 118)).is_light_color()) self.assertFalse(Color((117, 117, 117)).is_light_color())