[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Convert HEX to RGB

Bruno Malvestuto

8/10/2006 12:23:00 AM

How i can convert hex to rgb?

--
Posted via http://www.ruby-....

11 Answers

Chad Perrin

8/10/2006 12:30:00 AM

0

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.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
"A script is what you give the actors. A program
is what you give the audience." - Larry Wall

Ben Bleything

8/10/2006 12:40:00 AM

0

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

Austin Ziegler

8/10/2006 12:46:00 AM

0

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
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Martin DeMello

8/10/2006 7:53:00 AM

0

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

Avram Dorfman

11/30/2007 10:47:00 AM

0

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 via http://www.ruby-....

Avram Dorfman

11/30/2007 10:50:00 AM

0

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 via http://www.ruby-....

Eustaquio 'TaQ' Rangel

11/30/2007 10:59:00 AM

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]

- --
Eustáquio "TaQ" Rangel
http://eustaquio...

"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-----

Avram Dorfman

11/30/2007 11:02:00 AM

0

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 via http://www.ruby-....

Jeremy Hinegardner

11/30/2007 8:13:00 PM

0

On Fri, Nov 30, 2007 at 07:59:15PM +0900, 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]

Or use color (http://rubyforge.org/proje...)

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

--
========================================================================
Jeremy Hinegardner jeremy@hinegardner.org


Austin Ziegler

12/1/2007 12:19:00 AM

0

On 11/30/07, Jeremy Hinegardner <jeremy@hinegardner.org> wrote:
> On Fri, Nov 30, 2007 at 07:59:15PM +0900, 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]
>
> Or use color (http://rubyforge.org/proje...)
>
> 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...
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca