Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Go from rgb to 32bit int or vice-versa
import random
def rgb_to_int(r,g,b):
i = str()
for c in r,g,b:
for d in str("%03d" % (c,)):
i += '%s%s%s' % (d,d,d)
return i
def int_to_rgb(integer):
rgb = []
ri = integer[:9]
gi = integer[9:18]
bi = integer[18:32]
for i in [ri,gi,bi]:
c = i[:1]
c += i[3:4]
c += i[7:8]
return rgb
r = random.randrange(1,255)
g = random.randrange(1,255)
b = random.randrange(1,255)
integer = rgb_to_int(r,g,b)
print "integer = %s" % rgb_to_int(r,g,b)
print "rgb = %s" % int_to_rgb(integer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment