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
Color Picker [Flash 9, AS3] (11.1 KiB, 1,522 hits)
Color Picker [Silverlight 2, C#] (20.6 KiB, 2,896 hits)
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);
}
