Hexadecimal (hex) color codes are a way to represent colors in a format that uses a combination of letters and numbers. Each color is represented by a six-digit code that consists of three pairs of two characters each. These pairs represent the intensity of red, green, and blue components of the color. Here's how the hex code works:
The first two characters represent the red component.
The next two characters represent the green component.
The last two characters represent the blue component.
For example:
The hex code #FF0000 represents pure red (maximum red, no green, no blue).
The hex code #00FF00 represents pure green (no red, maximum green, no blue).
The hex code #0000FF represents pure blue (no red, no green, maximum blue).
From HEX code to RGB ( Red Green Blue ) values.
The commented part can be used to get the return value as Tuple for further processing.
def hex_to_rgb(hex):
#rgb = []
str1='RGB('
for i in (0, 2, 4):
#decimal = int(hex[i:i+2], 16)
str1=str1+str(int(hex[i:i+2],16))+','
#rgb.append(decimal)
#return tuple(rgb)
str1=str1.rstrip(',')+")"
return str1
print(hex_to_rgb('#FF65AA'.lstrip('#'))) # Output RGB(255,101,170)
Key Points About hex() Function
Output Format: The result is a string that represents the hexadecimal value, prefixed with "0x".
Negative Integers: The hex() function also works with negative integers, returning the hexadecimal representation of the two's complement of the given number.
Non-Integer Inputs: If the input to hex() is not an integer, a TypeError will be raised, emphasizing the need for integer inputs.