【发布时间】:2026-01-15 04:00:02
【问题描述】:
我希望能够调用一个函数,该函数采用基色并返回对应于相同颜色的不同阴影的值数组。该数组可以包含十六进制值或 rgba() 值。我也希望能够输入所需阴影的数量。然后,阴影的数量也可以用作增加阴影的度量。例如,如果我希望输出具有 3 种不同的阴影,则第一个阴影将是底色的 1/3。但是数学会起作用...此外,在某些情况下,第一个阴影可能需要 100% 透明,所以我愿意接受初始 alpha 的参数。我已经组织了我认为该函数的基本逻辑,但数学对我来说还不清楚。
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
【问题讨论】:
-
你搜索了吗? *.com/questions/5560248/…
-
你能补充一些例子吗?
-
您也可以通过操作数字来简单地更改 hsl,并从中生成 CSS 字符串
标签: javascript colors