def test_rgba(self): self.assert_nvector_equal( parse_color("rgba(12, 34, 56, 0.7)"), NVector(12/0xff, 34/0xff, 56/0xff, 0.7) ); self.assert_nvector_equal( parse_color("rgba(12%, 34%, 56%, 0.7)"), NVector(0.12, 0.34, 0.56, 0.7) );
def test_current(self): self.assert_nvector_equal( parse_color("currentColor", NVector(0.1, 0.2, 0.3, 0.4)), NVector(0.1, 0.2, 0.3, 0.4) ); self.assert_nvector_equal( parse_color("inherit", NVector(0.1, 0.2, 0.3, 0.4)), NVector(0.1, 0.2, 0.3, 0.4) );
def test_hex_short(self): self.assert_nvector_equal( parse_color("#abc"), NVector(0xa/0xf, 0xb/0xf, 0xc/0xf, 1) ) self.assert_nvector_equal( parse_color("#abc"), NVector(0xa/0xf, 0xb/0xf, 0xc/0xf, 1) )
def test_hex_long(self): self.assert_nvector_equal( parse_color("#abcdef"), NVector(0xab/0xff, 0xcd/0xff, 0xef/0xff, 1) ) self.assert_nvector_equal( parse_color("#ABCDEF"), NVector(0xab/0xff, 0xcd/0xff, 0xef/0xff, 1) )
def test_named(self): self.assert_nvector_equal( parse_color("transparent"), NVector(0, 0, 0, 0) ); self.assert_nvector_equal( parse_color("red"), NVector(1, 0, 0, 1) ); self.assert_nvector_equal( parse_color("lime"), NVector(0, 1, 0, 1) ); self.assert_nvector_equal( parse_color("blue"), NVector(0, 0, 1, 1) );
def __init__(self, name): self.name = name rgb = Color(*parse_color(name)[:3]) xyz = rgb.converted(ColorMode.XYZ) self.representations = { "RGB": rgb, "HSV": rgb.converted(ColorMode.HSV), "XYZ": xyz, "LAB": xyz.converted(ColorMode.LAB), "LUV": xyz.converted(ColorMode.LUV), "LCH_uv": xyz.converted(ColorMode.LCH_uv), }
def test_hsla(self): self.assert_nvector_equal( parse_color("hsla(0, 100%, 50%, 0.7)"), NVector(1, 0, 0, 0.7) ); self.assert_nvector_equal( parse_color("hsla(60, 100%, 50%, 0.7)"), NVector(1, 1, 0, 0.7) ); self.assert_nvector_equal( parse_color("hsla(120, 100%, 50%, 0.7)"), NVector(0, 1, 0, 0.7) ); self.assert_nvector_equal( parse_color("hsla(180, 100%, 50%, 0.7)"), NVector(0, 1, 1, 0.7) ); self.assert_nvector_equal( parse_color("hsla(240, 100%, 50%, 0.7)"), NVector(0, 0, 1, 0.7) ); self.assert_nvector_equal( parse_color("hsla(300, 100%, 50%, 0.7)"), NVector(1, 0, 1, 0.7) ); self.assert_nvector_equal( parse_color("hsla(360, 100%, 50%, 0.7)"), NVector(1, 0, 0, 0.7) ); self.assert_nvector_equal( parse_color("hsla(120, 100%, 0%, 0.7)"), NVector(0, 0, 0, 0.7) ); self.assert_nvector_equal( parse_color("hsla(120, 100%, 100%, 0.7)"), NVector(1, 1, 1, 0.7) ); self.assert_nvector_equal( parse_color("hsla(120, 100%, 25%, 0.7)"), NVector(0, 0.5, 0, 0.7) ); self.assert_nvector_equal( parse_color("hsla(120, 75%, 75%, 0.7)"), NVector(0.5625, 0.9375, 0.5625, 0.7) );
def _parse_color(color): # Inkscape colors if re.match(r"^[0-9a-fA-F]{8}$", color): return NVector(*(int(color[i:i + 2], 16) / 0xff for i in range(0, 8, 2))) return parse_color(color)