(JS) Convert short color hex to pair


If you have a three digit hex for a color. For example #FFF and want to convert it to a 6 digit number here’s a simple way:

// Expects 3 digit hex, like '#FFF';
function uniformColorHex(hex) {

var newHex = '#' +
hex.charAt(1) +
hex.charAt(1) +
hex.charAt(2) +
hex.charAt(2) +
hex.charAt(3) +
hex.charAt(3);

console.log(hex + ' => ' + newHex);
return newHex;
}
JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *