0

I want to set a color for each number from 0-9, from white to black white being 0, and black being 9, so in the middle would be blue=2, green=1, red=4. So when a user enter a number 2144214 i could calculate and create a new color. Something like 2xgreen, 2xblue and 3xred, that would be 2+2+1+1+4+4+4 and the result would be a new color. That is the idea, but the part that is killing me is setting a color value to a variable, is it possible? I'm working in a WPF C# app.

AlexDev
  • 85
  • 1
  • 9
  • any color in WPF can be represented by an html string: #TTRRGGBB where TT is the transparency and RR,GG,BB the red, green and blue, so you can create any color with this line of code: Color color = (Color)ColorConverter.ConvertFromString("#FFDFD991"); that is an answer here on stack overflow http://stackoverflow.com/questions/2109756/how-to-get-color-from-hexadecimal-color-code-using-net – Sabrina_cs Aug 24 '16 at 18:43
  • And i would have to initialize red, blue and green, so i can count the number of each of the colors later in the app? – AlexDev Aug 25 '16 at 12:19

2 Answers2

1

I would recommend you to use Enum Types

enum Color
{
    white = 0,
    red = 1,
    blue = 2,
    black = 9
}

Link: https://msdn.microsoft.com/en-us/library/cc138362.aspx?f=255&MSPPError=-2147217396

0

Looking at your complete problem, I think you need to store red, green, and blue separately, and increment / decrement those values according to the digits input. It could go something like this

// get a digit

switch (digit) {
    case 0: // black, darken the color
        red -= 4;  // value of 4 is arbitrary, it could be 1 or other
        green -= 4;
        blue -= 4;
        break;
    case 1: // red
        red += 4;
        break;
    case 2: // green
        green += 4;
        break;
    case 3: // blue
        blue+= 4;
        break;
    case 4:  // cyan
        green += 4;
        blue += 4;
        break;
    case 5:  // yellow
        red += 4;
        green += 4;
        break;
    case 6:  // magenta
        red += 4;
        blue += 4;
        break;
    case 7:  // white, lighten everything
        red += 4;
        green += 4;
        blue += 4;
        break;
    default:
        break;
}

Of course, you will need to initialize red, green, and blue to 0, and after building up your color, would need to check it for validity (many color systems have a limit of 255 on each color). After all this is done, you will have red, green, and blue, and can comine those with Color.FromArgb() as @EdPlunkett suggests above.

roderick young
  • 284
  • 1
  • 10
  • so you are suggesting that i should make 9 cases for different colors, from black=case 0 to white=case 9? – AlexDev Aug 25 '16 at 12:19
  • Sorry for the delay in replying. That is the drift of the code above. There are of course many ways to do it, and there are established ways of picking a color by RGB or HSL. I wasn't exactly sure of what you wanted, but it sounded like you had a dish of water, and wanted to add more drops of red, green, blue, etc. food coloring, depending on the input string. – roderick young Aug 28 '16 at 03:41
  • well I'll try and make it more understandable - I want to make an app in which the user would input information, ofc, numbers, like "enter your yearly income" and the user would input 321444 and the app would make a color out of those numbers, ofc i first thought it could be done by making 10 colors from 0-9 but in the meantime i discovered that it is much more easier to use just red green and blue colors. but my problem now is how do I sum up the numbers. – AlexDev Aug 28 '16 at 13:35
  • I also made 3 lists for red,green and blue and each list has their own numbers, for example, red has 0,1,3 green has 2,4,6,8 and blue has 5,7,9 now what i want to do is sum up all the 1s, and the 3s, and the 4s and the 7s for example and it would give me the value for each color and then i could use FromRgb() to create a color. – AlexDev Aug 28 '16 at 13:38
  • Hopefully you got it by now, but if not, you can use the "%" (mod) operator to get the last digit of a number. For example, if the number is 123456, then (123456 % 10) is 6. To shift the digits to the right, you would divide by 10. (123456 / 10) is 12345, from which you could extract the rightmost digit. Don't give up. See if you can find a friend to help you with this sort of thing. Stackoverflow can be very picky about having a well-defined, easy-to-answer question. – roderick young Aug 30 '16 at 14:33
  • I am definitely not quitting, and thanks for the advice, I'm gonna figure something out! – AlexDev Aug 30 '16 at 20:12