Skip to content

Instantly share code, notes, and snippets.

@AbbadonAA
Created September 26, 2021 22:36
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 AbbadonAA/2cd8fd164b5d25fac1bbd701421f0ef2 to your computer and use it in GitHub Desktop.
Save AbbadonAA/2cd8fd164b5d25fac1bbd701421f0ef2 to your computer and use it in GitHub Desktop.
Pass decimal to hexadecimal RGB representation
def rgb(r, g, b):
dictionary = {0: 0,
1: 1,
2: 2,
3: 3,
4: 4,
5: 5,
6: 6,
7: 7,
8: 8,
9: 9,
10: 'A',
11: 'B',
12: 'C',
13: 'D',
14: 'E',
15: 'F',
}
r = (0 if r < 0 else r) and (255 if r > 255 else r)
g = (0 if g < 0 else g) and (255 if g > 255 else g)
b = (0 if b < 0 else b) and (255 if b > 255 else b)
r1 = f'{dictionary[int(r // 16)]}{dictionary[int(r % 16)]}'
g1 = f'{dictionary[int(g // 16)]}{dictionary[int(g % 16)]}'
b1 = f'{dictionary[int(b // 16)]}{dictionary[int(b % 16)]}'
return f'{r1}{g1}{b1}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment