Setting Color Using Number in C# - c#

I can set a colors to object using Brush as follows:
Brushes.Red
How to apply the same using numbers,
say,
SetColor("#ffffff");
The above is an imaginary example.

You can use ColorTranslator.FromHtml
EDIT - In response to your comment, you can create a brush based on your colour:
SolidBrush brush = new SolidBrush(ColorTranslator.FromHtml("#ffffff"));

You can make Brushes with your own Color:
Color col = Color.FromArgb(255, 255, 255);
SolidBrush br = new SolidBrush(col);
Hope that helps.

In WPF:
var x = (Color)ColorConverter.ConvertFromString("#faffff");

Color color = Color.FromRgb(255, 255, 255);
i assume you know how to calculate the values?

I think you are looking for the Color.FromArgb method. It has an overload that allows specifying the color as an integer number.

Color c = (Color)((new ColorConverter()).ConvertFromString("#ffffff"));

Related

Color a button in Unity

I was trying to change the color of my button through scripting. I'm facing a weird problem.
if I write
button1.color = new Color(255, 0, 0);
It colors the button correctly. But if I change the color something like:
button1.color = new Color(237, 85, 59);
this, then it turns to ash or no color.
I'm new in unity and don't know where is the actual problem.
The Color constructor takes in 4 values: red, green, blue and alpha. The values are between 0.0f and 1.0f, instead of
button1.color = new Color(237, 85, 59);
write something like
button1.color = new Color(0.93f, 0.33f, 0.23f, 1.0f)
The way to get the 0.0 - 1.1 values from the RGB 0 - 255 values is by dividing the color values by 255. Hope this answered your question.

How brush convert to color (UWP)?

I have convert "Windows.UI.Xaml.Media.Brush" to "Windows.UI.Color". But VS return error. Tell me please how can I do this conversion correctly?
You cannot convert a brush to a color. The concept of a brush cannot be reduced to a color, as it could be a gradient of colors, or an image etc.
The conversion only makes sense for the special case of SolidColorBrush. I am guessing that's what you are after. Here is how you do it in code:
Windows.UI.Color colorFromBrush;
if (brush is SolidColorBrush)
colorFromBrush = (brush as SolidColorBrush).Color;
else
throw new Exception("Can't get color from a brush that is not a SolidColorBrush");
Thanks,
Stefan Wick - Windows Developer Platform
You can convert Brush to color, but you have to write it explicitly.
To do so just do this:
StackPanel pane = new StackPanel()
{
Background = Background = new SolidColorBrush(new Windows.UI.Color() { A = 255, R = 25, B = 0, G = 0})
}
This works for EVERY single UIElement as long as you assign the Background property properly.
There is a workaround if you're converting it to a string, then the string to bytes and then the bytes to color:
var brushString = Foreground.ToString(); // brushString equals "#FF000000" (in case of black brush)
var brushWithoutHash = brushString.Substring(1); // brushWithoutHash equals "FF000000"
var color = Color.FromArgb(Convert.ToByte(brushWithoutHash.Substring(0, 2), 16), Convert.ToByte(brushWithoutHash.Substring(2, 2), 16), Convert.ToByte(brushWithoutHash.Substring(4, 2), 16), Convert.ToByte(brushWithoutHash.Substring(6, 2), 16));
in the last line you take the hexadecimal string values and convert them to a byte.
Make sure that you're brush is made out of one single color and not null, otherwise you'll get an exception.

Cast Color Name to SolidColorBrush

How I can cast color name to SolidColorBrush type? I mean the word i.e. "Yellow".
SolidColorBrush scb = ??? ; // "Yellow"
Thank you!
For getting the color, use:
Color col=(Color)ColorConverter.ConvertFromString("Red");
Then create your brush:
Brush brush=new SolidColorBrush(col);
or if you can use the Colors-enum
Brush brush=new SolidColorBrush(Colors.Red);
If you already know the name of the color you can get the brush directly from Brushes:
SolidColorBrush scb = Brushes.Yellow; //scb seems a bit redundant at this point...
In code you should usually not use converters unless you have a string whose value you do not know.
You cannot cast one to another. They are simply different concepts. A brush is brush and color is, well, a color. Just because a brush "paints" in a specific color, doesn't mean you can interchange one with another.
You can however create a SolidColorBrush with a specific color, for example:
var brush = new SolidColorBrush(Color.Yellow);
// Yellow is green + red
SolidColorBrush yellowBrush = new SolidColorBrush(System.Windows.Media.Color.FromRgb(255, 255, 0));

C# color value from name

I need to get the RGB value of a color given it's name in C#. I am trying to use the predefined KnownColors enum, but can't figure out how to get the value.
Any help?
Thank you.
Use FromKnownColor:
Color blue = Color.FromKnownColor(KnownColor.Blue);
Then blue.R, blue.G and blue.B for the RGB values.
Or, if you just want the int value for the RGB color, you can do:
int blueRgb = Color.FromKnownColor(KnownColor.Blue).ToArgb();
The Color class has some interesting static methods:
Color.FromName ("Red").ToArgb()
Next to that, there are some properties like:
var c = Color.FromName ("Red"); // Or use Color.FromKnownColor (KnownColor.Red)
Console.WriteLine (String.Format ("RGB: {0} {1} {2}", c.R, c.G, c.B);
Use Color.FromKnownColor then access RGB values using Color.R, Color.G and Color.B.
Color clr = FromKnownColor(System.Drawing.KnownColor.Blue);
string.Format("R:{0}, G:{1}, B:{2}" clr.R, clr.G, clr.B);
Check this Out
enter code hereYou could do
int r = Color.FromName("Purple").ToArgb();
//Or
int r1 = Color.FromKnownColor(KnownColor.Purple).ToArgb();
Color.FromName and Color.FromKnownColor witll return Color object and it has properties for Red, Green and Blue components if you want that.
Color c = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString("Red");

compare a brush and color

if (backBrush == SystemColors.ActiveCaption)
This fails. Says you can't compare a brush and a color.
How do I find the color of the brush?
If backBrush is a SolidColorBrush (based on what you're trying to do it probably is) you can use:
if(((SolidColorBrush)backBrush).Color == SystemColors.ActiveCaption)
If the brush is a SolidBrush you can compare the Color member of the brush. Something like this.
SolidBrush solidBrush = brush as SolidBrush;
if (solidBrush != null && solidBrush.Color == SystemColors.ActiveCaption)
{
// ....
}
The above is for WinForms, for WPF you would use SolidColorBrush rather than SolidBrush.
A Brush does not have a color.
You use a Brush with a Color for filling/painting etc.
Some brushes do have a color (HatchBrush has two), so you will need to cast to the type of brush and compare colors then:
((HatchBrush)backBrush).BackgroundColor == SystemColors.ActiveCaption
Have you tried the SystemBrushes namespace?
if (backBrush == SystemBrushes.ActiveCaption)
{...

Categories