开发者_运维技巧How can I convert colors from (N, N, N) format to #AABBCC (and #AAABBBCCC) ?
thanks
#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)
From 00 to FF. It is hexacecimal for 0 to 255.
RRGGBB RRGGBB
#000000 - #FFFFFF
Black - White
RR = 00 - FF or 0 - 255
GG = 00 - FF or 0 - 255
BB = 00 - FF or 0 - 255
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.
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"
Using Python? Try this:
c = (0., 1., 0.)
rgb = '#%02X%02X%02X' % (c[0] * 255, c[1] * 255, c[2] * 255)
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.
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
精彩评论