Convert RGB Color to Hex Code in Python

In this tutorial, we will introduce the way to convert a rgb color value to hex code using python.

Here is an example:

RGB(68, 68, 68) will be converted to #444444

We will create a python function to convert rgb color to hex code.

def decToHexa(n):  
  
    # char array to store hexadecimal number  
    hexaDeciNum = ['0'] * 100
  
    # Counter for hexadecimal number array  
    i = 0
      
    while (n != 0):  
  
        # Temporary variable to store remainder  
        temp = 0
  
        # Storing remainder in temp variable.  
        temp = n % 16
  
        # Check if temp < 10  
        if (temp < 10):  
            hexaDeciNum[i] = chr(temp + 48) 
            i = i + 1
  
        else:  
            hexaDeciNum[i] = chr(temp + 55) 
            i = i + 1
  
        n = int(n / 16) 
  
    hexCode = "" 
    if (i == 2): 
        hexCode = hexCode + hexaDeciNum[0]  
        hexCode = hexCode + hexaDeciNum[1]  
  
    elif (i == 1):  
        hexCode = "0"
        hexCode = hexCode + hexaDeciNum[0] 
  
    elif (i == 0): 
        hexCode = "00"
  
    # Return the equivalent  
    # hexadecimal color code  
    return hexCode 
  
# Function to convert the  
# RGB code to Hex color code  
def convertRGBtoHex(R, G, B):  
  
    if ((R >= 0 and R <= 255) and
        (G >= 0 and G <= 255) and
        (B >= 0 and B <= 255)):  
  
        hexCode = "#";  
        hexCode = hexCode + decToHexa(R) 
        hexCode = hexCode + decToHexa(G)  
        hexCode = hexCode + decToHexa(B)  
        return hexCode 
  
    # The hex color code doesn't exist  
    else: 
        return "-1"

Then you can use this function to convert.

print(convertRGBtoHex(R=68, G=68, B=68))

The result will be: #444444