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}"
  end
  def to_hex(n)
    n.to_s(16).rjust(2, '0').upcase
  end

In console:

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

Leave a Reply

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