Convert HEX to RGB

B

Ben Bleything

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
 
A

Austin Ziegler

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
 
A

Avram Dorfman

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
 
A

Avram Dorfman

Ok, answered my own question. Figured I'd post incase anyone else is
searching for this:

"ff".hex
=>255

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
 
E

Eustáquio 'TaQ' Rangel

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

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

Avram Dorfman

Sweet. That is *so* much more readable. Thank you.

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]
 
J

Jeremy Hinegardner

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

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
 
A

Austin Ziegler

-----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...
 
J

Jeremy Hinegardner

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

Yup, count me in.

enjoy,

-jeremy
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top