Click here to Skip to main content
13,050,450 members (84,657 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


Posted 10 Jan 2012

Color Code to SolidColor Conversion

, 10 Jan 2012
Rate this:
Please Sign up or sign in to vote.
I think there might be a couple of typos in the original code. This works, though...public SolidColorBrush GetColor(string ColorCode){ return (SolidColorBrush)XamlReader.Load( "<SolidColorBrush xmlns=\"http://schemas." + "\"...
I think there might be a couple of typos in the original code. This works, though...

public SolidColorBrush GetColor(string ColorCode)
    return (SolidColorBrush)XamlReader.Load(
        "<SolidColorBrush xmlns=\"http://schemas." +
        "\" Color=\"" + 
        ColorCode + "\" />");

Alternatively you could do something like this rather than use the XamlReader...

public static SolidColorBrush GetColor(string s)
    //this method assumes "#00000000" or "00000000" format and DOES NOT VALIDATE - add anti-splode code here!
    int hashOffset = s.Substring(0, 1) == "#" ? 1 : 0; //accounts for "#FF000000" format
    byte a = Convert.ToByte(s.Substring(0 + hashOffset, 2), 16);
    byte r = Convert.ToByte(s.Substring(2 + hashOffset, 2), 16);
    byte g = Convert.ToByte(s.Substring(4 + hashOffset, 2), 16);
    byte b = Convert.ToByte(s.Substring(6 + hashOffset, 2), 16);
    return new SolidColorBrush(Color.FromArgb(a, r, g, b));

Laurent Duveau has a nice implementation with an extension method here[^].


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Adam David Hill
Software Developer
United Kingdom United Kingdom


Musician turned Software Engineer (turned professional around 6 years ago). Mainly interested in games & mobility.

Sometimes I do real work, too.

My articles:

My open source software:

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 10 Jan 2012
Article Copyright 2012 by Adam David Hill
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid