Inflating color value
-
I have a very basic query. Let's say we have a Color1, and I need Color2 to be
Color.FromRgb (Math.Min(Color1.R+10,255), Math.Min(Color1.G+10,255), Math.Min(Color1.B+10,255));
But I am not able to do this simple thing. Basically, I've got stuck with integer and byte conversion. Please suggest in following code-
private Color GetNewColor(Color colorParameter)
{
Color newColor;
//Set value of newColor here
return newColor;
}//Color1 = Color.FromRgb(255, 0, 0);
//return GetNewColor(Color1);
//It should return (255,10,10) -
I have a very basic query. Let's say we have a Color1, and I need Color2 to be
Color.FromRgb (Math.Min(Color1.R+10,255), Math.Min(Color1.G+10,255), Math.Min(Color1.B+10,255));
But I am not able to do this simple thing. Basically, I've got stuck with integer and byte conversion. Please suggest in following code-
private Color GetNewColor(Color colorParameter)
{
Color newColor;
//Set value of newColor here
return newColor;
}//Color1 = Color.FromRgb(255, 0, 0);
//return GetNewColor(Color1);
//It should return (255,10,10)What exactly is going wrong? Are you getting an exception or a compiler error or is it just not doing what you expect? One possible issue I can see is an overflow of the colour component. For example, with a
byte
, if you add 10 to 255 you'll actually end up with 9, andMath.Min
won't work as you expect. You can catch this problem by surrounding the code with achecked
statement, which will cause the runtime to throw on an overflow. The quickest solution to this would be to cast thebyte
to anint
first, do the addition, and cast the result back to a byte (once you're certain it'll fit). -
What exactly is going wrong? Are you getting an exception or a compiler error or is it just not doing what you expect? One possible issue I can see is an overflow of the colour component. For example, with a
byte
, if you add 10 to 255 you'll actually end up with 9, andMath.Min
won't work as you expect. You can catch this problem by surrounding the code with achecked
statement, which will cause the runtime to throw on an overflow. The quickest solution to this would be to cast thebyte
to anint
first, do the addition, and cast the result back to a byte (once you're certain it'll fit).Thanks for the reply! Problem was with casting only. I've sorted that out now. Trivial, but just thought of sharing it:
byte r = Convert.ToByte(Math.Min(255, Color1.R + 10));
byte g = Convert.ToByte(Math.Min(255, Color1.G + 10));
byte b = Convert.ToByte(Math.Min(255, Color1.B + 10));
return Color.FromRgb(r, g, b); -
I have a very basic query. Let's say we have a Color1, and I need Color2 to be
Color.FromRgb (Math.Min(Color1.R+10,255), Math.Min(Color1.G+10,255), Math.Min(Color1.B+10,255));
But I am not able to do this simple thing. Basically, I've got stuck with integer and byte conversion. Please suggest in following code-
private Color GetNewColor(Color colorParameter)
{
Color newColor;
//Set value of newColor here
return newColor;
}//Color1 = Color.FromRgb(255, 0, 0);
//return GetNewColor(Color1);
//It should return (255,10,10)