-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgb2hex.py
62 lines (41 loc) · 1.25 KB
/
rgb2hex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def rgb_hex():
invalid_msg = "Invalid value, enter a value between (0 - 255)"
red = int(raw_input("Enter red (R) value:"))
if red not in range(0,256):
print invalid_msg
green = int(raw_input("Enter green (G) value:"))
if green not in range(0,256):
print invalid_msg
blue = int(raw_input("Enter blue (B) value:"))
if blue not in range(0,256):
print invalid_msg
val = (red << 16) + (green << 8) + blue
print "%s" % (hex(val)[2:]).upper()
def hex_rgb():
hex_val = raw_input("Enter a hexadecimal value:")
if len(hex_val) != 6:
print "Invalid value entered, value should contain 6 digits"
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 "Red: %s Green: %s Blue: %s" % (red, green, blue)
def convert():
while True:
option = raw_input("Enter 1 to convert RGB to HEX. Enter 2 to convert HEX to RGB. Enter X to Exit:")
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()