Created
January 18, 2019 21:53
-
-
Save aferriss/7be179c2a88e6a53055651ab2cd50399 to your computer and use it in GitHub Desktop.
Encode an int to a normalized rgb value and back again.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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