Nothing groundbreaking but-

A function to convert RGB values to Hex

function rgbToHex(r, g, b) { var rgb = b | (g << 8) | (r << 16); return "#" + (0x1000000 + rgb).toString(16).slice(1); }
Code language: JavaScript (javascript)

And a function that does the opposite, Hex to RGB

function hexToRgb(hex) { var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; hex = hex.replace(shorthandRegex, function (m, r, g, b) { return r + r + g + g + b + b; }); 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; }
Code language: JavaScript (javascript)

Not sure with whom to credit these but I’ve found myself needing to look them up enough that they’re now here for safe keeping

Tags:

No Responses

Leave a Reply

Your email address will not be published.