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 Response to “Convert Visual Basic / Microsoft Long Integer Color Values to CSS RGB Format”
Leave a Reply
Recent Articles
Latest comments
- RSL
Yeah. I’ve found myself vacillating on how much and what exactly to spec myself. It’s definitely a process, finding what...
- Carlisia
Thanks a lot, this is simple yet reassuring info for a newcomer to Rails!
- Mikel
@RSL: You know, you are right :) I am too of course! But the bottom line is that I guess...
- RSL
Hey. Great blog. Nice to see a new face [to me at least] writing about testing. I do have to...
- Oli
Hi,
I am looking for something like this, but now I don;t understand the mail = TMail::Mail.parse(message) what is the...
- Vinay
I m getting javascript error in firebug console saying “document.observe is not a function”
but i included <%= javascript_include_tag :defaults...
June 25th, 2008 at 05:20 PM
def long_to_rgb_hex_string(val) sprintf(’#%06x’, val) end