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