conversions.hex_to_rgb

  • 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

Functions

hex_to_rgb(→ str)

Converts a hexadecimal color code to RGB values.

Module Contents

conversions.hex_to_rgb.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.