Skip to content

Instantly share code, notes, and snippets.

@DewofyourYouth
Created June 20, 2018 11:51
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 DewofyourYouth/5ed91903f2f376b8e99a3262cb279c1f to your computer and use it in GitHub Desktop.
Save DewofyourYouth/5ed91903f2f376b8e99a3262cb279c1f to your computer and use it in GitHub Desktop.
HEX RGB Conversion
def get_hex(red, green, blue):
rgb = [red, green, blue]
hex_val = '#'
for color in rgb:
hex_val += hex(color)[2:]
print(hex_val)
get_hex(209, 212, 211)
#%%
def get_rgb(hex_val):
if len(hex_val) == 3:
hex_val = hex_val * 2
hex_conversion = {'a': 10, 'b': 11, 'c': 12, 'd': 13, 'e': 14, 'f': 15 }
rgb = ['Red: ', 'Green:', 'Blue: ']
i = 1
j = 0
for char in hex_val:
try:
int(char)
num = int(char)
except ValueError:
if char not in hex_conversion:
print('\n<--- invalid hex code ---->'.upper())
print('\n \\ /')
print(' * *')
print(' < ')
print('\n ~ \n')
print('<--- invalid hex code ---->'.upper())
break
num = hex_conversion[char.lower()]
if i % 2 != 0:
num1 = num * 16
else:
print('{} {}'.format(rgb[j], num + num1))
j = j + 1
i = i + 1
user_hex = input('Type a hex color value (no hash): ')
get_rgb(user_hex)
#%%
def convert_hex_to_rgb():
user_hex = input('Type a hex color value (w/o hash): ')
get_rgb(user_hex)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment