Skip to content
Advertisement

HTML5 input type Color read single RGB values

With this code on browser many field are available by the user, it can change R,G,B, HEX VALUE, HUE ecc. I need to read only the Red value.

  <input id="color_pick"type="color" value="#ff0000">

var toread = document.getElementById('color_pick');
toread.value # get the hex 
toread.value.red() # would it be possible to get r?

I’ve read this document but cannot figure how to get the single R value from the input.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input/color

Advertisement

Answer

Since you already have hexadecimal from node.value property, you just have to convert it to integer.

function pickRedInt(){
  var toread = document.getElementById('color_pick');
  console.log("Red Value - "+parseInt("0x"+toread.value.slice(1,3)));
}

pickRedInt();
Try changing this:
<hr>
<input id="color_pick"type="color" value="#ff0000" onchange="pickRedInt()">
7 People found this is helpful
Advertisement