MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / from_string

Method from_string

py/selenium/webdriver/support/color.py:60–99  ·  view source on GitHub ↗
(cls, str_: str)

Source from the content-addressed store, hash-verified

58
59 @classmethod
60 def from_string(cls, str_: str) -> Color:
61 import re
62
63 class Matcher:
64 match_obj: Match[str] | None
65
66 def __init__(self) -> None:
67 self.match_obj = None
68
69 def match(self, pattern: str, str_: str) -> Match[str] | None:
70 self.match_obj = re.match(pattern, str_)
71 return self.match_obj
72
73 @property
74 def groups(self) -> Sequence[str]:
75 return () if not self.match_obj else self.match_obj.groups()
76
77 m = Matcher()
78
79 if m.match(RGB_PATTERN, str_):
80 return cls(*m.groups)
81 if m.match(RGB_PCT_PATTERN, str_):
82 rgb = tuple(float(each) / 100 * 255 for each in m.groups)
83 return cls(*rgb)
84 if m.match(RGBA_PATTERN, str_):
85 return cls(*m.groups)
86 if m.match(RGBA_PCT_PATTERN, str_):
87 rgba = tuple([float(each) / 100 * 255 for each in m.groups[:3]] + [m.groups[3]])
88 return cls(*rgba)
89 if m.match(HEX_PATTERN, str_):
90 rgb = tuple(int(each, 16) for each in m.groups)
91 return cls(*rgb)
92 if m.match(HEX3_PATTERN, str_):
93 rgb = tuple(int(each * 2, 16) for each in m.groups)
94 return cls(*rgb)
95 if m.match(HSL_PATTERN, str_) or m.match(HSLA_PATTERN, str_):
96 return cls._from_hsl(*m.groups)
97 if str_.upper() in Colors:
98 return Colors[str_.upper()]
99 raise ValueError(f"Could not convert {str_} into color")
100
101 @classmethod
102 def _from_hsl(cls, h: ParseableFloat, s: ParseableFloat, light: ParseableFloat, a: ParseableFloat = 1) -> Color:

Callers 15

test_rgb_to_rgbFunction · 0.45
test_rgb_to_rgbaFunction · 0.45
test_rgb_pct_to_rgbaFunction · 0.45
test_rgba_to_rgbaFunction · 0.45
test_rgba_pct_to_rgbaFunction · 0.45
test_hex_to_hexFunction · 0.45
test_hex_to_rgbFunction · 0.45
test_hex_to_rgbaFunction · 0.45

Calls 3

matchMethod · 0.95
MatcherClass · 0.85
_from_hslMethod · 0.80

Tested by 15

test_rgb_to_rgbFunction · 0.36
test_rgb_to_rgbaFunction · 0.36
test_rgb_pct_to_rgbaFunction · 0.36
test_rgba_to_rgbaFunction · 0.36
test_rgba_pct_to_rgbaFunction · 0.36
test_hex_to_hexFunction · 0.36
test_hex_to_rgbFunction · 0.36
test_hex_to_rgbaFunction · 0.36