Ejemplo n.º 1
0
    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_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_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))