Silverlight Stars/Sparkles

I was in a “star” mood this afternoon and created this Silverlight 2.0 demonstration.

image

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’ve become a big fan of the VSM – it’s easier to use than what WPF has as of 3.5 SP1 (I know it’s coming to WPF as well).

If you click into the UI to give it focus, you can press the [F] key to go full screen.

Switching to full screen is simple:

SilverlightHost host = Application.Current.Host;
if(host != null)
{
    Content content = host.Content;
    content.IsFullScreen = true;
}

I’ve also used my ToColorFromHex function liberally (I used Kuler to make some colors and grabbed their hex values and directly pasted into my source code).

To create the star trails (as I’ve called them), I needed to do a little bit of trigonometry:

double radians = Math.Atan2(-star.Vector2D.Y, -star.Vector2D.X);
(star.StarTrailUI.RenderTransform as RotateTransform).Angle = radians * 180 / Math.PI;

I used a RenderTransform in place of building a line with the proper X & Y coordinates as it made dealing with the LinearGradientBrush on the trail a TON easier (for me at least).

Demonstration

I’ve provided all of the source code – maybe you’ll find it useful (if so, I’d like to hear about it!).

Source code

Update: Fixed the links. Sorry!

ColorConverter for Silverlight

Silverlight 2.0 is missing a ColorConverter. There’s 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).

I wrote this code to end this annoyance (but didn’t bother with a full-fledged TypeConverter):

public static class Extensions
{
    public static void SetFromHex ( this Color c, string hex )
    {
        Color c1 = ToColorFromHex(hex);

        c.A = c1.A;
        c.R = c1.R;
        c.G = c1.G;
        c.B = c1.B;                        
    }
public static Color ToColorFromHex ( string hex )
{
    if(string.IsNullOrEmpty(hex))
    {
        throw new ArgumentNullException("hex");
    }

    // remove any "#" characters
    while(hex.StartsWith("#"))
    {
        hex = hex.Substring(1);
    }

    int num = 0;
    // get the number out of the string 
    if(!Int32.TryParse(hex, System.Globalization.NumberStyles.HexNumber, null, out num))
    {
        throw new ArgumentException("Color not in a recognized Hex format.");
    }

    int[] pieces = new int[4];
    if(hex.Length > 7)
    {
        pieces[0] = ((num >> 24) & 0x000000ff);
        pieces[1] = ((num >> 16) & 0x000000ff);
        pieces[2] = ((num >> 8) & 0x000000ff);
        pieces[3] = (num & 0x000000ff);
    }
    else if(hex.Length > 5)
    {
        pieces[0] = 255;
        pieces[1] = ((num >> 16) & 0x000000ff);
        pieces[2] = ((num >> 8) & 0x000000ff);
        pieces[3] = (num & 0x000000ff);
    }
    else if(hex.Length == 3)
    {
        pieces[0] = 255;
        pieces[1] = ((num >> 8) & 0x0000000f);
        pieces[1] += pieces[1] * 16;
        pieces[2] = ((num >> 4) & 0x000000f);
        pieces[2] += pieces[2] * 16;
        pieces[3] = (num & 0x000000f);
        pieces[3] += pieces[3] * 16;
    }
    return Color.FromArgb((byte) pieces[0], (byte) pieces[1], (byte) pieces[2], (byte) pieces[3]);
}

}

 

It accepts 3 standard formats for colors:

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

To use the function:

Color c1 = Extensions.ToColorFromHex("#00B2D6E8");

Obviously the built in components, brushes, etc. support that format already, but there doesn’t seem to be any place that functionality is exposed.

My Mix09 10K entry is now showing …

I entered the Mix09 10K programming contest this year.  The basic premise – code  any Silverlight or WPF click-once application and use less than 10K code/assets total. I struggled to get what I wanted in under 10K (I kept trimming/removing)! I had to change my idea several times to get it to fit into 10K. All my variables had one or two character names, I used var, lambda expressions, every messy-code-squashing trick I could think of to get it to fit!

I’d appreciate your vote here.

image

Thanks!

Backspace on a calculator?! No Way!

What a great idea! A back button on a calculator! I don’t use “real” world calculators much anymore, but seeing a back button made me wonder why it’s taken so long for a back button to be added! (OK, maybe they’ve been there for a while … and I haven’t noticed them.).

image

Annoying C# Web Exception …

System.Net.WebException

The request was aborted: The request was canceled.

Cannot close stream until all bytes are written.

If you’re getting that exception, make sure that the timeout for the webrequest is set sufficiently to handle the request. The default as of .NET 3.5 SP1 seems to be 100 seconds. I’m working on an fix to SnugUp and discovered that large files would throw the exception above. I thought it must be a problem on the SmugMug side, but couldn’t come up with a rational reason why it would just fail with my application … and then it dawned on me that it was closing the stream early because it had exceeded the timeout!

So, if you’ve seen that exception, and can’t figure it out … consider checking the timeout. (Also, verify the content-length is set properly – that’s another reason this exception might be thrown).