Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# How to limit addition/negation on an incrementation code series in C#

jv1597

Coder
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:

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.
 
I found a good solution for this.

Here is the code 'Bing AI' pulled up in response to my search:
Code:
public static int Clamp(int value, int min, int max)
{
    return (value < min) ? min : (value > max) ? max : value;
}

int x = Clamp(300, 0, 255); // x will be 255
int y = Clamp(-10, 0, 255); // y will be 0
int z = Clamp(100, 0, 255); // z will be 100


Here is my implementation of the above cited solution:

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);

    static int lim(int value, int min, int max)
    {
        return (value < min) ? min : (value > max) ? max : value;
    }

    r1.Fill = new SolidColorBrush(Windows.UI.Color.FromArgb(a, r, g, b));

    red += 25;
    red = lim(red, 0, 255);
    green += 25;
    green = lim(green, 0, 255);
    blue += 25;
    blue = lim(blue, 0, 255);

    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 += 25;
    red = lim(red, 0, 255);
    green += 25;
    green = lim(green, 0, 255);
    blue += 25;
    blue = lim(blue, 0, 255);

    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 += 25;
    red = lim(red, 0, 255);
    green += 25;
    green = lim(green, 0, 255);
    blue += 25;
    blue = lim(blue, 0, 255);

    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 += 25;
    red = lim(red, 0, 255);
    green += 25;
    green = lim(green, 0, 255);
    blue += 25;
    blue = lim(blue, 0, 255);

    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));
}

Hope this helps!
 
A bit late, but here's another:
Code:
static int lim(int value, int min, int max)
    {
        return Math.Min(Math.Max(value, min), max);
    }
 

New Threads

Buy us a coffee!

Back
Top Bottom