Forum: Ruby Convert HEX to RGB

Posted by Lenrek bruno (lenrek)
on 2006-08-10 02:23
How i can convert hex to rgb?
Posted by Chad Perrin (Guest)
on 2006-08-10 02:32
(Received via mailing list)
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.
Posted by Ben Bleything (Guest)
on 2006-08-10 02:41
(Received via mailing list)
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
Posted by Austin Ziegler (austin)
on 2006-08-10 02:47
(Received via mailing list)
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
Posted by Martin DeMello (Guest)
on 2006-08-15 14:12
(Received via mailing list)
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
Posted by Avram Dorfman (avramd)
on 2007-11-30 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
Posted by Avram Dorfman (avramd)
on 2007-11-30 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
Posted by Eustáquio 'TaQ' Rangel (Guest)
on 2007-11-30 11:59
(Received via mailing list)
-----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-----
Posted by Avram Dorfman (avramd)
on 2007-11-30 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]
> 
Posted by Jeremy Hinegardner (Guest)
on 2007-11-30 21:13
(Received via mailing list)
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
Posted by Austin Ziegler (austin)
on 2007-12-01 01:19
(Received via mailing list)
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...
Posted by Jeremy Hinegardner (Guest)
on 2007-12-01 04:02
(Received via mailing list)
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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.