色彩随机线性变换
程序员文章站
2023-12-25 22:46:51
...
有时候做一些颜色随机,需要变化,但是跨度太大不好看,最好有一个过度,就是改变一个色值,达成一个循环,这样变化看起来比较柔和.
//获取一个随机颜色位置
public static float GetLineColorSeed(float colorMin = 0f, float colorMax = 1f)
{
float range = colorMax - colorMin;
float randomrange = range * 6f;
return UnityEngine.Random.Range(0, randomrange);
}
//循环变色,colorSeed是颜色的随机基位置,step是每个周期的位置
public static Color GetLineColor(float colorSeed, float alpha, float step ,float colorMin = 0f, float colorMax = 1f)
{
float range = colorMax - colorMin;
float randomrange = range * 6f;
float nowC = (colorSeed + step) % randomrange;
Color color;
color.a = alpha;
if (nowC < range)
{
//R 变大
color.r = nowC % range;
color.g = colorMin;
color.b = colorMax;
}
else if (nowC < range * 2)
{
//B 变小
color.r = colorMax;
color.g = colorMin;
color.b = colorMax - nowC % range;
}
else if (nowC < range * 3)
{
//G 变大
color.r = colorMax;
color.g = colorMin + nowC % range;
color.b = colorMin;
}
else if (nowC < range * 4)
{
//R 变小
color.r = colorMax - nowC % range;
color.g = colorMax;
color.b = colorMin;
}
else if (nowC < range * 5)
{
//B 变大
color.r = colorMin;
color.g = colorMax;
color.b = colorMin + nowC % range; ;
}
else
{
//G 变小
color.r = colorMin;
color.g = colorMax - nowC % range;
color.b = colorMax;
}
//Debug.Log( color.ToString());
return color;
}
调用
//先随机一个颜色.
float colorSeed = Fun.GetLineColorSeed(0f, 0.7f);
colorStep每次增加,来改变颜色,这样过度比较平滑.
Color corFrom = Fun.GetLineColor(colorSeed, 1f, colorStep, 0f, 0.7f);
colorStep += 0.01f;