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
function decToHexa ( n ) { let hexaDeciNum = Array . from ( { length : 2 } , ( _ , i ) => 0 ) ; let i = 0 ; while ( n != 0 ) { let temp = 0 ; temp = n % 16 ; if ( temp < 10 ) { hexaDeciNum [ i ] = String . fromCharCode ( temp + 48 ) ; i ++ ; } else { hexaDeciNum [ i ] = String . fromCharCode ( temp + 55 ) ; i ++ ; } n = Math . floor ( n / 16 ) ; } let hexCode = " " ; if ( i == 2 ) { hexCode += hexaDeciNum [ 0 ] ; hexCode += hexaDeciNum [ 1 ] ; } else if ( i == 1 ) { hexCode = " " ; hexCode += hexaDeciNum [ 0 ] ; } else if ( i == 0 ) hexCode = " " ; return hexCode ; } function convertRGBtoHex ( R , G , B ) { if ( ( R >= 0 && R <= 255 ) && ( G >= 0 && G <= 255 ) && ( B >= 0 && B <= 255 ) ) { let hexCode = " " ; hexCode += decToHexa ( R ) ; hexCode += decToHexa ( G ) ; hexCode += decToHexa ( B ) ; return hexCode ; } else return " " ; } let R = 0 , G = 0 , B = 0 ; document . write ( convertRGBtoHex ( R , G , B ) + " " ) ; R = 255 ; G = 255 ; B = 255 ; document . write ( convertRGBtoHex ( R , G , B ) + " " ) ; R = 25 ; G = 56 ; B = 123 ; document . write ( convertRGBtoHex ( R , G , B ) + " " ) ; R = 2 ; G = 3 ; B = 4 ; document . write ( convertRGBtoHex ( R , G , B ) + " " ) ; R = 255 ; G = 255 ; B = 256 ; document . write ( convertRGBtoHex ( R , G , B ) + " " ) ;