RGB to Hex Conversion and Hex to RGB Conversion in python


Posted on 22 April 2017

In CSS we use three ways to specify the color in the color property, first one using the name second using the hex values and third using the rgb values, now what basically these rgb and hex values are, let's see.

Recommended Read: Steganography Tutorial-Art of Hiding Data into Images

RGB Values

The RGB color model is an additive color model in which red, green and blue light are added together in various ways to reproduce a broad array of colors. The name of the model comes from the initials of the three additive primary colors, red, green and blue.(Source)

Hex Values

A hex triplet is a six-digit, three-byte hexadecimal number used in HTML, CSS, SVG, and other computing applications to represent colors. The bytes represent the red, green and blue components of the color. One byte represents a number in the range 00 to FF (in hexadecimal notation), or 0 to 255 in decimal notation. This represents the least (0) to the most (255) intensity of each of the color components. Thus web colors specify colors in the True Color (24-bit RGB) color scheme. The hex triplet is formed by concatenating three bytes in hexadecimal notation, in the following order:

Byte 1: red value (color type red)
Byte 2: green value (color type green)
Byte 3: blue value (color type blue) (Source)

let's see an example on both of them

 color:#ffffff  /*hex value*/
 background:rgb(255, 255, 255) /*rgb value*/

Now we know what rgb and hex are and dont worry this article in not on CSS tongue-out. In this article we will learn how to convert rgb to hex and hex to rgb by writing a python code for it.

For this we are going to create two python functions

  • hex2rgb() : To convert rgb to hex
  • rgb2hex() : To convert hex to rgb

Let's create our first function

def rgb2hex(r,g,b):
    hex = "#{:02x}{:02x}{:02x}".format(r,g,b)
    return hex

In the above function we are taking three parameters r, g, b and formatting each of the value into hexadecimal format and adding a '#' before all the three values and storing the string into a variable hex and then the function returns the hex.

def hex2rgb(hexcode):
    rgb = tuple(map(ord,hexcode[1:].decode('hex')))
    return rgb

In the above function maybe the second line is a bit confusing for you guys but let me explain it,  we are map() function, map basically implements a certain function to all the items present in a list, in the above map() function we are applying the ord() function to all the items, the ord() function takes a string and returns an int representing the unicode code point of the character. The tuple() is used to represent the data in the form of a tuple.

Now implement these functions and write a full code for our RGB2HEX convertor

def rgb2hex(r,g,b):
    hex = "#{:02x}{:02x}{:02x}".format(r,g,b)
    return hex
def hex2rgb(hexcode):
    rgb = tuple(map(ord,hexcode[1:].decode('hex')))
    return rgb

r = int(input("Enter r: "))
g = int(input("Enter g: "))
b = int(input("Enter b: "))
rgb = (r,g,b)
hexvalue = rgb2hex(rgb[0],rgb[1],rgb[2])

hexcode = raw_input("Enter a hex value: ")
rgbvalue = hex2rgb(hexcode)


rgb to hex conversion in python

Thank you for reading.

Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook page. You can also comment your questions below.

Also, don't forget to subscribe to our Newsletter.

If you like this article, then please share it and help us grow.

If You Love this article, You Should Consider:

  • Like us on Facebook
  • Follow us on Instagram
  • Follow us on Twitter
  • Subscribe to our Newsletter.
  • Let us know your suggestions and queries in the comments below.

Thank you for your Love and Support

Share your thoughts