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)
{...
Related
I am trying to read the argb value of a cell using epplus 4.5.2. I am able to read the same for most of the cells. For two particular colors in my file epplus is returning null as values. I did further digging into stack trace and found that pattern type for these cells with two colors are null.
COLOR RGB values found out using Excel Color picker tool are :
COLOR 1 : R = 198, G = 224, B = 180 (Theme Color)
COLOR 2 : R = 172, G = 185, B = 202 (Theme Color)
COLOR 3 : R = 201, G = 201, B = 201 (Theme Color) Pattern type Solid set for this.
I have tried changing the colors of these cells to solid red and epplus seems to recognizing the red color for those same cells.
The theme color field is set for all colors which are not providing rgb values. I will have to write a method for converting theme value and tint value to RGB values.
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.
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));
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"));
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");