var rgbToHex = function(rgb) { // rgb(x, y, z) var color = rgb.toString().match(/\\d+/g); // 把 x,y,z 推送到 color 数组里 var hex = "#"; for (var i = 0; i < 3; i++) { // 'Number.toString(16)' 是JS默认能实现转换成16进制数的方法. // 'color[i]' 是数组,要转换成字符串. // 如果结果是一位数,就在前面补零。例如: A变成0A hex += ("0" + Number(color[i]).toString(16)).slice(-2); } return hex; };
四:十进制—–>rgb颜色
方法一: functiongetColor(number) { const alpha = number > 24 & 0xff; const red = number > 16 & 0xff; const green = number > 8 & 0xff; const blue = number & 0xff; return`rgba(${red}, ${green}, ${blue}, ${alpha})`; } 方法二: functiongetColor(number) { let color = number; const blue = parseInt(color % 0x100, 10); color = color > 8; const green = parseInt(color % 0x100, 10); color = color > 8; const red = parseInt(color % 0x100, 10); const alpha = (parseInt(color > 8, 10) / 0xFF).toFixed(1); return`rgba(${red}, ${green}, ${blue}, ${alpha})`; }
五:hsl—–>rgb
/** * HSL颜色值转换为RGB. * 换算公式改编自 http://en.wikipedia.org/wiki/HSL_color_space. * h, s, 和 l 设定在 [0, 1] 之间 * 返回的 r, g, 和 b 在 [0, 255]之间 * * @param Number h 色相 * @param Number s 饱和度 * @param Number l 亮度 * @return Array RGB色值数值 */ functionhslToRgb(h, s, l) { var r, g, b; if(s == 0) { r = g = b = l; // achromatic } else { var hue2rgb = functionhue2rgb(p, q, t) { if(t < 0) t += 1; if(t > 1) t -= 1; if(t < 1/6) return p + (q - p) * 6 * t; if(t < 1/2) return q; if(t < 2/3) return p + (q - p) * (2/3 - t) * 6; return p; }
var q = l < 0.5 ? l * (1 + s) : l + s - l * s; var p = 2 * l - q; r = hue2rgb(p, q, h + 1/3); g = hue2rgb(p, q, h); b = hue2rgb(p, q, h - 1/3); }
/** * RGB 颜色值转换为 HSL. * 转换公式参考自 http://en.wikipedia.org/wiki/HSL_color_space. * r, g, 和 b 需要在 [0, 255] 范围内 * 返回的 h, s, 和 l 在 [0, 1] 之间 * * @param Number r 红色色值 * @param Number g 绿色色值 * @param Number b 蓝色色值 * @return Array HSL各值数组 */ functionrgbToHsl(r, g, b) { r /= 255, g /= 255, b /= 255; var max = Math.max(r, g, b), min = Math.min(r, g, b); var h, s, l = (max + min) / 2; if (max == min){ h = s = 0; // achromatic } else { var d = max - min; s = l > 0.5 ? d / (2 - max - min) : d / (max + min); switch(max) { case r: h = (g - b) / d + (g < b ? 6 : 0); break; case g: h = (b - r) / d + 2; break; case b: h = (r - g) / d + 4; break; } h /= 6; }
return [h, s, l]; }
七:rgb—->hsb/hsv
functionrgbToHsv(r, g, b) { r = r / 255; g = g / 255; b = b / 255; var h, s, v; var min = Math.min(r, g, b); var max = v = Math.max(r, g, b); var l = (min + max) / 2; var difference = max - min; if (max == min) { h = 0; } else { switch (max) { case r: h = (g - b) / difference + (g < b ? 6 : 0); break; case g: h = 2.0 + (b - r) / difference; break; case b: h = 4.0 + (r - g) / difference; break; } h = Math.round(h * 60); } if (max == 0) { s = 0; } else { s = 1 - min / max; } s = Math.round(s * 100); v = Math.round(v * 100); return [h, s, v]; }