Skip to content

Commit 5a8dce9

Browse files
bites 86
1 parent a01213b commit 5a8dce9

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@
4242
/36/README.md
4343
/10/README.md
4444
/62/README.md
45+
/86/README.md

86/rgb2hex.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def rgb_to_hex(rgb):
2+
"""Receives (r, g, b) tuple, checks if each rgb int is within RGB
3+
boundaries (0, 255) and returns its converted hex, for example:
4+
Silver: input tuple = (192,192,192) -> output hex str = #C0C0C0"""
5+
if rgb[0] > 255 or rgb[1] > 255 or rgb[2] > 255:
6+
raise ValueError
7+
return '#'+format(rgb[0],'02X') + format(rgb[1],'02X') + format(rgb[2],'02X')
8+
9+
# print(rgb_to_hex((255,255,0)))

86/test_rgb2hex.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import pytest
2+
3+
from rgb2hex import rgb_to_hex
4+
5+
6+
@pytest.mark.parametrize("rgb, hex_", [
7+
# https://www.rapidtables.com/web/color/RGB_Color.html
8+
((0, 0, 0), '#000000'), # black
9+
((255, 255, 255), '#FFFFFF'), # white
10+
((255, 0, 0), '#FF0000'), # red
11+
((0, 255, 0), '#00FF00'), # lime
12+
((0, 0, 255), '#0000FF'), # blue
13+
((255, 255, 0), '#FFFF00'), # yellow
14+
((0, 255, 255), '#00FFFF'), # cyan / aqua
15+
((255, 0, 255), '#FF00FF'), # magenta / fuchsia
16+
((192, 192, 192), '#C0C0C0'), # silver
17+
((128, 128, 128), '#808080'), # gray
18+
((128, 0, 0), '#800000'), # maroon
19+
((128, 128, 0), '#808000'), # olive
20+
((0, 128, 0), '#008000'), # green
21+
((128, 0, 128), '#800080'), # purple
22+
((0, 128, 128), '#008080'), # teal
23+
((0, 0, 128), '#000080'), # navy
24+
])
25+
def test_rgb_to_hex(rgb, hex_):
26+
assert rgb_to_hex(rgb) == hex_
27+
28+
29+
def test_wrong_values():
30+
with pytest.raises(ValueError):
31+
rgb_to_hex((-1, 100, 100))
32+
rgb_to_hex((100, 300, 200))
33+
rgb_to_hex((100, 200, 256))

0 commit comments

Comments
 (0)