rgbToHex

371 阅读1分钟

function componentToHex(c) {
    const hex = c.toString(16);
    return hex.length === 1 ? `0${hex}` : hex;
}
// RGB 转换为16进制
function rgbToHex(r, g, b) {
    return `#${componentToHex(r)}${componentToHex(g)}${componentToHex(b)}`.toLocaleUpperCase();
}

alert(rgbToHex(0, 51, 255)); // #0033ff

stackoverflow.com/questions/5…