main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# Python3 program to convert the given # RGB color code to Hex color code # Function to convert decimal to hexadecimal 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" # Driver Code R = 0 G = 0 B = 0 print (convertRGBtoHex(R, G, B)) R = 255 G = 255 B = 255 print (convertRGBtoHex(R, G, B)) R = 25 G = 56 B = 123 print (convertRGBtoHex(R, G, B)) R = 2 G = 3 B = 4 print (convertRGBtoHex(R, G, B)) R = 255 G = 255 B = 256 print (convertRGBtoHex(R, G, B)) # This code is contributed by Pratik Basu |