JavaScript-补色生成器
我们需要编写一个以十六进制颜色作为唯一输入的JavaScript函数。
然后,我们的函数应找到作为输入的颜色的互补色。
这是一些输入和输出对-
getComplementaryColor('#142814') = '#ebd7eb';
getComplementaryColor('#ffffff') = '#000000';
getComplementaryColor('#3399ff') = '#cc6600';示例
为此的代码将是-
const str1 = '#142814';
const str2 = '#ffffff';
const str3 = '#3399ff';
const getComplementaryColor = (color = '') => {
const colorPart = color.slice(1);
const ind = parseInt(colorPart, 16);
let iter = ((1 << 4 * colorPart.length) - 1 - ind).toString(16);
while (iter.length < colorPart.length) {
iter = '0' + iter;
};
return '#' + iter;
};
console.log(getComplementaryColor(str1));
console.log(getComplementaryColor(str2));
console.log(getComplementaryColor(str3));输出结果
控制台中的输出将是-
#ebd7eb #000000 #cc6600
热门推荐
10 回复姐姐生日祝福语简短
11 女友妈妈生日祝福语简短
12 靳年祝福语大全简短
13 离别赠言离职祝福语简短
14 升职祝福语简短霸气押韵
15 开工红包祝福语毕业简短
16 友情祝福语简短小故事
17 小班小孩生日祝福语简短
18 女孩生日祝福语简短的话