forked from inglesp/python-data-structure-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrgb_colours.py
More file actions
85 lines (68 loc) · 2.03 KB
/
rgb_colours.py
File metadata and controls
85 lines (68 loc) · 2.03 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# This program knows about the RGB code corresponding to common colours.
#
# Usage:
#
# $ python rgb_colours.py [colour]
#
# For instance:
#
# $ python rgb_colours.py red
# The RGB code for red is F00
#
# or:
#
# $ python rgb_colours.py "burnt sienna"
# I don't know the RGB code for burnt sienna
import argparse
import re
colours = [
['red', 'F00'],
['yellow', 'FF0'],
['green', '0F0'],
['cyan', '0FF'],
['blue', '00F'],
['magenta', 'F0F'],
]
colours_dict = dict(colours)
def main():
arg_dict = get_args()
arg = arg_dict["colour_or_rgb"]
if get_rgb_code(arg):
print(f"The RGB code for {arg} is {get_rgb_code(arg)}")
elif is_arg_an_rgb_code(arg):
colour = get_colour_from_rgb_code(arg.upper())
if colour:
print(f"The colour for RGB code {arg} is {colour}")
else:
print(f"I don't know the colour for RGB code {arg}")
else:
print(f"I don't know the RGB code for {arg}")
def get_args():
parser = argparse.ArgumentParser(
prog="rbg_colours",
description="Display the RGB code for a corresponding colour, or display the colour for a corresponding RGB code"
)
parser.add_argument(
"colour_or_rgb",
help="the name of a colour or an RGB colour code",
nargs="?",
default="",
type=str,
)
return vars(parser.parse_args())
def get_rgb_code(colour):
return colours_dict.get(colour.lower())
def is_arg_an_rgb_code(arg):
return bool(re.search('0', arg))
def get_colour_from_rgb_code(rgb_code):
corresponding_colour = [k for k, v in colours_dict.items() if v == rgb_code]
if corresponding_colour:
return corresponding_colour[0]
if __name__ == "__main__":
main()
# TODO:
# * Implement the program as described in the comments at the top of the file.
# TODO (extra):
# * Change the program so that users can also enter an RGB colour code, and be
# told the name of the corresponding colour.
# * Change the program so that it ignores the case of the user's input.