Closed trans closed 10 years ago
Sounds interesting. I don't have time to work on this at the moment (paying work, work on my wife's novel, and gem work on libraries that I consider more important/relevant) but your proposed API isn't a bad one. The only thing that I'd probably do differently is that I probably wouldn't have #css_name return an inexact match unless the user requested it (maybe #css_nearest_names—since it could theoretically return more than one).
If you want to work up a pull request, I'd be happy to consider it for inclusion.
Cool. I don't have time at the moment either, but I'll try to get to it sooner rather then later.
These methods are going to be hung off Color::RGB. The functionality is:
Color::RGB.by_hex('0ff') # => Color::RGB::Cyan
Color::RGB.by_hex('333') # => Color::RGB.from_hex('333')
Color::RGB.by_name('cyan') # => Color::RGB::Cyan
Color::RGB.by_name('cyanide') # => raise KeyError
Color::RGB.by_css('0ff') # => Color::RGB::Cyan
Color::RGB.by_css('333') # => Color::RGB.from_hex('333')
Color::RGB.by_css('cyan') # => Color::RGB::Cyan
Color::RGB.by_css('cyanide') # => raise ArgumentError
I've made it so that Color::CSS[name]
calls Color::RGB.by_name(name)
instead.
As far as #extract_names
is concerned, here’s the test:
assert_equal([ Color::RGB::BlanchedAlmond, Color::RGB::Cyan ],
Color::RGB.extract_colors('BlanchedAlmond is a nice shade, but #00ffff is not.'))
Came across this project today: https://github.com/twoism-dev/Spectrum. It's a rather small set of functions for extracting color references from strings.
Seems like that could be a good addition to color gem, and can do it better since it can return actual Color objects.
So maybe something like:
Also, it would be cool to get the CSS name given the html string.
Taking it one step further, maybe even return the closest matching name, if there is no exact match.