Closed crypt1d closed 2 years ago
Looking at the code, I find it weird that the "green" worked, since only the Symbol code path looks for Named colors.
That's really weird indeed 🤔
Hi,
From what I've read in the code, "green" decomposed in RGB would be gr
,ee
,n
when translated to hexadecimal would be 0
,238
,0
as to_i
would return 0
for non valid numbers.
The fact that "green" actually produces a green (not as green as the actual green output which gives "\e[32mthis is a test message\e[0m"
) output is just pure luck 😄
As for "red", it errors because the B of RGB would be nil
and nil.to_i
doesn't accept any parameters.
Maybe Rainbow::Color.parse_hex_color
should check its argument is an hexadecimal string with a length of 6 characters after removing the potential #
prefix, if it makes sense?
@chiting wow, that's an awesome detective work 👍
Indeed it would make sense to validate the string, maybe with a regexp like /^#?[a-f0-9]{6}/i
or similar, and raise an exception if it doesn't match.
Would it be possible to release a new gem version with this fix? Breaking a few projects.
I’m split between recommending bumping the patch version and the major version for this fix. Although this is a bug fix, it has a breaking change that might have gone un-noticed and not really impacting the code that uses that gem.
https://rubygems.org/gems/rainbow/versions/3.1.0 has been released. @chiting thanks for making a PR!
Specifying
red
color by name seems to fail, while it works for other colors. It also works if color name is specified as a symbol, eg:red
To reproduce
Ruby: 2.4.1 rubygems: 2.6.14 rainbow gem: 3.0.0 OS: CentOS 7.4