# Convert RGB to hex color code in Python

In this article, we are going to learn about how to convert RGB to hex color code in python. This article is mainly for the conversion of RGB to hex color code but we will also see the reverse of this how-to convert hex color code to RGB in python. Before moving further we need to understand that what is RGB and hex color.

## Difference between RGB and hex color

RGB color:- In RGB color R stands for Red, G stands for Green, and B stands for Blue, and it ranges from the decimal value of 0 – 255. Therefore for every set of color have 256 combinations of color. So, the total possible value of color available is (256 x 256 x 256) i.e. 16,777,216. Example:- (255,0,0) –> this color is red.

Hex color:- A hex color code is a unique way to express color using the hexadecimal values. The code is written in a hex triplet, which represents each value that specifies the components of the colors. The code always starts with a hashtag sign (#) and after this sign, six hex value or three hex value pair is written. Example:- #bab7c8

## Conversion of RGB to hex and vice-versa in Python

There are many methods available for the conversion of RGB to hex and vice-versa. Let’s understand with some examples:-

• Simple code without using any module

RGB to Hex

```def rgb_to_hex(rgb):
return '%02x%02x%02x' % rgb
rgb_to_hex((255, 255, 195))```

Output:- ‘ffffc3

In the above example, we created a function and passed the RGB value as an argument inside it and we converted RGB into Hex using the string conversion.

Hex to RGB

```def hex_to_rgb(value):
value = value.lstrip('#')
lv = len(value)
return tuple(int(value[i:i+lv//3], 16) for i in range(0, lv, lv//3))
hex_to_rgb("FF65BA")```

Output:- (255, 102, 186)

In the above example, we created the function to convert Hex to RGB and passed the string inside it and then converted it to the tuple of integers.

• Conversion of RGB to Hex and vice-versa using matplotlib module
```import matplotlib

print(matplotlib.colors.to_hex([ 0.47, 0.0, 1.0 ]))
print(matplotlib.colors.to_hex([ 0.7, 0.321, 0.3, 0.5 ], keep_alpha=True))

print(matplotlib.colors.to_rgb("#aabbcc"))
print(matplotlib.colors.to_rgb("#ddee9f"))```

Output:-

```#7800ff
#b2524c80
(0.6666666666666666, 0.7333333333333333, 0.8)
(0.8666666666666667, 0.9333333333333333, 0.6235294117647059)

```

In this example, we imported the required module i.e. matplotlib and then used the function “colors.to_hex” and “colors.to_rgb” and passed the required value inside each function. In one of the examples, we passed four arguments inside the function colors.to_hex, the fourth argument is for the opacity of the color. Opacity varies from 0 to 1.

### 3 responses to “Convert RGB to hex color code in Python”

1. safdf says:

def rgb_to_hex(rgb):
return ‘%02x%02x%02x’ % rgb
rgb_to_hex((255, 255, 195))

this code is not right. what is rgb? is it r*b*g? or merged or what?

• Farhan Hai Khan says:

#it works!!!
def rgb_to_hex(rgb):
r,g,b=rgb
return ‘#%02x%02x%02x’ % (r,g,b)
print(rgb_to_hex((0, 34, 255)))

2. Can't blame a gal for tryin' says:

Test comment. A minimum of 40 characters is required, so, let’s try to fulfill that.