Jon*_*eet 12
这听起来像是在线性插值之后 - 在您的情况下,从白色插值到指定的颜色.例如,在C#中:
public IEnumerable<Color> Interpolate(Color from, Color to, int steps)
{
int range = steps-1; // Makes things a bit easier
for (int i=0; i < steps; i++)
{
// i is the proportion of the "to" colour to use.
// j is the proportion of the "from" colour to use.
int j = range - i;
int r = ((from.R * j) + (to.R * i)) / range;
int g = ((from.G * j) + (to.G * i)) / range;
int b = ((from.B * j) + (to.B * i)) / range;
yield return new Color(r, g, b);
}
}
Run Code Online (Sandbox Code Playgroud)
当然,除了线性插值之外,还有其他方法可以做到这一点,但这可能是最简单的.请注意,如果您有很多步骤或更大的值,这会变得棘手,因为您需要考虑溢出的可能性.在这种情况下你应该没问题 - 你不太可能需要超过256步,最大值是255,所以你不会接近ints 的限制.
编辑:如评论中所述,RGB可能不是使用线性插值的最佳域.您可能最好将from/to RGB值转换为HSL或HSV,并对其进行插值.根据您的平台,这可能很容易或很棘手.如果没有为您提供适当的计算,则上一句中的维基百科链接会给出公式.