ntrwva
Last Updated: February 25, 2016
·
1.341K
· filiptepper

Convert CSS RGB value to HEX value

A simple function to convert CSS return value of capybara's evaluate_script call to $('.class).css('value') to HEX value:

def rgb_to_hex(value)
  %Q{
    ##{
      value.match(/[\d,\s]+/)[0].split(',').map(&:strip).map { |value|
        value.to_i.to_s(16).rjust(2, '0')
      }.join('')
    }
  }.strip
end