Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to switch Brush colours?

Tags:

c#

winforms

I'm new to C# and working on a snake project. I'm trying to make it rainbow coloured, is there a better way to switch between six colours and then repeat?

public Brush Colour(int i)
{
    Brush snakeColour;
    switch (i)
    {
        case 0:
        case 6:
        case 12: 
        case 18: 
        case 24:
            snakeColour = Brushes.HotPink;
            break;

        case 1: 
        case 7: 
        case 13: 
        case 19: 
        case 25:
            snakeColour = Brushes.Orange;
            break;

        case 2: 
        case 8: 
        case 14: 
        case 20: 
        case 26:
            snakeColour = Brushes.PeachPuff;
            break;

        etc.

        default:
            snakeColour = Brushes.White;
            break;
    }

    return snakeColour;
}

Any suggestions?

like image 897
Sarasaurus Avatar asked Jan 05 '20 08:01

Sarasaurus


People also ask

How do I change my brush mode?

Select the Brush Tool and then go to the Options Bar, click the Mode drop-down menu, and select Color . To prepare to colorize an element, go to the image and right -click (PC)/ Control -click (Mac) anywhere to open the Brush Preset Picker .

What is the shortcut key for brush tool?

Brush Tool: Which keyboard shortcut makes the current tool a brush? To select the Brush Tool press the b key.


2 Answers

If you use the remainder operator (and you assume non-negative input) you know you'll always have a value in the range 0 to 5 inclusive, so you don't need a switch at all - just use an array:

private static readonly Brush[] brushes =
{
    Brushes.HotPink,
    Brushes.Orange,
    Brushes.PeachPuff,
    ...
    Brushes.White
};

// TODO: Potentially rename from using "position" to something else,
// based on what the parameter is really meant to represent.
public Brush GetBrushForPosition(int position) => brushes[position % 6];

If the input might be negative, you can use the slightly more long-winded expression of ((position % 6) + 6) % 6 for the array index, which will still cycle appropriately. (There are other approaches of course, but that's reasonably simple.)

like image 107
Jon Skeet Avatar answered Oct 02 '22 03:10

Jon Skeet


You could reduce the number of case labels using the modulus operator:

public Brush Colour(int i)
{
    Brush snakeColour;
    switch (i % 6)
    {
        case 0:
            snakeColour = Brushes.HotPink;
            break;

        case 1:
            snakeColour = Brushes.Orange;
            break;

        case 2:
            snakeColour = Brushes.PeachPuff;
            break;

//      etc.

        default:
            snakeColour = Brushes.White;
            break;

    }

    return snakeColour;
}

But note: this would most likely remove the need for your default case (assuming you have all cases from 0 thru 5 taken care of). That may be contrary to what you intend, if the actual i value is outside a specific range!

like image 27
Adrian Mole Avatar answered Oct 02 '22 01:10

Adrian Mole