How to Convert HEX to RGB and HSV in Python

3 Best Methods On How to Convert HEX to RGB and HSV in Python

This article is about How to Convert HEX to RGB and HSV in Python.

When you want to work with color in a programing language, you need to know how to refer to different color values using the same name.

In other words, what if you had thousands of RGB values but only needed a few HSV ones? Enter the HEX-RGB-HSV (or HSV) conversion function in Python.

This simple function allows you to take an RGB value and convert it into an HSV one.

In this article, I’ll show you different ways to convert HEX to RGB and HSV values in your Python code.

How to Convert HEX to RGB and HSV in Python

To convert HEX to RGB and HSV in Python use the user-defined function. Here is an example:

def hex_to_rgb(hex_value):
h = hex_value.strip(“#”)
convertsTo_rgb = tuple(int(h[i:i+2], 16) for i in (0, 2, 4))
return convertsTo_rgb
print(hex_to_rgb(“#180C27”))
print(hex_to_rgb((“#F51930”)))

What is a HEX value in Python?

A HEX value is a numeric representation of a hexadecimal number. The HEX string is a way of expressing the number in decimal notation.

It is used to represent numbers that are not exactly 16 digits long. It also means numbers that are not exactly 2 digits long.

The hexadecimal number is used in many applications such as cryptography, data storage, and computer programming. It is also used in some operating systems to represent file names and other data.

The hexadecimal number is a base-16 number system. It has six digits, each representing 16 binary digits. The first digit represents the power of 10, the second digit represents the power of 100, and so on. The last three digits represent the power of 1000.

What is an RGB value in Python?

The RGB color model is a system of color representation that was developed in the early 20th century.

The RGB color model uses three primary colors (red, green, and blue) to represent different shades of color.

Each primary color has a specific brightness and saturation, which are represented by a numerical value. The RGB color model is commonly used to create images and printouts that are easy to read and understand.

What is a HSV value in Python?

HSV – (hue, saturation, value), also known as HSB (hue, saturation, brightness), is often used by artists because it is often more natural to think about a color in terms of hue and saturation than in terms of additive or subtractive color components.

HSV is a transformation of an RGB colorspace, and its features and colorimetry are relative to the RGB colorspace from which it was derived.

Converting HEX to RGB in Python – 3 Best Methods

1. Using the user-defined function to Convert HEX to RGB in Python

To convert HEX color code into RGB, you can create a user-defined function in Python. As you know, a HEX string value comes with a ‘#’ sign in front of the color code.

Firstly, remove the hex sign. Then again, slice the resulting HEX string into three equal parts – the three substrings matching the three channels on the RGB color scheme.

Finally, get the hexadecimal representation of those substrings. An example is given below.

def hex_to_rgb(hex_value):
h = hex_value.strip("#")
convertsTo_rgb = tuple(int(h[i:i+2], 16) for i in (0, 2, 4))
return convertsTo_rgb
print(hex_to_rgb("#180C27"))
print(hex_to_rgb(("#F51930")))

Output:

(24, 12, 39)

(245, 25, 48)

2. Using the matplotlib Library to Convert HEX to RGB in Python

Matplotlib is a plotting library for Python, which is famous for its interactive visualization uses.

It provides a handy function named colors(), which gives ease to this problem. The colors() function explicitly converts a HEX string into an RGB color code.

from matplotlib import colors
convertsTo_rgb = tuple(i*255 for i in colors.to_rgb("#180C27"))
print(convertsTo_rgb)

Output:

(24, 12, 39)

3. Using the pillow Library to Convert HEX to RGB in Python

The Python Pillow library is an updated version of an older library called PIL. PIL stands for Python Imaging Library.

This library was designed to deal with images in Python. However, you can use the PIL library to convert your HEX string into RGB with the help of the ImageColor() function.

The ImageColor() function takes two parameters. The first parameter is the color code value, and the second one is the mode.

from PIL import ImageColor
convertsTo_rgb = ImageColor.getcolor("#180C27", "RGB")
print(rgb)

Output:

(24, 12, 39)

Converting RGB to HSV in Python

RGB to HSV conversion could be a long process in Python.

Once you understand the definition of the color code and their conversion style, it will be easier for you. Let me help you by splitting up the steps for you:

Step1: Neutralize the r, g, b values.

Step2: Calculate the max, min, and the difference of (r,g,b)

Step3: Figure out the H value

Step4: Figure out the S value

Step5: Figure out the V value

def rgbTo_hsv(r, g, b):

    r, g, b = r / 255.0, g / 255.0, b / 255.0

    max_rgb = max(r, g, b)

    min_rgb = min(r, g, b)

    diff = max_rgb-min_rgb

    if max_rgb == min_rgb:

    h = 0

    elif max_rgb == r:

    h = (60 * ((g - b) / diff) + 360) % 360

    elif max_rgb == g:

    h = (60 * ((b - r) / diff) + 120) % 360

    elif max_rgb == b:

    h = (60 * ((r - g) / diff) + 240) % 360

    if max_rgb == 0:

    s = 0

    else:

    s = (diff / max_rgb) * 100

    v = max_rgb * 100

    return tuple(map(round, (h, s, v)))

print(rgbTo_hsv(245, 25, 48))

Output:

(354, 90, 96)