4

I am trying to set a colour of an ellipse object in code behind. So far I'm doing it by using the SolidColorBrush method. Is there a way to insert the colour value in hexadecimal, like in CSS?

Here is a code that I am using:

ellipse.Fill = new SolidColorBrush(Colors.Yellow);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Drahcir
  • 12,311
  • 19
  • 63
  • 76
  • It's very strange you can't do this in Silverlight, considering most XAML uses hex strings for color. Thus, they have the code, but they're not exposing it for the rest of us in a manner to use in code-behind (C#). – RyBolt Feb 03 '11 at 22:45

7 Answers7

4

Something like this would work

ellipse.Fill = 
    new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF00DD")); 

(Edit: It looks like this is WPF only. Alex Golesh has a blog post here about his Silverlight ColorConverter)

Although I prefer the Color.FromRgb method

byte r = 255;
byte g = 0;
byte b = 221;
ellipse.Fill = new SolidColorBrush(Color.FromRgb(r,g,b)); 
Ray
  • 45,695
  • 27
  • 126
  • 169
2

I wrote a simple color converter function to solve this problem. The happy faces are really the number 8 and a parentheses, like this: 8).

2

From MSDN

SolidColorBrush mySolidColorBrush = new SolidColorBrush();

// Describes the brush's color using RGB values. 
// Each value has a range of 0-255.
mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, 255);
myRgbRectangle.Fill = mySolidColorBrush;   
PaulB
  • 23,264
  • 14
  • 56
  • 75
2

Of course, you could also do something like this (using the hex numbers in the FromArgb function):

SolidColorBrush mySolidColorBrush = new SolidColorBrush();

// Describes the brush's color using RGB HEX values. 
// Each value has a range of 0-255. Use 0x for HEX numbers
mySolidColorBrush.Color = Color.FromArgb(255, 0xFF, 0xC0, 0xD0);
myRgbRectangle.Fill = mySolidColorBrush;
WiredPrairie
  • 58,954
  • 17
  • 116
  • 143
1

For using a hex value:

 your_contorl.Color = DirectCast(ColorConverter.ConvertFromString("#D8E0A627"), Color)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

Another one, small, fast and useful:

public static Color ToColor(this uint argb)
{
    return Color.FromArgb((byte)((argb & -16777216) >> 0x18),
                          (byte)((argb & 0xff0000) >> 0x10),
                          (byte)((argb & 0xff00) >> 8),
                          (byte)(argb & 0xff));
}

Use in code:

SolidColorBrush scb  = new SolidColorBrush (0xFFABCDEF.ToColor());

Of course it is needed to use 0xFFFFFFFF (uint) notation instead of "#FFFFFFFF" (string), but I am sure it's no big deal.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
A1exandr Belan
  • 4,442
  • 3
  • 26
  • 48
0

I think this will work, as it works for textbox.

var bc = new BrushConverter();
textRichTextBoxEditor.Foreground = (Brush)bc.ConvertFrom("#FF97315A");

Visit http://soulsolutions.com.au/Blog/tabid/73/EntryId/617/Using-Hex-Colour-values-to-set-Foreground-in-WPF.aspx

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user765573
  • 21
  • 6