C# 如何制作新颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13670274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:23:06  来源:igfitidea点击:

How can I make a new color?

c#winformscolors

提问by sara

I have a form in C# that I want to enter as red, green and blue in 3 TextBoxcontrols 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.

我有一个 C# 表单,我想在 3 个TextBox控件中输入红色、绿色和蓝色并创建新颜色。例如:red=3, green=2, blue=5 当我点击“MAKE COLOR”按钮时,一个标签会显示新的颜色。

采纳答案by Miltos Kokkonidis

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 ForeColorproperty (or the BackColorproperty -- not sure which one you meant to change) of the label to c.

然后将标签的ForeColor属性(或BackColor属性 - 不确定您要更改哪个)为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:

注意:如果你还想有一个 alpha 组件,你可以试试这个:

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

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

一般提示:如果你想使用表单的 HTML/CSS 颜色规范,#RRGGBB例如#335577,试试这个模式

int red = 0x33, green = 0x55, blue = 0x77; 

回答by Prabhu Murthy

Use FromRgbto create custom colors:

使用FromRgb创建自定义颜色:

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