def test_percent(self): """Test that percents work properly.""" color = "color(--okhsl 20 100% 75% / 100%)" okhsl = Color(color) self.assertEqual("color(--okhsl 20 1 0.75)", okhsl.to_string()) color = "color(--okhsl 20 1 0.75 / 20%)" okhsl = Color(color) self.assertEqual("color(--okhsl 20 1 0.75 / 0.2)", okhsl.to_string())
def test_percent(self): """Test that percents work properly.""" color = "color(--din99o 20% 10 -30 / 100%)" din99o = Color(color) self.assertEqual("color(--din99o 20 10 -30)", din99o.to_string()) color = "color(--din99o 20% 10 -30 / 20%)" din99o = Color(color) self.assertEqual("color(--din99o 20 10 -30 / 0.2)", din99o.to_string())
def test_percent(self): """Test that percents work properly.""" color = "color(--lchuv 20% 10 130 / 100%)" lchuv = Color(color) self.assertEqual("color(--lchuv 20 10 130)", lchuv.to_string()) color = "color(--lchuv 20% 10 130 / 20%)" lchuv = Color(color) self.assertEqual("color(--lchuv 20 10 130 / 0.2)", lchuv.to_string())
def test_percent(self): """Test that percents work properly.""" color = "color(--hsluv 20 50% 75% / 100%)" hsluv = Color(color) self.assertEqual("color(--hsluv 20 50 75)", hsluv.to_string()) color = "color(--hsluv 20 50 75 / 20%)" hsluv = Color(color) self.assertEqual("color(--hsluv 20 50 75 / 0.2)", hsluv.to_string())
def test_percent(self): """Test that percents work properly.""" args = {} color = "lch(20% 10 130 / 100%)" lch = Color(color) self.assertEqual("lch(20% 10 130)", lch.to_string(**args)) color = "lch(20% 10 130 / 20%)" lch = Color(color) self.assertEqual("lch(20% 10 130 / 0.2)", lch.to_string(**args))
def test_percent(self): """Test that percents work properly.""" args = {} color = "oklab(20% 0.1 -0.3 / 100%)" lab = Color(color) self.assertEqual("oklab(20% 0.1 -0.3)", lab.to_string(**args)) color = "oklab(20% 0.1 -0.3 / 20%)" lab = Color(color) self.assertEqual("oklab(20% 0.1 -0.3 / 0.2)", lab.to_string(**args))
def test_no_alpha(self): """Test no alpha.""" args = {"comma": True, "alpha": False} color = "hsla(20, 100%, 75%, 0.2)" hsl = Color(color) self.assertEqual("hsl(20, 100%, 75%)", hsl.to_string(**args)) args["comma"] = False color = "hsl(20 100% 75% / 0.2)" hsl = Color(color) self.assertEqual("hsl(20 100% 75%)", hsl.to_string(**args))
def test_force_alpha(self): """Test force alpha.""" args = {"comma": True, "alpha": True} color = "hsla(20, 100%, 75%, 1)" hsl = Color(color) self.assertEqual("hsla(20, 100%, 75%, 1)", hsl.to_string(**args)) args["comma"] = False color = "hsl(20 100% 75% / 1)" hsl = Color(color) self.assertEqual("hsl(20 100% 75% / 1)", hsl.to_string(**args))
def test_space(self): """Test space input and space output format.""" args = {} color = "rgb(30 100 200)" rgb = Color(color) self.assertEqual(color, rgb.to_string(**args)) color = "rgb(30 100 200 / 1)" rgb = Color(color) self.assertEqual("rgb(30 100 200)", rgb.to_string(**args)) color = "rgb(30 100 200 / 0.2)" rgb = Color(color) self.assertEqual(color, rgb.to_string(**args))
def test_force_alpha(self): """Test force alpha.""" color = "color(--lchuv 20% 10 130 / 1)" lchuv = Color(color) self.assertEqual("color(--lchuv 20 10 130 / 1)", lchuv.to_string(alpha=True))
def test_space(self): """Test space input and space output format.""" args = {} color = "lch(20% 10 130)" lch = Color(color) self.assertEqual(color, lch.to_string(**args)) color = "lch(20% 10 130 / 1)" lch = Color(color) self.assertEqual("lch(20% 10 130)", lch.to_string(**args)) color = "lch(20% 10 130 / 0.2)" lch = Color(color) self.assertEqual(color, lch.to_string(**args))
def test_force_alpha(self): """Test force alpha.""" color = "color(--lch99o 20% 10 130 / 1)" din99o = Color(color) self.assertEqual("color(--lch99o 20 10 130 / 1)", din99o.to_string(alpha=True))
def test_no_alpha(self): """Test no alpha.""" color = "color(--lch99o 20% 10 130 / 0.2)" din99o = Color(color) self.assertEqual("color(--lch99o 20 10 130)", din99o.to_string(alpha=False))
def test_space(self): """Test space input and space output format.""" args = {} color = "hsl(20 100% 75%)" hsl = Color(color) self.assertEqual(color, hsl.to_string(**args)) color = "hsl(20 100% 75% / 1)" hsl = Color(color) self.assertEqual("hsl(20 100% 75%)", hsl.to_string(**args)) color = "hsl(20 100% 75% / 0.2)" hsl = Color(color) self.assertEqual(color, hsl.to_string(**args))
def test_space(self): """Test space input and space output format.""" args = {} color = "oklab(20% 0.1 -0.3)" lab = Color(color) self.assertEqual(color, lab.to_string(**args)) color = "oklab(20% 0.1 -0.3 / 1)" lab = Color(color) self.assertEqual("oklab(20% 0.1 -0.3)", lab.to_string(**args)) color = "oklab(20% 0.1 -0.3 / 0.2)" lab = Color(color) self.assertEqual(color, lab.to_string(**args))
def test_hex_upper(self): """Test hex input.""" args = {"hex": True, "upper": True} color = "#AA33DD" rgb = Color(color) self.assertEqual(color, rgb.to_string(**args)) color = "#AA33DDFF" rgb = Color(color) self.assertEqual("#AA33DD", rgb.to_string(**args)) color = "#AA33DD22" rgb = Color(color) self.assertEqual("#AA33DD22", rgb.to_string(**args))
def test_hex(self): """Test hex input and hex output format.""" args = {"hex": True} color = "#AA33DD" rgb = Color(color) self.assertEqual(color.lower(), rgb.to_string(**args)) color = "#AA33DDFF" rgb = Color(color) self.assertEqual("#aa33dd", rgb.to_string(**args)) color = "#AA33DD22" rgb = Color(color) self.assertEqual("#aa33dd22", rgb.to_string(**args))
def test_no_alpha(self): """Test no alpha.""" color = "color(--lchuv 20% 10 130 / 0.2)" lchuv = Color(color) self.assertEqual("color(--lchuv 20 10 130)", lchuv.to_string(alpha=False))
def test_comma(self): """Test comma input and comma output format.""" args = {"comma": True} color = "rgb(30, 100, 200)" rgb = Color(color) self.assertEqual(color, rgb.to_string(**args)) color = "rgb(30, 100, 200, 1)" rgb = Color(color) self.assertEqual("rgb(30, 100, 200)", rgb.to_string(**args)) color = "rgb(30, 100, 200, 0.2)" rgb = Color(color) self.assertEqual("rgba(30, 100, 200, 0.2)", rgb.to_string(**args))
def assert_round_trip(self, color, space): """Cycle through all the other colors and convert to them and back and check the results.""" c1 = Color(color).convert(space) for space in SPACES: # Print the color space to easily identify which color space broke. c2 = c1.convert(space) c2.convert(c1.space(), in_place=True) # Catch cases where we are really close to 360 which should wrap to 0 for c in (c1, c2): if isinstance(c._space, Cylindrical): if util.round_half_up(util.no_nan(c.hue), c.PRECISION) == 360: c.hue = 0 # Run rounded string back through parsing in case we hit something like a hue that needs normalization. str1 = Color(c1.to_string(color=True, fit=False)).to_string(color=True, fit=False) str2 = Color(c2.to_string(color=True, fit=False)).to_string(color=True, fit=False) # Print failing results for debug purposes if str1 != str2: print('----- Convert: {} <=> {} -----'.format( c1.space(), space)) print('Original: ', color.to_string(color=True, fit=False)) print(c1.space() + ': ', str1, c1.coords()) print(space + ': ', str2, c2.coords()) assert str1 == str2
def test_comma(self): """Test comma input and comma output format.""" args = {"comma": True} color = "hsl(20, 100%, 75%)" hsl = Color(color) self.assertEqual(color, hsl.to_string(**args)) color = "hsl(20, 100%, 75%, 1)" hsl = Color(color) self.assertEqual("hsl(20, 100%, 75%)", hsl.to_string(**args)) color = "hsl(20, 100%, 75%, 0.2)" hsl = Color(color) self.assertEqual("hsla(20, 100%, 75%, 0.2)", hsl.to_string(**args))
def test_no_alpha(self): """Test no alpha.""" args = {"alpha": False} color = "lch(20% 10 130 / 0.2)" lch = Color(color) self.assertEqual("lch(20% 10 130)", lch.to_string(**args))
def test_no_alpha(self): """Test no alpha.""" args = {"alpha": False} color = "color(display-p3 0.3 1 0.5 / 0.2)" p3 = Color(color) self.assertEqual("color(display-p3 0.3 1 0.5)", p3.to_string(**args))
def test_force_alpha(self): """Test force alpha.""" args = {"alpha": True} color = "color(xyz-d50 0.3 1 0.5 / 100%)" xyz = Color(color) self.assertEqual("color(xyz-d50 0.3 1 0.5 / 1)", xyz.to_string(**args))
def test_no_alpha(self): """Test no alpha.""" args = {"alpha": False} color = "color(xyz-d50 0.3 1 0.5 / 0.2)" xyz = Color(color) self.assertEqual("color(xyz-d50 0.3 1 0.5)", xyz.to_string(**args))
def test_no_alpha(self): """Test no alpha.""" args = {"alpha": False} color = "color(--okhsl 20 100% 75% / 0.2)" okhsl = Color(color) self.assertEqual("color(--okhsl 20 1 0.75)", okhsl.to_string(**args))
def test_force_alpha(self): """Test force alpha.""" args = {"alpha": True} color = "color(rec2020 0.3 1 0.5 / 100%)" rec = Color(color) self.assertEqual("color(rec2020 0.3 1 0.5 / 1)", rec.to_string(**args))
def test_no_alpha(self): """Test no alpha.""" args = {"alpha": False} color = "color(rec2020 0.3 1 0.5 / 0.2)" rec = Color(color) self.assertEqual("color(rec2020 0.3 1 0.5)", rec.to_string(**args))
def test_no_alpha(self): """Test no alpha.""" args = {"alpha": False} color = "color(--ictcp 1 0.2 -0.3 / 0.5)" ictcp = Color(color) self.assertEqual("color(--ictcp 1 0.2 -0.3)", ictcp.to_string(**args))
def test_force_alpha(self): """Test force alpha.""" args = {"alpha": True} color = "lch(20% 10 130 / 1)" lch = Color(color) self.assertEqual("lch(20% 10 130 / 1)", lch.to_string(**args))