RGB 16진수 함수


hexToRgb(16진수)

function hexToRgb(hex) {
  var result = /^#?((a-f\d){2})((a-f\d){2})((a-f\d){2})$/i.exec(hex);
  return result ? {
    r: parseInt(result(1), 16),
    g: parseInt(result(2), 16),
    b: parseInt(result(3), 16)
  } : null;
}

//

console.log(hexToRgb("#0033ff")); 	// {"r": 0, "g": 51, "b":255}
console.log(hexToRgb("#0033ff").r);	// 0
console.log(hexToRgb("#0033ff").g);	// 51
console.log(hexToRgb("#0033ff").b);	// 255

rgbToHex(r,g,b)

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

//

console.log(rgbToHex(0, 51, 255);	// #0033ff