Converting a Hex Code Variable to RGB Values

Hi all!

Could anyone suggest a way to have a variable (in this case a hex code) converted into a string of r, g, b values that can be used in scripts?

I'm working on a cosmetic feature for my game where the player is able to pick out the colors of different features of their character bust picture.
I'm managing this with a combination of layered grayscale pictures and the default $gameScreen.tintPicture script. I've been able to make a simple little swatch system, where I have a preselected bunch of colors defined by variables that the player can pick from.

I wish to give the player the ability to put in their own hex code. I could technically just let them insert RGB values, but that's cumbersome and unintuitive and would rather avoid that.

Any direction would be hugely appreciated!
There is a trick I'm using, it coverts a color string into an array of 4 in the order of [r,g,b,a]
JavaScript:
//used to get color value
var pix = document.createElement('canvas');
pix.willReadFrequently = true;
pix.width=1;pix.height=1;
var pctx = pix.getContext('2d',{willReadFrequently:true});
function getRgba(color){
  pctx.fillStyle = color;
  pctx.clearRect(0,0,1,1);
  pctx.fillRect(0,0,1,1);
  return pctx.getImageData(0,0,1,1).data;
}
I've used this method I found on stack overflow for years now, it works good. I can't find the original post on stack overflow anymore though but I have seen many people use this method quite often.


JavaScript:
function hexToRgb (hex) {
  const integer = parseInt(hex, 16)
  const r = (integer >> 16) & 255
  const g = (integer >> 8) & 255
  const b = integer & 255

  return r + ',' + g + ',' + b
}

With all that said, PIXI also has it's own hex2rgb method but it only takes a a number the calssic 0xffffff type of hex and not the string hex. But it's easy enough to account for.

JavaScript:
const hexFromUser = 'ffffff'// instruct user to not include the #, otherwise you need to remove it

const rgbValue = PIXI.utils.hex2rgb(parseInt(hexFromUser, 16)); // returns [1, 1, 1]

// convert to a string

rgbValue.toString()

// OR

const rgbValue = PIXI.utils.hex2rgb(parseInt(hexFromUser, 16)).toString();
I've used this method I found on stack overflow for years now, it works good. I can't find the original post on stack overflow anymore though but I have seen many people use this method quite often.


JavaScript:
function hexToRgb (hex) {
  const integer = parseInt(hex, 16)
  const r = (integer >> 16) & 255
  const g = (integer >> 8) & 255
  const b = integer & 255

  return r + ',' + g + ',' + b
}

With all that said, PIXI also has it's own hex2rgb method but it only takes a a number the calssic 0xffffff type of hex and not the string hex. But it's easy enough to account for.

JavaScript:
const hexFromUser = 'ffffff'// instruct user to not include the #, otherwise you need to remove it

const rgbValue = PIXI.utils.hex2rgb(parseInt(hexFromUser, 16)); // returns [1, 1, 1]

// convert to a string

rgbValue.toString()

// OR

const rgbValue = PIXI.utils.hex2rgb(parseInt(hexFromUser, 16)).toString();
This seems to be exactly what I'm looking for. Sorry in advance cause this'll seem like such a beginner question but how would you recommend I approach this in actual script/eventing inside the engine, like say a $gameVariable's value.

EDIT: Specifically pixi.js's number hex string.
(either way thanks this has given me a north to follow!)

Original thread: https://forums.rpgmakerweb.com/threads/converting-a-hex-code-variable-to-rgb-values.171395/