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))
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_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_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_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))
def prepare_colors(self): self.colors = [[Color.from_name(c).hex] for c in self.color_names] self.colors2 = [[Color.from_name(c).hex] for c in self.color_names2] self.key = operator.itemgetter(0)
def prepare_colors(self): self.colors = [Color.from_name(c).hex for c in self.color_names] self.colors2 = [Color.from_name(c).hex for c in self.color_names2] self.key = None