Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically set TextBlock Foreground Hex Color

I have to use Foreground Hex color of a Textblock Pragmatically.it does not work.Please help me.

I have tried this example

txtHome.Foreground = new SolidColorBrush(Colors.Red);

but I want use Hex color code not use Color.red etc..

like image 544
Kanniyappan Avatar asked Mar 11 '14 06:03

Kanniyappan


4 Answers

While there are answers that create unnecessary strings, I'd suggest just using the most efficient:

var brush=new SolidColorBrush(Color.FromArgb(0xFF, 0xD0, 0x20, 0x30));

I've just used the FromArgb method to convert the hex representation of a color directly. The first parameter is the alpha or opacity which you can always use 255/0xFF to specify full opacity. Then, it's just providing the 3 bytes representing the color, which are in the same order as they would have appeared in a common hex representation of a color. In the example above: "D02030".

Also, you might consider creating a reusable resource representing the SolidColorBrush and adding it to the app.xaml file so it becomes globally available:

<SolidColorBrush x:Key="myBrush" Color="#D02030" />

Then, in code:

txtHome.Foreground =  App.Current.Resources["myBrush"] as SolidColorBrush;
like image 103
WiredPrairie Avatar answered Oct 19 '22 06:10

WiredPrairie


Try this:

public class ColorConverter
    {
     public static SolidColorBrush GetColorFromHexa(string hexaColor)
        {
            return new SolidColorBrush(
                Color.FromArgb(
                    Convert.ToByte(hexaColor.Substring(1, 2), 16),
                    Convert.ToByte(hexaColor.Substring(3, 2), 16),
                    Convert.ToByte(hexaColor.Substring(5, 2), 16),
                    Convert.ToByte(hexaColor.Substring(7, 2), 16)
                )
            );
        }
    }

Then you can use it like:

txtHome.Foreground = ColorConverter.GetColorFromHexa(("#FFF0F0F0"));
like image 26
Olter Avatar answered Oct 19 '22 05:10

Olter


You can use this function to convert hex color in to color value then u can set it on textblock.

public Color ConvertStringToColor(String hex) {

        hex = hex.Replace("#", "");

        byte a = 255;
        byte r = 255;
        byte g = 255;
        byte b = 255;

        int start = 0;

        //handle ARGB strings (8 characters long)
        if (hex.Length == 8)
        {
            a = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
            start = 2;
        }

        //convert RGB characters to bytes
        r = byte.Parse(hex.Substring(start, 2), System.Globalization.NumberStyles.HexNumber);
        g = byte.Parse(hex.Substring(start + 2, 2), System.Globalization.NumberStyles.HexNumber);
        b = byte.Parse(hex.Substring(start + 4, 2), System.Globalization.NumberStyles.HexNumber);

        return Color.FromArgb(a, r, g, b);
    }

Color color1 = ConvertStringToColor("#F0A30A"); txtHome.Foreground = new SolidColorBrush(color1);

like image 22
Praveen Mishra Avatar answered Oct 19 '22 06:10

Praveen Mishra


 txtHome.Foreground = GetColorFromHexa("#FF0000");

 SolidColorBrush GetColorFromHexa(string hexaColor)
        {
            byte r = Convert.ToByte(hexaColor.Substring(1, 2), 16);
            byte g = Convert.ToByte(hexaColor.Substring(3, 2), 16);
            byte b = Convert.ToByte(hexaColor.Substring(5, 2), 16);
            SolidColorBrush soliColorBrush = new SolidColorBrush(Color.FromArgb(0xFF, r, g, b));
            return soliColorBrush;
        }
like image 1
Jaihind Avatar answered Oct 19 '22 04:10

Jaihind