我希望能够调用一个采用基本颜色并返回对应于相同颜色的不同阴影的值的数组。该数组可以包含十六进制值或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字符串 –