原文出处:https://zhidao.baidu.com/question/710988923723598245.html
代码(有改良)
// 计算反色, ilighten - 减弱对比度(-1 ~ -15)
// 示例: oppositeColor("#000000", -4); 返回: #bbbbbb
function oppositeColor(a, ilighten) {
a = a.replace('#', '');
//var max16 = 15;
var max16 = Math.floor(15 + (ilighten || 0));
if (max16 < 0 || max16 > 15) max16 = 15;
var c16, c10, b = [];
for (var i = 0; i < a.length; i++) {
c16 = parseInt(a.charAt(i), 16); // to 16进制
c10 = parseInt(max16 - c16, 10); // 10进制计算
if (c10 < 0) c10 = Math.abs(c10);
b.push(c10.toString(16)); // to 16进制
}
return '#' + b.join('');
}
效果图: