所以我试图通过向RGB添加不同的值并将标签设置为这些值来使这个文本标签具有非常平滑的彩虹渐变,但是我无法找到一种方法来停止启动整个值的起始值处理! 我知道这段代码很混乱,并且不需要像随机数这样的东西。但它抛出错误“System.ArgumentException:''256'的值对'red'无效.'red'应该大于或等于0且小于或等于255。'”
int R = 0;
int G = 0;
int B = 0;
private void timer2_Tick(object sender, EventArgs e)
{
Random r = new Random();
int A = r.Next(255, 255);
R += 1;
if (R > 250)
{
G += 1;
R -= 1;
}
if (G > 250)
{
B += 1;
G -= 1;
}
if (B > 250)
{
R += 1;
B -= 1;
}
lblMarquee.ForeColor = Color.FromArgb(A, R, G, B);
}
答案 0 :(得分:0)
你的代码存在很多问题,所以我认为id只关注它。至于你的彩虹配色方案,即使你走在正确的轨道上,我也不确定。但假设你这样做可以帮助你
// never recreate the random class in your method
// always just create one
private static readonly Random _rand = new Random();
// your variables, make them byte as that's what we are dealing with
private static byte _r = 0;
private static byte _g = 0;
private static byte _b = 0;
private static byte _a = 0;
private static SomeMethod()
{
// make your life easier with some helper methods
void Inc(ref byte val)
=> val = (byte)(val>=255 ? 0: val++);
void Dec(ref byte val)
=> val = (byte)(val<=0 ? 255: val--);
// not sure why you want this
_a = (byte)_rand.Next(255);
_r += 1;
// i have no idea what your logic is here, but it looks neater
// and wont overflow, which is your problem
// however i seriously doubt this will give you a rainbow
if (_r > 250)
{
Inc(ref _g);
Dec(ref _r);
}
if (_g > 250)
{
Inc(ref _b);
Dec(ref _g);
}
if (_b > 250)
{
Inc(ref _r);
Dec(ref _b);
}
lblMarquee.ForeColor = Color.FromArgb(_a, _r, _g, _b);
}