I have a fixed width div with gradient applied using css. I want to build slider based color picker based on this gradient.
When i drag the slider i calculate the percentage position, and i want to get the hex or rgb color code based on this value.
My idea was to create an array with the start/stop positions and colors defined, then find two values from this array based on the slider position, then somehow find the color between: this is where i can't move forward.
Demo: http://jsfiddle.net/pdu8rpfv/
var gradient = [
[
0,
'ff0000'
],
[
28,
'008000'
],
[
72,
'0000ff'
],
[
100,
'ff0000'
]
];
$( "#slider" ).slider({
min: 1,
slide: function( event, ui ) {
var colorRange = []
$.each(gradient, function( index, value ) {
if(ui.value<=value[0]) {
colorRange = [index-1,index]
return false;
}
});
$('#result').css("background-color", 'red');
}
});
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…