Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    def test_hsl_colors(self):
        black = Color.from_name('black')
        white = Color.from_name('white')
        gray = Color.from_name('gray')
        red = Color.from_name('red')
        yellow = Color.from_name('yellow')

        colors = hsl_colors()
        self.assertEqual(len(colors), 361)
        self.assertTrue(colors[0].is_same_color(red))
        self.assertTrue(colors[-1].is_same_color(red))
        self.assertTrue(colors[60].is_same_color(yellow))

        colors = hsl_colors(h_interval = 15)
        self.assertEqual(len(colors), 25)
        self.assertTrue(colors[0].is_same_color(red))
        self.assertTrue(colors[-1].is_same_color(red))
        self.assertTrue(colors[4].is_same_color(yellow))

        colors = hsl_colors(l = 0)
        for c in colors:
            self.assertTrue(c.is_same_color(black))

        colors = hsl_colors(l = 100)
        for c in colors:
            self.assertTrue(c.is_same_color(white))

        colors = hsl_colors(s = 0)
        for c in colors:
            self.assertTrue(c.is_same_color(gray))
Ejemplo n.º 3
0
    def test_from_name(self):
        yellow = Color.from_name('yellow')
        self.assertTrue(isinstance(yellow, Color))
        self.assertEqual(yellow.name, 'yellow')
        self.assertEqual(yellow.hex, '#ffff00')

        yellow = Color.from_name('Yellow')
        self.assertEqual(yellow.name, 'yellow')
        self.assertEqual(yellow.hex, '#ffff00')

        self.assertIsNone(Color.from_name('kiiro'))
    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)
Ejemplo n.º 5
0
    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_default(self):
        color_names = ('red', 'yellow', 'lime', 'cyan', 'fuchsia', 'blue')
        colors = [Color.from_name(c) for c in color_names]
        red, yellow, lime, cyan, fuchsia, blue = colors
        default_order = [red, yellow, lime, cyan, blue, fuchsia]
        rgb_order = [yellow, fuchsia, red, cyan, lime, blue]

        self.assertEqual(sorter.sorted(colors), default_order)
        self.assertEqual(sorter.sorted(colors, "RGB"), rgb_order)
    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)
Ejemplo n.º 8
0
    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))
Ejemplo n.º 9
0
    def test_from_hex(self):
        yellow_normalized_hex = '#ffff00'
        yellow_name = 'yellow'
        undefined_color_hex = '#f3f2f1'
        undefined_color_name = 'undefined_color'
        new_yellow_name = 'new_yellow'

        yellow = Color.from_hex(yellow_normalized_hex)
        self.assertTrue(isinstance(yellow, Color))
        self.assertEqual(yellow.name, yellow_name)
        self.assertEqual(yellow.hex, yellow_normalized_hex)

        yellow = Color.from_hex('#FFFF00')
        self.assertTrue(isinstance(yellow, Color))
        self.assertEqual(yellow.name, yellow_name)
        self.assertEqual(yellow.hex, yellow_normalized_hex)

        yellow = Color.from_hex('#ff0')
        self.assertTrue(isinstance(yellow, Color))
        self.assertEqual(yellow.name, yellow_name)
        self.assertEqual(yellow.hex, yellow_normalized_hex)

        undefined_color = Color.from_hex(undefined_color_hex)
        self.assertTrue(isinstance(undefined_color, Color))
        self.assertEqual(undefined_color.name, undefined_color_hex)
        self.assertEqual(undefined_color.hex, undefined_color_hex)

        undefined_color = Color.from_hex(undefined_color_hex, undefined_color_name)
        self.assertEqual(undefined_color.name, undefined_color_name)

        new_yellow = Color.from_hex('#ff0', new_yellow_name)
        self.assertEqual(new_yellow.hex, yellow_normalized_hex)
        self.assertEqual(new_yellow.name, new_yellow_name)
Ejemplo n.º 10
0
    def test_from_rgb(self):
        yellow_rgb = (255, 255, 0)
        yellow_name = 'yellow'

        yellow = Color.from_rgb(yellow_rgb)
        self.assertTrue(isinstance(yellow, Color))
        self.assertEqual(yellow.rgb, yellow_rgb)
        self.assertEqual(yellow.name, yellow_name)

        new_name = 'new_yellow'
        new_yellow = Color.from_rgb(yellow_rgb, new_name)

        self.assertEqual(new_yellow.rgb, yellow_rgb)
        self.assertEqual(new_yellow.name, new_name)

        unnamed_rgb = (123, 234, 123)
        unnamed_hex = '#7bea7b'
        new_color = Color.from_rgb(unnamed_rgb)

        self.assertFalse(unnamed_hex in HEX_TO_COLOR)
        self.assertEqual(new_color.hex, unnamed_hex)
        self.assertEqual(new_color.name, unnamed_hex)
Ejemplo n.º 11
0
    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)
Ejemplo n.º 12
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))
Ejemplo n.º 13
0
    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_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)
Ejemplo n.º 15
0
    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))
Ejemplo n.º 16
0
 def test_str(self):
     yellow = Color((255, 255, 0), 'yellow')
     yellow_rgb = '#ffff00'
     self.assertEqual(str(yellow), 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)
Ejemplo n.º 18
0
    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_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))
Ejemplo n.º 20
0
    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'))
Ejemplo n.º 21
0
    def test_from_hsl(self):
        self.assertEqual(Color.from_hsl((60, 100, 50)).hex, '#ffff00')
        self.assertEqual(Color.from_hsl((60.0, 100.0, 50.0)).hex, '#ffff00')

        self.assertEqual(Color.from_hsl((30, 100, 50)).hex, '#ff8000')
        self.assertEqual(Color.from_hsl((30.0, 100.0, 50.0)).hex, '#ff8000')
Ejemplo n.º 22
0
    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)
Ejemplo n.º 23
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))
Ejemplo n.º 24
0
    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)
Ejemplo n.º 25
0
 def test_has_max_contrast(self):
     self.assertTrue(Color((255, 255, 0)).has_max_contrast())
     self.assertFalse(Color((255, 165, 0)).has_max_contrast())
Ejemplo n.º 26
0
    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))
Ejemplo n.º 27
0
    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)
Ejemplo n.º 28
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)
Ejemplo n.º 29
0
    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())
Ejemplo n.º 30
0
 def test_is_light_color(self):
     self.assertTrue(Color((118, 118, 118)).is_light_color())
     self.assertFalse(Color((117, 117, 117)).is_light_color())