How can I make a new color?

后端 未结 2 1187
轻奢々
轻奢々 2020-12-29 05:20

I have a form in C# that I want to enter as red, green and blue in 3 TextBox controls and make a new color. For example: red=3, green=2, blue=5 when I click on

相关标签:
2条回答
  • 2020-12-29 05:50

    Let us assume that you have some code that looks similar to this:

    int red = Convert.ToInt32(RedColorComponentValueTextBox.Text);
    int green = Convert.ToInt32(GreenColorComponentValueTextBox.Text);
    int blue = Convert.ToInt32(BlueColorComponentValueTextBox.Text);
    //Don't forget to try/catch this
    

    Then to create the color from these values, try

    Color c = Color.FromArgb(red, green, blue);
    

    Then set the ForeColor property (or the BackColor property -- not sure which one you meant to change) of the label to c.

    You will need to have

    using System.Drawing;
    

    in your code file (or class) preamble.

    Note: If you wanted to also have an alpha component, you could try this:

    Color c = Color.FromArgb(alpha, red, green, blue);
    

    General hint: If you want to use an HTML/CSS color specification of the form #RRGGBB e.g. #335577, try this pattern

    int red = 0x33, green = 0x55, blue = 0x77; 
    
    0 讨论(0)
  • 2020-12-29 06:06

    Use FromRgb to create custom colors:

    Color myRgbColor = new Color();
    myRgbColor = Color.FromRgb(3, 2, 5);
    
    0 讨论(0)
提交回复
热议问题