Skip to content

Instantly share code, notes, and snippets.

@aferriss
Created January 18, 2019 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aferriss/7be179c2a88e6a53055651ab2cd50399 to your computer and use it in GitHub Desktop.
Save aferriss/7be179c2a88e6a53055651ab2cd50399 to your computer and use it in GitHub Desktop.
Encode an int to a normalized rgb value and back again.
base = 256
def encode(val):
r = val / (base * base)
g = (val / base) % base
b = val % base
return [float(r)/255.0, float(g)/255.0, float(b)/255.0]
def dot(K, L):
if len(K) != len(L):
return 0
return sum(i[0] * i[1] for i in zip(K, L))
def decode(rgb):
rgb = [rgb[0]*255.0, rgb[1]*255.0, rgb[2]*255]
return dot(rgb, [base*base, base, 1.0])
val = encode(12345679)
print(val)
print(decode(val))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment