Convert HEX to RGB

How i can convert hex to rgb?

On Thu, Aug 10, 2006 at 09:23:04AM +0900, Bruno M. 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 Thu, Aug 10, 2006, Bruno M. 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 :slight_smile:

Ben

On 8/10/06, Bruno M. [email protected] 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

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 M. [email protected] 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 8/9/06, Ben B. [email protected] wrote:

On Thu, Aug 10, 2006, Bruno M. 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 :slight_smile:

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

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

“ff”.hex
=>255

Avram D. 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 M. [email protected] 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

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 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 11/30/07, Jeremy H. [email protected] wrote:

[c.red, c.green, c.blue] # => [16.0, 26.0, 255.0]
Why thank you, Jeremy. :wink:

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…

-----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 Sat, Dec 01, 2007 at 09:18:55AM +0900, Austin Z. 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. :wink:

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

def hex_to_rgb input
a = ( input.match /#(…?)(…?)(…?)/ )[1…3]
a.map!{ |x| x + x } if input.size == 4
“rgb(#{a[0].hex},#{a[1].hex},#{a[2].hex})”
end

hex_to_rgb “#fff” #=> “rgb(255,255,255)”
hex_to_rgb “#ffccff” #=> “rgb(255,204,255)”