Here is how to convert the microsoft way of storing color values (which looks like 16777215 for white) into the familiar RGB CSS style HEX values (which look like #FFFFFF) in Ruby.

1
2
3
4
5
6
7
8
def long_to_rgb_hex_string(val)
  r = "%x" % (val % 256)
  g = "%x" % ((val / 256) % 256)
  b = "%x" % ((val / 65536) % 256)
  "#" + [r,g,b].collect { |c| c.to_s.ljust(2, '0')}.to_s
end

long_to_rgb_hex_string(2093422) # => #6ef11f

Or Grant Hutchins gave this as an option from the comments:

1
2
3
def long_to_rgb_hex_string(val)
  sprintf(’#%06x’, val)
end

Any other good options? :)

blogLater

Mikel

  1. Grant Hutchins Says:

    def long_to_rgb_hex_string(val) sprintf(’#%06x’, val) end

  2. casinos con bonos Says:

    tell me how it convert into c#.the Microsoft way of storing color values.so it is great usability and good for us.I would like to thank you for the efforts you have made in writing this article.

Leave a Reply