File tree 1 file changed +62
-0
lines changed
1 file changed +62
-0
lines changed Original file line number Diff line number Diff line change
1
+ def rgb_hex ():
2
+ invalid_msg = "Invalid value, enter a value between (0 - 255)"
3
+
4
+ red = int (raw_input ("Enter red (R) value:" ))
5
+ if red not in range (0 ,256 ):
6
+ print invalid_msg
7
+
8
+ green = int (raw_input ("Enter green (G) value:" ))
9
+ if green not in range (0 ,256 ):
10
+ print invalid_msg
11
+
12
+ blue = int (raw_input ("Enter blue (B) value:" ))
13
+ if blue not in range (0 ,256 ):
14
+ print invalid_msg
15
+
16
+ val = (red << 16 ) + (green << 8 ) + blue
17
+
18
+ print "%s" % (hex (val )[2 :]).upper ()
19
+
20
+ def hex_rgb ():
21
+ hex_val = raw_input ("Enter a hexadecimal value:" )
22
+
23
+ if len (hex_val ) != 6 :
24
+ print "Invalid value entered, value should contain 6 digits"
25
+ return
26
+
27
+ else :
28
+ hex_val = int (hex_val , 16 )
29
+
30
+ two_hex_digits = 2 ** 8
31
+ blue = hex_val % two_hex_digits
32
+
33
+ hex_val = hex_val >> 8
34
+
35
+ green = hex_val % two_hex_digits
36
+
37
+ hex_val = hex_val >> 8
38
+
39
+ red = hex_val % two_hex_digits
40
+
41
+ print "Red: %s Green: %s Blue: %s" % (red , green , blue )
42
+
43
+
44
+ def convert ():
45
+ while True :
46
+ option = raw_input ("Enter 1 to convert RGB to HEX. Enter 2 to convert HEX to RGB. Enter X to Exit:" )
47
+
48
+ if option == "1" :
49
+ print "RGB to Hex.."
50
+ rgb_hex ()
51
+
52
+ elif option == "2" :
53
+ print "Hex to RGB.."
54
+ hex_rgb ()
55
+
56
+ elif option == "X" or option == "x" :
57
+ break
58
+
59
+ else :
60
+ print "Error"
61
+
62
+ convert ()
You can’t perform that action at this time.
0 commit comments