Converting RGB colors to Hexadecimal with ruby

Just a tip to convert RGB colors to Hexadecimal:

  def rgb(r, g, b)
    "##{to_hex r}#{to_hex g}#{to_hex b}"
  def to_hex(n)
    n.to_s(16).rjust(2, '0').upcase

In console:

rgb(50, 205, 50)
=> "#32CD32"

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.