How i can convert hex to rgb?
on 10.08.2006 02:32
On Thu, Aug 10, 2006 at 09:23:04AM +0900, Bruno Malvestuto wrote:
> How i can convert hex to rgb?
Hex is RGB.
#ff0000 = r:255 g:0 b:0
I'm not entirely sure what you're asking.
on 10.08.2006 02:41
On Thu, Aug 10, 2006, Bruno Malvestuto wrote:
> How i can convert hex to rgb?
Do you mean a hex color code like #ffccff to RGB values like:
R: 255
G: 204
B: 255
?
The first two characters of the hex color code are the red value, the
second pair the green, and the final pair the blue value. Grab those
three substrings, convert the hex to decimal, and you're golden.
"#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 :)
Ben
on 10.08.2006 02:47
On 8/9/06, Ben Bleything <ben@bleything.net> wrote: > 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 :) Better yet, use color-tools (gem i color-tools). This is currently part of the Ruby PDF project, but it will be moving to the "color" project in due course. -austin
on 15.08.2006 14:12
On 8/10/06, Bruno Malvestuto <bruno@garageim.com> wrote:
> How i can convert hex to rgb?
hex = 0xffeedd
rgb = {}
%w(r g b).inject(hex) {|a,i| rest, rgb[i] = a.divmod 256; rest}
p rgb
martin
on 30.11.2007 11:47
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 DeMello wrote: > On 8/10/06, Bruno Malvestuto <bruno@garageim.com> wrote: >> How i can convert hex to rgb? > > hex = 0xffeedd > rgb = {} > %w(r g b).inject(hex) {|a,i| rest, rgb[i] = a.divmod 256; rest} > p rgb > > martin
on 30.11.2007 11:50
Ok, answered my own question. Figured I'd post incase anyone else is searching for this: "ff".hex =>255 Avram Dorfman wrote: > 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 DeMello wrote: >> On 8/10/06, Bruno Malvestuto <bruno@garageim.com> wrote: >>> How i can convert hex to rgb? >> >> hex = 0xffeedd >> rgb = {} >> %w(r g b).inject(hex) {|a,i| rest, rgb[i] = a.divmod 256; rest} >> p rgb >> >> martin
on 30.11.2007 11:59
-----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] - -- Eustáquio "TaQ" Rangel http://eustaquiorangel.com "Imagination is more important than knowledge." A. Einstein -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) iD8DBQFHT+z+b6UiZnhJiLsRAtBSAJ40zsi0V1+QERk8OkZaCwWsUDQ2OwCaAzsf 7UvsRM3F0ILHZH+Wzj4ylSM= =s76P -----END PGP SIGNATURE-----
on 30.11.2007 12:01
Sweet. That is *so* much more readable. Thank you. Eustáquio 'TaQ' Rangel wrote: > -----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] >
on 30.11.2007 21:13
On Fri, Nov 30, 2007 at 07:59:15PM +0900, Eust?quio 'TaQ' Rangel wrote: > >> "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] enjoy, -jeremy
on 01.12.2007 01:19
On 11/30/07, Jeremy Hinegardner <jeremy@hinegardner.org> wrote: > > > [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...
on 01.12.2007 04:02
On Sat, Dec 01, 2007 at 09:18:55AM +0900, Austin Ziegler wrote: > > > Gimme a good and old blue RGB: > > 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... Yup, count me in. enjoy, -jeremy