Get the inverse of a hex color in Ruby
1 def invert_color(color) 2 color.gsub!(/^#/, '') 3 sprintf("%X", color.hex ^ 0xFFFFFF) 4 end
Example:
1 invert_color('#c0c0c0') #=> "3F3F3F"
Limitations:
Doesn't handle named colors or 3 digit colors (i.e. #FFF == #FFFFFF)