Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map two values to one object

I am trying to write a function in C# that will take two small int values (range 0-3) and return a Color object based on those values. The problem is that there is no way to programmatically determine the color from the two values, they are specific to a type of LED and must be hardcoded.

The simplest way I can think of would be a massive (16 cases) if-else statement that will check each value, but this doesn't seem like a very elegant solution. Is there a better way to determine the Color?

like image 351
Nate Avatar asked Dec 15 '25 20:12

Nate


1 Answers

What about a two dimensional array of Color objects?

Color[,] colors = new[,] {
    { Color.FromArgb(1, 2, 3), Color.FromArgb(3, 4, 5), Color.FromArgb(6, 7, 8), Color.FromArgb(9, 10, 11) },
    { Color.FromArgb(21, 22, 23), Color.FromArgb(23, 24, 25), Color.FromArgb(26, 27, 28), Color.FromArgb(29, 30, 31) },
};

Color color = colors[index1, index2];
like image 148
Kirk Woll Avatar answered Dec 17 '25 12:12

Kirk Woll



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!