Skip to content

Commit 8a2efff

Browse files
cicerotcvcclauss
andauthored
feat: implements a hex to rgb conversion function (#11113)
* feat: implements a hex to rgb conversion function * docs: adds module's author, description and references * chore: fix file trailing line * feat: insert the algorithms into Directory.md --------- Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent c77db5a commit 8a2efff

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎DIRECTORY.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222
* [Energy Conversions](conversions/energy_conversions.py)
223223
* [Excel Title To Column](conversions/excel_title_to_column.py)
224224
* [Hex To Bin](conversions/hex_to_bin.py)
225+
* [Hex To Rgb](conversions/hex_to_rgb.py)
225226
* [Hexadecimal To Decimal](conversions/hexadecimal_to_decimal.py)
226227
* [Int To Negative Binary Base](conversions/int_to_negative_binary_base.py)
227228
* [Ipv4 Conversion](conversions/ipv4_conversion.py)

‎conversions/hex_to_rgb.py‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""
2+
* Author: Cicero Tiago Carneiro Valentim (https://github.com/cicerotcv)
3+
* Description: Convert hexadecimal (#FF2000) color to RGB (rgb(255, 32, 0)).
4+
5+
References:
6+
https://www.w3schools.com/colors/colors_rgb.asp
7+
https://www.w3schools.com/colors/colors_hexadecimal.asp
8+
"""
9+
10+
11+
def hex_to_rgb(hex_color: str) -> str:
12+
"""
13+
Converts a hexadecimal color code to RGB values.
14+
15+
Args:
16+
hex_color (str): A hexadecimal color code, e.g., "#RGB" or "#RRGGBB".
17+
18+
Returns:
19+
str: A string representation of a rgb value "rgb(r, g, b)" containing
20+
three integers.
21+
22+
Raises:
23+
ValueError: If the input hex_color is not a valid hexadecimal color code.
24+
25+
Examples:
26+
>>> hex_to_rgb("#FF0000")
27+
'rgb(255, 0, 0)'
28+
29+
>>> hex_to_rgb("#00FF00")
30+
'rgb(0, 255, 0)'
31+
32+
>>> hex_to_rgb("#123")
33+
'rgb(17, 34, 51)'
34+
35+
>>> hex_to_rgb("#000000")
36+
'rgb(0, 0, 0)'
37+
38+
>>> hex_to_rgb("#FFFFFF")
39+
'rgb(255, 255, 255)'
40+
41+
>>> hex_to_rgb("#0088FF")
42+
'rgb(0, 136, 255)'
43+
44+
>>> hex_to_rgb("#0032ccAA")
45+
Traceback (most recent call last):
46+
...
47+
ValueError: Invalid hex color code
48+
49+
>>> hex_to_rgb("#0032")
50+
Traceback (most recent call last):
51+
...
52+
ValueError: Invalid hex color code
53+
54+
Note:
55+
- The function supports both 6-digit and 3-digit hex color codes.
56+
"""
57+
58+
hex_color = hex_color.lstrip("#")
59+
60+
# Check if the input is a valid hex color code
61+
if not (len(hex_color) == 6 or len(hex_color) == 3):
62+
raise ValueError("Invalid hex color code")
63+
64+
# Expand 3-digit hex codes to 6 digits (e.g., "#123" to "#112233")
65+
if len(hex_color) == 3:
66+
hex_color = "".join([char * 2 for char in hex_color])
67+
68+
# Parse the hex values to integers
69+
red = int(hex_color[0:2], 16)
70+
green = int(hex_color[2:4], 16)
71+
blue = int(hex_color[4:6], 16)
72+
73+
return f"rgb({red}, {green}, {blue})"
74+
75+
76+
if __name__ == "__main__":
77+
import doctest
78+
79+
doctest.testmod()

0 commit comments

Comments
 (0)