B
Bruno Malvestuto
How i can convert hex to rgb?
How i can convert hex to rgb?
How i can convert hex to rgb?
On Thu, Aug 10, 2006, Bruno Malvestuto wrote:
"#ffccff".match /#(..)(..)(..)/
puts "R: #{m[1].hex}"
puts "G: #{m[2].hex}"
puts "B: #{m[3].hex}"
That's not going to work in all cases (for instance, it's legal to do
#fff if you mean #ffffff), but it should get you started
How i can convert hex to rgb?
Martin said:How i can convert hex to rgb?
hex = 0xffeedd
rgb = {}
%w(r g b).inject(hex) {|a,i| rest, rgb = a.divmod 256; rest}
p rgb
martin
Avram said:Ummm... This is a really clever solution, except if you have a hex value
that you need to convert into an rgb array, it is probably a string, not
an integer literal. Is there an easy way to convert a hex string
directly into an integer literal?
(not that anybody is still watching this thread, but I figured I'd ask)
Martin said:How i can convert hex to rgb?
hex = 0xffeedd
rgb = {}
%w(r g b).inject(hex) {|a,i| rest, rgb = a.divmod 256; rest}
p rgb
martin
Ummm... This is a really clever solution, except if you have a hex value
that you need to convert into an rgb array, it is probably a string, not
an integer literal. Is there an easy way to convert a hex string
directly into an integer literal?
Eustáquio 'TaQ' Rangel said:-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Ummm... This is a really clever solution, except if you have a hex value
that you need to convert into an rgb array, it is probably a string, not
an integer literal. Is there an easy way to convert a hex string
directly into an integer literal?
Gimme a good and old blue RGB:
=> [16, 26, 255]
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Ummm... This is a really clever solution, except if you have a hex value
that you need to convert into an rgb array, it is probably a string, not
an integer literal. Is there an easy way to convert a hex string
directly into an integer literal?
Gimme a good and old blue RGB:
=> [16, 26, 255]
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Ummm... This is a really clever solution, except if you have a hex value
that you need to convert into an rgb array, it is probably a string, not
an integer literal. Is there an easy way to convert a hex string
directly into an integer literal?
Gimme a good and old blue RGB:
=> [16, 26, 255]"101aff".scan(/../).map {|color| color.to_i(16)}
Or use color (http://rubyforge.org/projects/color/)
require 'rubygems'
require 'color'
c = Color::RGB.from_html("101aff")
c.css_rgb # => rgb(6.27%, 10.20%, 100.00%)
[c.red, c.green, c.blue] # => [16.0, 26.0, 255.0]
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Ummm... This is a really clever solution, except if you have a hex value
that you need to convert into an rgb array, it is probably a string, not
an integer literal. Is there an easy way to convert a hex string
directly into an integer literal?
Gimme a good and old blue RGB:
"101aff".scan(/../).map {|color| color.to_i(16)}
=> [16, 26, 255]
Or use color (http://rubyforge.org/projects/color/)
require 'rubygems'
require 'color'
c = Color::RGB.from_html("101aff")
c.css_rgb # => rgb(6.27%, 10.20%, 100.00%)
[c.red, c.green, c.blue] # => [16.0, 26.0, 255.0]
Why thank you, Jeremy.
What's beautiful about the Color rubygem is that it's a lot more than
just RGB, too. Much much more.
-austin, thinking he should try to conscript Jeremy into helping out
with Color again...
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.