Search code examples
rubyrubygemscolorize

Ruby Colorize gem - Is there a method to get the color name from a colorized string?


I'd like to get the name preferably, or the code of a colorized string.

For example:

"Hello".red.color => "red" or :red

I can use regular expressions but want to make sure there isn't already a quick way to do this. Thanks.


Solution

  • Since "Hello".colorize(:red) returns a string, I don't see it possible. A regex for specific color may be a good approach.

    Private function scan_for_colors https://github.com/fazibear/colorize/blob/7c0b50a278da01870be4435ec1b5753c2544e8f3/lib/colorize/instance_methods.rb#L108 is a good start point ...

    UPDATE:

    This solution can work with some colors...

    String.colors.find{|x| stringToCheck.colorize(x) === stringToCheck}