; quixoto하고 자세한 설명과 답변을 패트릭 DW 모두
감사합니다. 그러나 나는 약간 다른 해결책을 가지고있다. 1 차원 배열을 사용하는 초기 접근법을 유지하려고했지만 아래에서 볼 수 있듯이 다른 차원을 추가하는 것이 훨씬 쉽고 효율적입니다. 여기서 사용 된 배열은 예측 가능성을 나타내지 만 프로젝트가 완성되면 그럴 수 없습니다.
var colors = [
[0, '121D4A'],
[10800, '000000'],
[21600, 'FF5900'],
[32400, 'D3EEF0'],
[43200, '7DCDFF'],
[54000, '7DA6FF'],
[64800, 'FF5900'],
[75600, '31428C'],
[86399, '121D4A'],
];
function gradientStop(color1, color2, gradStop){
var r = Math.floor(gradStop * parseInt(color2.substr(0, 2), 16) + (1 - gradStop) * parseInt(color1.substr(0, 2), 16)).toString(16);
var g = Math.floor(gradStop * parseInt(color2.substr(2, 2), 16) + (1 - gradStop) * parseInt(color1.substr(2, 2), 16)).toString(16);
var b = Math.floor(gradStop * parseInt(color2.substr(4, 2), 16) + (1 - gradStop) * parseInt(color1.substr(4, 2), 16)).toString(16);
return (r.length < 2 ? '0' + r : r) + (g.length < 2 ? '0' + g : g) + (b.length < 2 ? '0' + b : b);
}
function getColor(colors, currentIndex){
for(var i = 0, m = colors.length; i < m; i++){
if(currentIndex >= colors[i][0]){
if(typeof(colors[i + 1]) !== 'undefined'){
if(currentIndex <= colors[i + 1][0]){
return gradientStop(colors[i][1], colors[i + 1][1], (currentIndex - colors[i][0])/(colors[i + 1][0] - colors[i][0]));
}
}
}
}
}
그리고 또한; 예 독 당점 그것은 나의 질문에 Programmatic gradient stops with Javascript, 따라서 gradientStop()
기능에 확장했다.
연관 배열이란? 'myarray [ '0'] = 0x0000FF;'? – stef
개체로 쉽게 될 것입니다. 배열을 사용하면 훨씬 어려워집니다. –
@Tomcat : 배열 인덱스에 예측 가능한 순서가 있습니까? – user113716