From c493d23dda0fdde7466ea17e8b3bd9bd076b8c66 Mon Sep 17 00:00:00 2001 From: cicerotcv Date: Mon, 30 Oct 2023 18:22:22 -0300 Subject: [PATCH 1/4] feat: implements a hex to rgb conversion function --- conversions/hex_to_rgb.py | 70 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 conversions/hex_to_rgb.py diff --git a/conversions/hex_to_rgb.py b/conversions/hex_to_rgb.py new file mode 100644 index 000000000..3a6c5b457 --- /dev/null +++ b/conversions/hex_to_rgb.py @@ -0,0 +1,70 @@ +def hex_to_rgb(hex_color: str) -> str: + """ + Converts a hexadecimal color code to RGB values. + + Args: + hex_color (str): A hexadecimal color code, e.g., "#RGB" or "#RRGGBB". + + Returns: + str: A string representation of a rgb value "rgb(r, g, b)" containing + three integers. + + Raises: + ValueError: If the input hex_color is not a valid hexadecimal color code. + + Examples: + >>> hex_to_rgb("#FF0000") + 'rgb(255, 0, 0)' + + >>> hex_to_rgb("#00FF00") + 'rgb(0, 255, 0)' + + >>> hex_to_rgb("#123") + 'rgb(17, 34, 51)' + + >>> hex_to_rgb("#000000") + 'rgb(0, 0, 0)' + + >>> hex_to_rgb("#FFFFFF") + 'rgb(255, 255, 255)' + + >>> hex_to_rgb("#0088FF") + 'rgb(0, 136, 255)' + + >>> hex_to_rgb("#0032ccAA") + Traceback (most recent call last): + ... + ValueError: Invalid hex color code + + >>> hex_to_rgb("#0032") + Traceback (most recent call last): + ... + ValueError: Invalid hex color code + + Note: + - The function supports both 6-digit and 3-digit hex color codes. + """ + + hex_color = hex_color.lstrip("#") + + # Check if the input is a valid hex color code + if not (len(hex_color) == 6 or len(hex_color) == 3): + raise ValueError("Invalid hex color code") + + # Expand 3-digit hex codes to 6 digits (e.g., "#123" to "#112233") + if len(hex_color) == 3: + hex_color = "".join([char * 2 for char in hex_color]) + + # Parse the hex values to integers + red = int(hex_color[0:2], 16) + green = int(hex_color[2:4], 16) + blue = int(hex_color[4:6], 16) + + return f"rgb({red}, {green}, {blue})" + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + From a62b5e5c9784149c951f026e233c1e611b8cd078 Mon Sep 17 00:00:00 2001 From: cicerotcv Date: Mon, 30 Oct 2023 18:45:40 -0300 Subject: [PATCH 2/4] docs: adds module's author, description and references --- conversions/hex_to_rgb.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/conversions/hex_to_rgb.py b/conversions/hex_to_rgb.py index 3a6c5b457..4efcea206 100644 --- a/conversions/hex_to_rgb.py +++ b/conversions/hex_to_rgb.py @@ -1,3 +1,13 @@ +""" +* Author: Cicero Tiago Carneiro Valentim (https://github.com/cicerotcv) +* Description: Convert hexadecimal (#FF2000) color to RGB (rgb(255, 32, 0)). + +References: +https://www.w3schools.com/colors/colors_rgb.asp +https://www.w3schools.com/colors/colors_hexadecimal.asp +""" + + def hex_to_rgb(hex_color: str) -> str: """ Converts a hexadecimal color code to RGB values. From c680fecd54eb5d8e7b3d87b8117282253562a7be Mon Sep 17 00:00:00 2001 From: cicerotcv Date: Mon, 30 Oct 2023 19:14:23 -0300 Subject: [PATCH 3/4] chore: fix file trailing line --- conversions/hex_to_rgb.py | 1 - 1 file changed, 1 deletion(-) diff --git a/conversions/hex_to_rgb.py b/conversions/hex_to_rgb.py index 4efcea206..6d97acb4f 100644 --- a/conversions/hex_to_rgb.py +++ b/conversions/hex_to_rgb.py @@ -77,4 +77,3 @@ if __name__ == "__main__": import doctest doctest.testmod() - From 124fe8d629ade1b210c22329e24c378b8df84cd6 Mon Sep 17 00:00:00 2001 From: cicerotcv Date: Tue, 31 Oct 2023 09:59:12 -0300 Subject: [PATCH 4/4] feat: insert the algorithms into Directory.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 9b2c8ce73..3163f9dd8 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -149,6 +149,7 @@ * [Energy Conversions](conversions/energy_conversions.py) * [Excel Title To Column](conversions/excel_title_to_column.py) * [Hex To Bin](conversions/hex_to_bin.py) + * [Hex To Rgb](conversions/hex_to_rgb.py) * [Hexadecimal To Decimal](conversions/hexadecimal_to_decimal.py) * [Length Conversion](conversions/length_conversion.py) * [Molecular Chemistry](conversions/molecular_chemistry.py)