-
-
Save codecademydev/c9d898fc20bcc67cf32647567c55795d to your computer and use it in GitHub Desktop.
Codecademy export
This file contains 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
def rgb_hex(): | |
invalid_msg="Sorry, thats an invalid response." | |
red=int(raw_input("Please enter the Red: ")) | |
if red<0 or red>255: | |
print invalid_msg | |
return | |
green=int(raw_input("Please enter the Green(G): ")) | |
if green<0 or green>255: | |
print invalid_msg | |
return | |
blue=int(raw_input("Please enter the Blue(B): ")) | |
if blue<0 or blue>255: | |
print invalid_msg | |
return | |
val=(red<<16)+(green<<8)+blue | |
print %s % (hex(val)[2:]).upper() | |
def hex_rgb(): | |
hex_val=raw_input("Please enter a hexadecimal value (six decimal digits): ") | |
if len(hex_val) != 6: | |
print invalid_msg | |
return | |
else: | |
hex_val=int(hex_val, 16) | |
two_hex_digits=2**8 | |
blue=hex_val % two_hex_digits | |
hex_val=hex_val>>8 | |
green=hex_val%two_hex_digits | |
hex_val=hex_val>>8 | |
red=hex_val % two_hex_digits | |
print "The RGB values are: R: %s, B: %s, G: %s " %(red, blue, green) | |
def convert(): | |
while True: | |
print "Enter 1 to convert RGB to HEX.\n" | |
print "Enter 2 to convert HEX to RGB.\n" | |
print "Enter X to Exit.\n" | |
option=raw_input("Response: ") | |
if option==1: | |
print "rgb to HEX" | |
rgb_hex() | |
elif option==2: | |
print"HEX to RGB" | |
hex_rgb() | |
elif option==X or option==x: | |
break | |
else: | |
print ERROR. | |
convert() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment