我希望能夠調用一個採用基本顏色並返回對應於相同顏色的不同陰影的值的數組。該數組可以包含十六進制值或rgba()值。我希望能夠輸入所需的色調數量。陰影的數量也可以用作度量來增加陰影。例如,如果我希望輸出具有3種不同的色調,第一種色調將是基準的1/3。但是,這個數學運算將會起作用...此外,在某些情況下,第一種色調可能需要100%透明,所以我想要接受初始alpha的一個參數。我已經組織了我認爲是該功能的基本邏輯,但數學對我來說還不清楚。Javascript - 生成相同顏色的不同陰影
var buildColorStops = function (baseColor,numberOfValues,initialAlpha) {
var b = baseColor;
var n = numberOfValues //expected number of colors in the output. If n was 3 results would be [light blue, blue(base), dark blue] (# or rgba will work)
var d = 1/n; // if number of values was 5 d would represent 1/5 of the base.
var ia = initialAlpha; // some situations may require the first color to be 100% transparent
var outputArray = [];
var i = 0;
while (i < n) {
if (i == 0) {
//...math on base color & incorporate initial alpha
outputArray.push("result of math on base")
}
else {
//...math on base color by incrementing d or 1/n
outputArray.push("result of math on base")
}
}
return outputArray;
}// end of buildColorStops
您是否搜索? http://stackoverflow.com/questions/5560248/programmatically-lighten-or-darken-a-hex-color-or-rgb-and-blend-colors – epascarello
你可以添加一些例子嗎? –
您也可以簡單地通過操縱數字來改變hsl,並從中生成CSS字符串 –