{"id":659,"date":"2009-01-17T16:13:43","date_gmt":"2009-01-17T21:13:43","guid":{"rendered":"http:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/659"},"modified":"2009-01-17T16:20:49","modified_gmt":"2009-01-17T21:20:49","slug":"colorconverter-for-silverlight","status":"publish","type":"post","link":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/659","title":{"rendered":"ColorConverter for Silverlight"},"content":{"rendered":"

Silverlight 2.0 is missing a ColorConverter. There\u2019s no easy and efficient way from code to convert from a HEX color format to a built in Color type. I like being able to paste standard web color formats into my source code without converting them to a Color.FromArgb byte format (as most web color selectors support that format).<\/p>\n

I wrote this code to end this annoyance (but didn\u2019t bother with a full-fledged TypeConverter):<\/p>\n

public static class <\/span>Extensions\n<\/span>{\n    public static void <\/span>SetFromHex ( this <\/span>Color <\/span>c, string <\/span>hex )\n    {\n        Color <\/span>c1 = ToColorFromHex(hex);\n\n        c.A = c1.A;\n        c.R = c1.R;\n        c.G = c1.G;\n        c.B = c1.B;                        \n    }<\/pre>\n
public static <\/span>Color <\/span>ToColorFromHex ( string <\/span>hex )\n{\n    if<\/span>(string<\/span>.IsNullOrEmpty(hex))\n    {\n        throw new <\/span>ArgumentNullException<\/span>("hex"<\/span>);\n    }\n\n    \/\/ remove any "#" characters\n    <\/span>while<\/span>(hex.StartsWith("#"<\/span>))\n    {\n        hex = hex.Substring(1);\n    }\n\n    int <\/span>num = 0;\n    \/\/ get the number out of the string \n    <\/span>if<\/span>(!Int32<\/span>.TryParse(hex, System.Globalization.NumberStyles<\/span>.HexNumber, null<\/span>, out <\/span>num))\n    {\n        throw new <\/span>ArgumentException<\/span>("Color not in a recognized Hex format."<\/span>);\n    }\n\n    int<\/span>[] pieces = new int<\/span>[4];\n    if<\/span>(hex.Length > 7)\n    {\n        pieces[0] = ((num >> 24) & 0x000000ff);\n        pieces[1] = ((num >> 16) & 0x000000ff);\n        pieces[2] = ((num >> 8) & 0x000000ff);\n        pieces[3] = (num & 0x000000ff);\n    }\n    else if<\/span>(hex.Length > 5)\n    {\n        pieces[0] = 255;\n        pieces[1] = ((num >> 16) & 0x000000ff);\n        pieces[2] = ((num >> 8) & 0x000000ff);\n        pieces[3] = (num & 0x000000ff);\n    }\n    else if<\/span>(hex.Length == 3)\n    {\n        pieces[0] = 255;\n        pieces[1] = ((num >> 8) & 0x0000000f);\n        pieces[1] += pieces[1] * 16;\n        pieces[2] = ((num >> 4) & 0x000000f);\n        pieces[2] += pieces[2] * 16;\n        pieces[3] = (num & 0x000000f);\n        pieces[3] += pieces[3] * 16;\n    }\n    return <\/span>Color<\/span>.FromArgb((byte<\/span>) pieces[0], (byte<\/span>) pieces[1], (byte<\/span>) pieces[2], (byte<\/span>) pieces[3]);\n}<\/pre>\n

<\/a><\/p>\n

}<\/pre>\n

<\/a><\/p>\n

 <\/p>\n

It accepts 3 standard formats for colors: <\/p>\n

    \n
  1. [ALPHA][RED][GREEN][BLUE] (alpha and all colors)<\/li>\n
  2. [RED][GREEN][BLUE] (no alpha channel, automatically set to 255)<\/li>\n
  3. Short hand [R][G][B] (takes the single digit hex number and adds it to the same number * 16, alpha set to 255);<\/li>\n<\/ol>\n

    To use the function:<\/p>\n

    Color <\/span>c1 = Extensions<\/span>.ToColorFromHex("#00B2D6E8"<\/span>);<\/pre>\n

    <\/a><\/p>\n

    Obviously the built in components, brushes, etc. support that format already, but there doesn\u2019t seem to be any place that functionality is exposed.<\/p>\n","protected":false},"excerpt":{"rendered":"

    Silverlight 2.0 is missing a ColorConverter. There\u2019s no easy and efficient way from code to convert from a HEX color format to a built in Color type. I like being able to paste standard web color formats into my source code without converting them to a Color.FromArgb byte format (as most web color selectors support […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[4,14],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pd5QIe-aD","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":568,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/568","url_meta":{"origin":659,"position":0},"title":"Data Binding and Tooltips in Silverlight","date":"September 19, 2008","format":false,"excerpt":"Have you ever wanted to databind a tooltip in Silverlight (or WPF for that matter), and found that the DataContext isn't available for tooltips (the datacontext is null)? It's very annoying. Tooltips, unfortunately, aren't connected to their parents in anyway when they're created, so they loose the ability to connect\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":577,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/577","url_meta":{"origin":659,"position":1},"title":"Silverlight Game Programming: Playing an Audio File","date":"October 12, 2008","format":false,"excerpt":"In my recent Silverlight game, there are a variety of sound effects that occur from bullet fires to explosions. Most of the audio files are brief, lasting only for a second or less on average. I had a few options -- put a MediaElement directly on the UI (in XAML)\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2008\/10\/image2.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":668,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/668","url_meta":{"origin":659,"position":2},"title":"Silverlight Stars\/Sparkles","date":"January 18, 2009","format":false,"excerpt":"I was in a \u201cstar\u201d mood this afternoon and created this Silverlight 2.0 demonstration. For rendering it uses the CompositionTarget.Rendering method (the easiest way to control dynamic animations such as this). It also uses the VisualStateManager in a variety of places to control the user interface. I\u2019ve become a big\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2009\/01\/image7.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":388,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/388","url_meta":{"origin":659,"position":3},"title":"Silverlight Weather Demonstration","date":"June 26, 2008","format":false,"excerpt":"Demonstration available here. (You'll need to wait for a moment while it loads the first time). I've created a reasonably simple, yet multi-technology (and discipline) demonstration using Silverlight for the user interface and ASP.NET as the back-end. The demonstration uses: Silverlight 2.0 Data binding Delayed downloading of images \"Web services\"\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2008\/06\/image21.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":723,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/723","url_meta":{"origin":659,"position":4},"title":"Silverlight Stopwatch class in C#","date":"March 1, 2009","format":false,"excerpt":"I needed a simple method for doing some timings in Silverlight. Silverlight lacks the high performance query counter that is available natively in Windows (and available in .NET 3.5 for example), but it does have a TickCount. My code (mostly) mirrors the existing .NET Stopwatch class using the TickCount property\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1701,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1701","url_meta":{"origin":659,"position":5},"title":"Windows 8 WinRT\/Metro Missing UpdateSourceTrigger","date":"August 5, 2012","format":false,"excerpt":"If you\u2019ve done WPF or Silverlight programming, you may have found an occasion where using the Binding property UpdateSourceTrigger set to PropertyChanged was extremely useful. (I know I have!) It may have looked something like this: The key feature was the live updating of the\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/659"}],"collection":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/comments?post=659"}],"version-history":[{"count":4,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/659\/revisions"}],"predecessor-version":[{"id":1034,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/659\/revisions\/1034"}],"wp:attachment":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/media?parent=659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/categories?post=659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/tags?post=659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}