public
Last active

Go from rgb to 32bit int or vice-versa

  • Download Gist
rgbint.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#!/bin/python
 
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]
rgb.append(c)
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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.