{"record":{"id":"ef856c891309e60a","repo":"SeleniumHQ/selenium","slug":"could-not-convert-str-inspect-into-color","errorCode":null,"errorMessage":"could not convert #{str.inspect} into color","messagePattern":"could not convert #(.+?) into color","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"rb/lib/selenium/webdriver/support/color.rb","lineNumber":71,"sourceCode":"            new Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3)\n          when RGB_PCT_PATTERN\n            array = [Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3)]\n            new(*array.map { |e| Float(e) / 100 * 255 })\n          when RGBA_PATTERN\n            new Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3), Regexp.last_match(4)\n          when RGBA_PCT_PATTERN\n            array = [Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3)]\n            new(*array.map { |e| Float(e) / 100 * 255 } << Regexp.last_match(4))\n          when HEX_PATTERN\n            array = [Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3)]\n            new(*array.map { |e| e.to_i(16) })\n          when HEX3_PATTERN\n            array = [Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3)]\n            new(*array.map { |e| (e * 2).to_i(16) })\n          when HSL_PATTERN, HSLA_PATTERN\n            from_hsl(Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3), Regexp.last_match(4))\n          else\n            raise ArgumentError, \"could not convert #{str.inspect} into color\"\n          end\n        end\n\n        def self.from_hsl(hue, sat, light, alpha)\n          hue = Float(hue) / 360\n          sat = Float(sat) / 100\n          light = Float(light) / 100\n          alpha = Float(alpha || 1)\n\n          if sat.zero?\n            r = light\n            g = r\n            b = r\n          else\n            luminocity2 = light < 0.5 ? light * (sat + 1) : light + sat - (light * sat)\n            luminocity1 = (light * 2) - luminocity2\n\n            r = hue_to_rgb(luminocity1, luminocity2, hue + (1.0 / 3.0))","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/rb/lib/selenium/webdriver/support/color.rb#L53-L89","documentation":"Support::Color.from_string parses a CSS color string by trying a fixed set of regexes (rgb, rgb%, rgba, rgba%, #rrggbb, #rgb, hsl, hsla). If none match it raises ArgumentError. Crucially, CSS named colors ('red', 'blue', 'transparent') and modern syntaxes (space-separated rgb(), 8-digit hex, color()) are NOT supported by these patterns, so a visually-valid color string can still fail.","triggerScenarios":"Calling Selenium::WebDriver::Support::Color.from_string with a named color ('red'), a modern css4 color function (color(srgb 1 0 0)), an 8-digit hex (#ff0000ff), a space-separated rgb (rgb(255 0 0)), a malformed/empty string, or a value with surrounding markup. Only the legacy comma-delimited rgb()/rgba()/hsl()/hsla() and #rgb/#rrggbb forms parse.","commonSituations":"Reading element.style.color or getCssValue('color') and passing it through Color.from_string; browsers increasingly return the space-separated rgb() form for getCssValue, which does not match RGBA_PATTERN and fails; test data with named colors.","solutions":["Normalize the string to a supported format (comma-separated rgb()/rgba()/hsl()/hsla() or #rrggbb) before calling from_string.","If the value came from getCssValue, convert space-separated rgb(r g b / a) to comma form rgba(r, g, b, a).","Map named colors to hex yourself before parsing, since from_string does not recognize color names."],"exampleFix":"# before (browser may return space-separated form)\ncolor = Selenium::WebDriver::Support::Color.from_string(element.css_value('background-color'))\n\n# after\nraw = element.css_value('background-color')\nraw = raw.sub(/rgba?\\(([^)]+)\\)/) { |inner| \"rgba(#{inner.tr(' /', ', ')})\" }\ncolor = Selenium::WebDriver::Support::Color.from_string(raw)","handlingStrategy":"validation","validationCode":"SUPPORTED_COLOR = /^\\s*(rgb|rgba|hsl|hsla)\\(|#\\h{3}(?:\\h{3})?\\b/\n\ndef parseable_color?(str)\n  str.is_a?(String) && str.match?(SUPPORTED_COLOR)\nend\n\ncolor = parseable_color?(raw) ? Color.from_string(raw) : fallback","typeGuard":"def legacy_css_color?(str)\n  return false unless str.is_a?(String)\n  patterns = [Color::RGB_PATTERN, Color::RGBA_PATTERN, Color::HEX_PATTERN,\n              Color::HEX3_PATTERN, Color::HSL_PATTERN, Color::HSLA_PATTERN]\n  patterns.any? { |p| str.match?(p) }\nend","tryCatchPattern":"begin\n  Color.from_string(raw)\nrescue ArgumentError => e\n  raise unless e.message.include?('into color')\n  Color.from_string(normalize_to_legacy_rgb(raw))\nend","preventionTips":["Normalize browser-returned css values (space-separated rgb) to comma form before parsing.","Map named colors to hex yourself; from_string does not accept color names.","Validate the string against a known pattern before calling from_string."],"tags":["color","parsing","validation","argument-error","ruby"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}