tags:

views:

155

answers:

8

How can I convert colors from (N, N, N) format to #AABBCC (and #AAABBBCCC) ?

thanks

+2  A: 

#FFFFFF, so simple

every single char has 0..F range. That is 0..15. So two chars is 0..(16*16-1) -> 0-255

To convert between formats just think about:

#AABBCC are three values AA BB CC. Every single value represents a channel (red, green, blue) that can span from 0 to 255 or from 0 to FF or from 0.0 to 1.0

if you have for example #123456 you can do

12 -> 1*16 + 2 = .. (result in range 0-255)
34 -> 3*16 + 4 = ..
56 -> 5*16 + 6 = ..

in general a two digits hex number composed by XY can be converted to an decimal value by multiplying X by 16 and adding Y, taking care of converting digits that are over 9 (A, B, C, D, E, F) to their counterparts (10, 11, 12, 13, 14, 15). So for example AC would be A*16 + C = 10*16 + 12.

(To be really precise a n digit hex number is converted by multiplying the i-th digit from right by 16^i and adding all of them together)

Jack
In the second line, I think you mean `0-15` and `0-(16*16-1) -> 0-255`.
gnovice
ye, sry, let me fix that.. otherwise it would be misleading :)
Jack
+1  A: 

From 00 to FF. It is hexacecimal for 0 to 255.

divideandconquer.se
@divideandconquer.se Sorry.. my problem is that I don't know how to convert between formats.. from (0,1,0) to #00FF00 to (000, 256, 000) (using Python)
Patrick
No worries, but please try to phrase this and future questions better :-)
divideandconquer.se
A: 
 RRGGBB    RRGGBB
#000000 - #FFFFFF
 Black  -  White

RR = 00 - FF or 0 - 255
GG = 00 - FF or 0 - 255
BB = 00 - FF or 0 - 255
gmcalab
@gmcalab OK... my problem is that I don't know how to convert between formats.. from (0,1,0) to #00FF00 to (000, 256, 000) (using Python)
Patrick
@Patrick: First convert the value 0..1 to 0..255, then convert it to hexadecimal. The first part is easy, you just multiply the value by 255 (or 0xFF). There are a number of ways to do the conversion to hex. You can use string interpolation (`%` operator), or the builtin `hex()` function, or a lookup table. See the various answers people (including myself) have posted to your question.
martineau
A: 

Those are hexidecimal representations of 16 bit numbers for the Red Green and Blue channels. So 0 to 255 for each channel. FF (hex) is equal to 255 decimal.

Coding Gorilla
A: 

As others have said, 00-FF.

Here's an overview of HTML colors in hex notation:
http://www.w3schools.com/Html/html_colors.asp

You can find out how to convert from hex to decimal here:
http://en.wikipedia.org/wiki/Hexadecimal

And, for Python:
http://stackoverflow.com/questions/214359/converting-hex-to-rgb-and-vice-versa/214657#214657

Or search for "convert hex decimal"

Ryan Emerle
A: 

This is hexadecimal (base 16) notation, where each digit goes from 0 to 15 (F).

The range of 0 to FF in hexadecimal is 0 to 255 in decimal.

If you want to convert from one to another, there are plenty of sites that will do that for you - like this one.

Oded
F is equivalent to 15, not 16.
gnovice
@gnovice - quite right.
Oded
A: 

Using Python? Try this:

c = (0., 1., 0.)
rgb = '#%02X%02X%02X' % (c[0] * 255, c[1] * 255, c[2] * 255)
eumiro
A: 

The min and max values for colors in the #AABBCC format is #000000...#FFFFFF, or 0...16777215 in decimal. Each individual color component ranges from #00..#FF, which is 0..255 in decimal and requires 8-bits or 1 byte of storage. For #AAABBBCCC the range of components is #000-#FFF or 0..4095 each and they require 12-bits or 1½ bytes of storage.

Not sure what the range of values is for N in (N, N, N), but if it's 0..1 then these two functions will convert from it to either 8-bit component #AABBCC or 12-bit component #AAABBBCCC color values (without rounding). Note that the output of each function is a string with the value shown after each print statement below. ITOH8 and ITOH12 are constant lookup tables used by the corresponding function.

ITOH8 = [('%02X' % i) for i in range(0x100)]

rgbconv8 = lambda c: ''.join( ['#'] + [ITOH8[int(v*0xFF)] for v in c] )

print rgbconv8((0., 1., 0.)) #00FF00
print rgbconv8((.2, .6, .75)) #3399BF

ITOH12 = [('%03X' % i) for i in range(0x1000)]

rgbconv12 = lambda c: ''.join( ['#'] + [ITOH12[int(v*0xFFF)] for v in c] )

print rgbconv12((0., 1., 0.)) #000FFF000
print rgbconv12((.2, .6, .75)) #333999BFF
martineau