Silverlight 2 Released (What a bad news for me) Silverlight 2 Upgrade: 3D Text Space
Oct 16

Today, I installed and uninstalled the Silverlight Tool many times. It was because I couldn’t open Silverlight Project after updating all the patches. The Visual Studio always show this message “The project type is not supported by this installation?”.  But finally, I found that I can actually use the Visual Web Developer 2008 to create Silverlight Application.

Does it mean that I can’t use Visual Studio anymore? I am not sure. Since I can continue my Silverlight development, I don’t care anymore.

By the way, the sample below is my first Silverlight 2 application. The logic is a little bit advanced, hope you will like it.

Lastly, I got a good news for all of you. I am going to update all the sample in my blog. Hopefully, all the samples should be ready be the end of this month.

Comparison

Flash implementation: 35 minutes
Silverlight implementation: 80 minutes (Implemented First)
What’s the difference?

  • HSL to RGB [AS3] vs [C#]

Source codes

Flash

Silverlight

HSL to RGB [AS3] vs [C#]

If you are going to implement drawing application, then you would probably need to handle HSL and RGB value. Below is a function that convert HSL to RGB. If you want to convert RGB to HSL, download and read the sample code above.

// AS3
// Convert HSL to RGB
public function hslToRgb(H : Number, S : Number, L : Number) : Array {
	var p1:Number;
	var p2 : Number;
	var rgb : Array = new Array(3);

	if (L<=0.5) p2 = L*(1+S);
	else p2 = L+S-(L*S);

	p1 = 2*L-p2;

	if (S == 0) {
		rgb[0] = L;
		rgb[1] = L;
		rgb[2] = L;
	} else {
		rgb[0] = toRgb(p1, p2, H+120);
		rgb[1] = toRgb(p1, p2, H);
		rgb[2] = toRgb(p1, p2, H-120);
	}
	rgb[0] *= 255;
	rgb[1] *= 255;
	rgb[2] *= 255;
	return rgb;
}

// Calculate the RGB Value
public function toRgb(q1 : Number, q2 : Number, hue : Number) : Number {
	if (hue>360) hue = hue-360;
	if (hue<0) hue = hue+360;
	if (hue<60) return (q1+(q2-q1)*hue/60);
	else if (hue<180) return (q2);
	else if (hue<240) return (q1+(q2-q1)*(240-hue)/60);

	return (q1);
}

Mathematical Syntax in C# is very similar to AS3. Most of the code can be reused by directly copy and paste.

// C#
// Convert HSL to RGB
private double [] hslToRgb(double H, double S, double L) {
	double [] rgb = new double [3];
	double p1 = 0;
	double p2 = 0;

	if (L<=0.5) p2 = L*(1+S);
	else p2 = L+S-(L*S);

	p1 = 2*L-p2;

	if (S == 0) {
		rgb[0] = L;
		rgb[1] = L;
		rgb[2] = L;
	} else {
		rgb[0] = toRgb(p1, p2, H+120);
		rgb[1] = toRgb(p1, p2, H);
		rgb[2] = toRgb(p1, p2, H - 120);
	}
	rgb[0] *= 255;
	rgb[1] *= 255;
	rgb[2] *= 255;
	return rgb;
}

// Calculate the RGB Value
private double toRgb(double q1, double q2, double hue) {
	if (hue>360) hue = hue-360;
	if (hue<0) hue = hue+360;
	if (hue<60) return (q1+(q2-q1)*hue/60);
	else if (hue<180) return (q2);
	else if (hue<240) return (q1+(q2-q1)*(240-hue)/60);

	return (q1);
}

Shares and Enjoy~

Did you like this post?

Subscribe here:  

31 Responses to “First Silverlight 2 Application: Color Picker”

  1. Boubalou Says:

    Weird, I tho i had the plugin for silverlight but this app doesn’t show and ask me to install it again.

  2. Boubalou Says:

    Alright, got it. I had the latest beta which wasn’t the release candidate. Working now.

  3. Mike Says:

    I’m not sure what was going wrong with your VS2008 Tools update. Mine worked fine when I updated from RC0 to the RTM. Maybe you didn’t install the sdk?

  4. Eran Lahav Says:

    10x.. ^_^
    your comparisons really shine us out in a few issues ..
    You are most welcome to try our new Silverlight add on

    http://www.headup.com just launched

    contact me for invitation…
    I would love to hear your opinion.
    Eran

  5. Jhonghee Says:

    I got Silverlight Plug-in 2.0.31005.0 installed for Firefox 3.0.3 and the silverlight version does not work. On the other hand, I got Flash 10 installed and works fine. That’s why Flash should be bigger. ;-)

  6. Jhonghee Says:

    Oh, I take that back. It works fine on Firefox. But I still like Flash version. ;-P

  7. Chris Says:

    Just read ScottGu’s blog:

    “If you do not have VS 2008, you can alternatively install the free Visual Web Developer 2008 Express SP1 Edition. ”

    and there’s a detailed instruction how to upgrade from beta2 to final.
    “If you have the Beta2 or RC versions of the VS Tools for Silverlight, or …..”

    http://weblogs.asp.net/scottgu/archive/2008/10/14/silverlight-2-released.aspx

    Cheers,
    Chris

  8. admin Says:

    Chris, thanks for the suggestion.

    Since my VS is Chinese version, may be it has some compatability problem with the SDK.

    Actually, Visual Web Developer 2008 works for me. However, if the application throw an error, the IE and Visual Web Developer will crash immediately. It’s really a painful expereince for me.

  9. Chris Says:

    I am using Visual Web Developer 2008, too.
    And Firefox as browser.
    No Problems yet.
    Cheers,
    Chris

  10. Silverlight Cream for October 17, 2008 -- #398 Says:

    [...] First Silverlight 2 Application: Color Picker [...]

  11. 2008 October 21 - Links for today « My (almost) Daily Links Says:

    [...] Terence Tsang’s great First Silverlight 2 Application: Color Picker [...]

  12. Metin Says:

    Silver light is better

  13. athena301 Says:

    牛东西啊….

  14. athena301 Says:

    我用vs2005,怎么用这个东西呀。.

  15. admin Says:

    You may only use VS 2008 to open it.

  16. string GetEnglishName() Says:

    First,thanks for the samples.
    Second,i need a simple english name,who can help me,thank you .
    I am Chinese,too.
    I like the silverlight and I will write my First Silverlight Application in the coming days.
    foreach(string word in mywritings)
    {
    if(there are mistakes)
    {
    I am sorry.
    I tryed my best to avoid the mistakes.
    }
    }
    Last I find the link of this page on the offical Microsoft Silverlight Sit. silverlight.net/showcase
    I will come back to see if someone can offer me a nice simple English names.

  17. admin Says:

    English Name is a bit personal. You may pick up a dictionary and choose the one you like.
    May be you can call yourself Silverlight.

  18. string GetEnglishName() Says:

    I need a professional name,Not a English word.
    It can be 6 characters or less.
    Thank you for your advice.

  19. Nolz Says:

    You need to be more correct with your comparison, I Think you should increas the FPS for the Flash-Application to make the comparision more correct, the problem now is that Silver Light throws graphic out as fast as the computer allows, but Flash dosent render more frames than you desired it do. and it looks like the Flash application in this example is set to 24, witch makes it look bad in the comparison.

  20. Develop Silverlight on Eclipse : Eclipse4SL Says:

    [...] try the reverse order. I attempt to move my ColorPicker Sample into the Eclipse Workspace. A new Eclipse Silverlight Project is created successfully. [...]

  21. Krzyś Says:

    “If you want to convert RGB to HSL, download and read the sample code above”

    Sorry - I cannot find it…

  22. admin Says:

    source code is provided. The links are on top of the Flash Application.

  23. Krzyś Says:

    But I cannot find function to convert RGB to HSL in that source code…

  24. admin Says:

    oh, yes. I am sorry. I didn’t write that part.

  25. Krzyś Says:

    http://en.wikipedia.org/wiki/HSL_and_HSV

    private double[] rgbToHsl(Color color)
    {
    double R, G, B;

    R = color.R / 255.0;
    G = color.G / 255.0;
    B = color.B / 255.0;

    return rgbToHsl(R, G, B);
    }

    private double[] rgbToHsl(double R, double G, double B)
    {
    double h, s, l;
    double[] hsl = new double[3];

    double max = Math.Max(Math.Max(R, G), B);
    double min = Math.Min(Math.Min(R, G), B);

    if (min == max)
    {
    h = 0;
    s = 0;
    l = min;
    }
    else
    {
    if (max == R)
    {
    h = 60.0 * (G - B) / (max - min);
    }
    else
    {
    if (max == G)
    {
    h = 60.0 * (B - R) / (max - min) + 120.0;
    }
    else // max == B
    {
    h = 60.0 * (R - G) / (max - min) + 240.0;
    }
    }
    while (h = 360.0) h -= 360.0;

    l = (min + max) / 2.0;

    if (l < 0.5)
    {
    s = (max - min) / (2.0 * l);
    }
    else
    {
    s = (max - min) / (2.0 - 2.0 * l);
    }
    }

    hsl[0] = h;
    hsl[0] = s;
    hsl[0] = l;

    return hsl;
    }

  26. Krzyś Says:

    Sorry…

    hsl[0] = h;
    hsl[1] = s;
    hsl[2] = l;

  27. tali Says:

    i dont see the point in this

  28. HSV Color Picker | Silverlike - A Free Microsoft Silverlight 3 Directory Says:

    [...] A HSV style color picker created by Terence Tsang. [...]

  29. Петр Says:

    Да, таких интересных блогов я еще не видел! Этот блог даст фору многим сайтам ( по содержанию и не только)! Десять баллов!

  30. Валерий Дасаев Says:

    Увлекательно! Хотелось бы побольше точно таких же заниматльных постов

  31. Flash ve Silverlight karşılaştırması | AdobeHaber Says:

    [...] Flash Silverlight Download [...]

Leave a Reply