sara
sara

Reputation: 319

How can I make a new color?

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 "MAKE COLOR" button, a label shows me the new color.

Upvotes: 25

Views: 68392

Answers (2)

Miltos Kokkonidis
Miltos Kokkonidis

Reputation: 3996

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; 

Upvotes: 43

Prabhu Murthy
Prabhu Murthy

Reputation: 9261

Use FromRgb to create custom colors:

Color myRgbColor = new Color();
myRgbColor = Color.FromRgb(3, 2, 5);

Upvotes: 4

Related Questions