-4

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

thanks

Gabe
  • 49,577
  • 28
  • 142
  • 181
aneuryzm
  • 63,052
  • 100
  • 273
  • 488
  • Do you mean basic hex values for colours? Where `AA` = red, `BB`=green and `CC`=blue, ranging from `00` to `FF`? (0-255) – Tarka Oct 22 '10 at 19:13
  • @Slokun yeah.. 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) – aneuryzm Oct 22 '10 at 19:14
  • @Patrick - then you should rephrase your question. You don't mention conversion or Python. – Ryan Emerle Oct 22 '10 at 19:19
  • @Ryan Emerle I guess it is better to create a new question then – aneuryzm Oct 22 '10 at 19:21
  • http://stackoverflow.com/questions/214359/converting-hex-to-rgb-and-vice-versa/ – Ryan Emerle Oct 22 '10 at 19:21
  • @Patrick - if you want to use it in Python, edit your question, add the 'python' tag and there's already a python answer for you below... – eumiro Oct 22 '10 at 19:32

8 Answers8

2

#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
  • 131,802
  • 30
  • 241
  • 343
1

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

activout.se
  • 6,058
  • 4
  • 27
  • 37
0
 RRGGBB    RRGGBB
#000000 - #FFFFFF
 Black  -  White

RR = 00 - FF or 0 - 255
GG = 00 - FF or 0 - 255
BB = 00 - FF or 0 - 255
Gabe
  • 49,577
  • 28
  • 142
  • 181
  • 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) – aneuryzm Oct 22 '10 at 19:17
  • @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 Oct 23 '10 at 15:27
0

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.

CodingGorilla
  • 19,612
  • 4
  • 45
  • 65
0

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:
Converting hex color to RGB and vice-versa

Or search for "convert hex decimal"

Community
  • 1
  • 1
Ryan Emerle
  • 15,461
  • 8
  • 52
  • 69
0

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
  • 489,969
  • 99
  • 883
  • 1,009
0

Using Python? Try this:

c = (0., 1., 0.)
rgb = '#%02X%02X%02X' % (c[0] * 255, c[1] * 255, c[2] * 255)
eumiro
  • 207,213
  • 34
  • 299
  • 261
0

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
  • 119,623
  • 25
  • 170
  • 301