0
Q:

rgb to hex

function rgbToHex(r, g, b) {
  return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}

function hexToRgb(hex) {
  var result = /^#?([a-fd]{2})([a-fd]{2})([a-fd]{2})$/i.exec(hex);
  if(result){
      var r= parseInt(result[1], 16);
      var g= parseInt(result[2], 16);
      var b= parseInt(result[3], 16);
      return r+","+g+","+b;//return 23,14,45 -> reformat if needed 
  } 
  return null;
}
console.log(rgbToHex(10, 54, 120)); //#0a3678
console.log(hexToRgb("#0a3678"));//"10,54,120"
4

<script>
    function parseAndGetHex(s){
    var a = s.split("(")[1].split(")")[0];
        a = a.split(",");
        return rgbToHex(parseInt(a[0]),parseInt(a[1]),parseInt(a[2]));
    }

    function rgbToHex(r, g, b) {
      return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
    }

      document.getElementById("rgb_input").addEventListener('change',function(){
		 document.getElementById("hex_output").value=parseAndGetHex(this.value);
      });

</script>

<input id="rgb_input" type="text" placeholder="rgb(255,255,255)">
<input id="hex_output" type="text" placeholder="">
-1

New to Communities?

Join the community