{"record":{"id":"559f8cfdfc2c1920","repo":"SeleniumHQ/selenium","slug":"could-not-convert-str-into-color","errorCode":null,"errorMessage":"Could not convert {str_} into color","messagePattern":"Could not convert (.+?) into color","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py/selenium/webdriver/support/color.py","lineNumber":99,"sourceCode":"        if m.match(RGB_PCT_PATTERN, str_):\n            rgb = tuple(float(each) / 100 * 255 for each in m.groups)\n            return cls(*rgb)\n        if m.match(RGBA_PATTERN, str_):\n            return cls(*m.groups)\n        if m.match(RGBA_PCT_PATTERN, str_):\n            rgba = tuple([float(each) / 100 * 255 for each in m.groups[:3]] + [m.groups[3]])\n            return cls(*rgba)\n        if m.match(HEX_PATTERN, str_):\n            rgb = tuple(int(each, 16) for each in m.groups)\n            return cls(*rgb)\n        if m.match(HEX3_PATTERN, str_):\n            rgb = tuple(int(each * 2, 16) for each in m.groups)\n            return cls(*rgb)\n        if m.match(HSL_PATTERN, str_) or m.match(HSLA_PATTERN, str_):\n            return cls._from_hsl(*m.groups)\n        if str_.upper() in Colors:\n            return Colors[str_.upper()]\n        raise ValueError(f\"Could not convert {str_} into color\")\n\n    @classmethod\n    def _from_hsl(cls, h: ParseableFloat, s: ParseableFloat, light: ParseableFloat, a: ParseableFloat = 1) -> Color:\n        h = float(h) / 360\n        s = float(s) / 100\n        _l = float(light) / 100\n\n        if s == 0:\n            r = _l\n            g = r\n            b = r\n        else:\n            luminocity2 = _l * (1 + s) if _l < 0.5 else _l + s - _l * s\n            luminocity1 = 2 * _l - luminocity2\n\n            def hue_to_rgb(lum1: float, lum2: float, hue: float) -> float:\n                if hue < 0.0:\n                    hue += 1","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/py/selenium/webdriver/support/color.py#L81-L117","documentation":"Color.from_string tries the rgb/rgba/rgb-percent/rgba-percent/hex/hex3/hsl/hsla regexes and the named-color lookup; if none match it raises ValueError naming the offending string. Note the hex regexes are not anchored at end-of-string, so partial/odd hex inputs may behave unexpectedly.","triggerScenarios":"Color.from_string('reed'), Color.from_string('123'), Color.from_string('#GGGGGG'), or strings with stray whitespace/units/quotes that defeat every pattern.","commonSituations":"Reading color from element.value_of_css_property('color'/'background-color') on browsers that return an unexpected format, or processing user-typed color strings.","solutions":["Trim and validate the string against a known CSS color format before parsing.","Wrap the call in try/except and fall back to a default color.","If the format is known, use the matching constructor directly."],"exampleFix":"# before\nc = Color.from_string(user_input)   # may be 'reed'\n\n# after\ntry:\n    c = Color.from_string(user_input.strip())\nexcept ValueError:\n    c = Color.from_string(\"#000000\")","handlingStrategy":"try-catch","validationCode":"import re\nVALID_COLOR = re.compile(r'^(#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})|rgb\\([^)]*\\)|rgba\\([^)]*\\)|hsl\\([^)]*\\)|hsla\\([^)]*\\)|[A-Za-z]+)$')\ndef looks_like_color(s: str) -> bool:\n    return bool(VALID_COLOR.match(s.strip()))","typeGuard":"def looks_like_color(s: str) -> bool:\n    import re\n    return bool(re.match(r'^(#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})|rgba?\\([^)]*\\)|hsla?\\([^)]*\\)|[A-Za-z]+)$', s.strip()))","tryCatchPattern":"try:\n    c = Color.from_string(raw)\nexcept ValueError:\n    c = Color.from_string(DEFAULT)","preventionTips":["Strip whitespace/quotes from css-derived color strings before parsing.","Prefer hex or rgb() formats when you control the input."],"tags":["color","css","parsing"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}