Мне нужна помощь, чтобы найти, почему этот алгоритм Darkness/Lightness Javascript hex Цвет в некоторых случаях проваливается, пожалуйста, запустите фрагмент, я хотел бы понять, почему его отказ больше, чем решение , потому что я буду использовать новый algorthim с hls color +%.Улучшить алгоритм Тьмы/Легкости Javascript hex Цвет
function similarColor(hex, percent) {
console.log(hex);
\t \t
var a = parseInt(hex.substring(1,3),16);
var b = parseInt(hex.substring(3,5),16);
var c = parseInt(hex.substring(5,6),16);
\t \t \t \t
a = parseInt(a * (100 + percent)/100);
b = parseInt(b * (100 + percent)/100);
c = parseInt(c * (100 + percent)/100); \t \t \t \t
\t \t \t \t \t \t \t \t \t
a = (a<255)?a:255;
b = (b<255)?b:255;
c = (c<255)?c:255;
var aa = (a.toString(16).length==1)?"0"+a.toString(16):a.toString(16);
var bb = (b.toString(16).length==1)?"0"+b.toString(16):b.toString(16);
var cc = (c.toString(16).length==1)?"0"+c.toString(16):c.toString(16);
\t \t \t \t
return "#"+aa+bb+cc;
}
//Error
document.getElementById('errorColor').style.backgroundColor=similarColor('#074E7C', 50);
//Well working
document.getElementById('newColor2').style.backgroundColor=similarColor('#2A7C2A', 50);
document.getElementById('newColor3').style.backgroundColor=similarColor('#FC1479', 50);
document.getElementById('newColor4').style.backgroundColor=similarColor('#FCFC6F', 50);
Error<br>
<div style="background-color:#074E7C">#074E7C</div>
<div id="errorColor">lightness</div>
<hr>
Correct<br>
<div style="background-color:#2A7C2A">original</div>
<div id="newColor2">lightness</div>
<hr>
Looks bad<br>
<div style="background-color:#FC1479">original</div>
<div id="newColor3">lightness</div>
<hr>
Correct<br>
<div style="background-color:#FCFC6F">original</div>
<div id="newColor4">lightness</div>
<hr>
Каким должен быть алгоритм? что не так с тем, что у вас есть? – joews
Может ли это быть var c = parseInt (hex.substring (5,7), 16); ? – b2238488
@ b2238488 да, мой плохой, спасибо jamesjara