2016-10-15 4 views
1

JavaScriptで色を補完しようとしていますが、必要なものが見つかりません。私はhereを見ましたが、#000000#142814のような値の場合、彼らは自分自身を返します。最初の価値については、#FFFFFFを得るのが理にかなっているように感じます。このように機能する関数はありますか?JavaScript補完的な色

答えて

0

あなたは2 からの差異を使用することができます - 2 の違い1と色の値と短いの色に対して - 1と色の値。

function getComplementaryColor(color) { 
 
    var c = color.slice(1), 
 
     i = parseInt(c, 16), 
 
     v = ((1 << 4 * c.length) - 1 - i).toString(16); 
 

 
    while (v.length < c.length) { 
 
     v = '0' + v; 
 
    } 
 
    return '#' + v; 
 
} 
 

 
console.log(getComplementaryColor('#142814')); 
 
console.log(getComplementaryColor('#000000')); 
 
console.log(getComplementaryColor('#111111')); 
 
console.log(getComplementaryColor('#ffffff')); 
 
console.log(getComplementaryColor('#39f')); 
 
console.log(getComplementaryColor('#000')); 
 
console.log(getComplementaryColor('#111')); 
 
console.log(getComplementaryColor('#fff'));
.as-console-wrapper { max-height: 100% !important; top: 0; }