I'm trying to minimize my color picker code by generating colors for multiple rectangle objects with one click of a button. I would like to know how to limit the addition and negation in RGB value incrementations/decrementations to keep from going under the 0 value and over the 255 value limit on them.
Here is some lab code to simplify:
I keep getting an error message saying: "Value was either too large or too small for an unsigned byte.', implying that the addition/negation has taken the value out of range 0-255. I would like to know how to limit the addition/subtraction to the 0-255 value range. Thanks in advance.
Here is some lab code to simplify:
Code:
public void btn(object sender, RoutedEventArgs e)
{
Button _b = sender as Button;
SolidColorBrush _s = _b.Background as SolidColorBrush;
int alpha = 255;
int red = Convert.ToInt16(_s.Color.R);
int green = Convert.ToInt16(_s.Color.G);
int blue = Convert.ToInt16(_s.Color.B);
var a = Convert.ToByte(alpha);
var r = Convert.ToByte(red);
var g = Convert.ToByte(green);
var b = Convert.ToByte(blue);
r1.Fill = new SolidColorBrush(Windows.UI.Color.FromArgb(a, r, g, b));
red += 10;
green += 10;
blue += 10;
a = Convert.ToByte(alpha);
r = Convert.ToByte(red);
g = Convert.ToByte(green);
b = Convert.ToByte(blue);
r2.Fill = new SolidColorBrush(Windows.UI.Color.FromArgb(a, r, g, b));
red += 10;
green += 10;
blue += 10;
a = Convert.ToByte(alpha);
r = Convert.ToByte(red);
g = Convert.ToByte(green);
b = Convert.ToByte(blue);
r3.Fill = new SolidColorBrush(Windows.UI.Color.FromArgb(a, r, g, b));
red += 10;
green += 10;
blue += 10;
a = Convert.ToByte(alpha);
r = Convert.ToByte(red);
g = Convert.ToByte(green);
b = Convert.ToByte(blue);
r4.Fill = new SolidColorBrush(Windows.UI.Color.FromArgb(a, r, g, b));
red += 10;
green += 10;
blue += 10;
a = Convert.ToByte(alpha);
r = Convert.ToByte(red);
g = Convert.ToByte(green);
b = Convert.ToByte(blue);
r5.Fill = new SolidColorBrush(Windows.UI.Color.FromArgb(a, r, g, b));
red += 10;
green += 10;
blue += 10;
a = Convert.ToByte(alpha);
r = Convert.ToByte(red);
g = Convert.ToByte(green);
b = Convert.ToByte(blue);
I keep getting an error message saying: "Value was either too large or too small for an unsigned byte.', implying that the addition/negation has taken the value out of range 0-255. I would like to know how to limit the addition/subtraction to the 0-255 value range. Thanks in advance.