r1.addEventListener('change',(ev)=>{
let res = test(ev.target.value)
d2.innerText=ev.target.value +' color: '+res
d2.style.backgroundColor = res
})
function test(value){
let colorPicked = pickRgbRange(value,
{color:[255,0,228,1], position:0},
{color:[0,194,255,1,1], position:15},
{color:[35,200,0,1], position:35},
{color:[255, 250, 164], position:50},
{color:[255,0,0,1], position:75},
{color:[0,0,0,1], position:100}
);
let resultRgba = `rgba(${colorPicked[0]},${colorPicked[1]},${colorPicked[2]},${colorPicked[3]})`;
return resultRgba
}
//(ildarin cc0) Start copy from here: ----------------------------------
/** @description usage
let colorPickedRgba = pickRgbRange(value,
{color:[255,0,228,1], position:0},
{color:[0,194,255,1,0.5], position:.15},
{color:[35,200,0,1], position:.35},
{color:[255, 250, 164], position:.50},
{color:[255,0,0,1], position:.75},
{color:[0,0,0,1], position:.100}
)
let resultRgba = `rgba(${colorPicked[0]},${colorPicked[1]},${colorPicked[2]},${colorPicked[3]})`
*/
function pickRgbRange(position, ...elements) {
var [left, right, weight] = pickClosest(position, ...elements);
return pickRgba(left.color, right.color, weight);
}
function pickRgba(color1, color2, weight) {
var w1 = weight;
var w2 = 1 - w1;
var rgba = [
Math.round(color1[0] * w2 + color2[0] * w1),
Math.round(color1[1] * w2 + color2[1] * w1),
Math.round(color1[2] * w2 + color2[2] * w1),
1
];
return rgba;
}
function pickClosest(position, ...elements) {
var left = elements[0],
right = { color: [0, 0, 0], position: Number.MAX_VALUE };
var leftIndex = 0;
for (var i = 0; i < elements.length; i++) {
if (position >= elements[i].position && position > left.position){
left = elements[i];
leftIndex = i;
}
}
if (elements.length - 1 === leftIndex) {
right = elements[leftIndex];
}
else {
right = elements[leftIndex + 1];
}
if(left == right){
return [right, right, 0];
}
var dleft = position - left.position;
var sum = dleft + right.position - position;
var weight = dleft / sum;
return [left, right, weight];
}
#r1{
width:100%;
}
#d1,
#d2 {
width: 100%;
height: 50px;
}
#d1 {
background: linear-gradient(90deg,
rgb(255, 0, 228) 0%,
rgb(0, 194, 255) 15%,
rgb(35, 200, 0) 35%,
rgb(255, 250, 164) 50%,
rgb(255, 0, 0) 75%,
rgb(0, 0, 0) 100%);
}
#d2 {
text-shadow:0 0 4px #fff;
background-color: #ccc;
}
<div id='d1'></div>
<input id='r1' type='range' />
<div id='d2'></div>
getColor
function gets percent as input, so we should usegrad.getColor(95)
instead ofgrad.getColor(.95)
. – Scriabin