<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>dotUpdate() &#187; Silverlight</title>
	<atom:link href="http://dotupdate.wordpress.com/category/silverlight/feed/" rel="self" type="application/rss+xml" />
	<link>http://dotupdate.wordpress.com</link>
	<description>Useful information about C# and .Net</description>
	<lastBuildDate>Mon, 28 Jan 2008 13:18:34 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='dotupdate.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/7f71844728c1706e14f53bc26005a0d7?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>dotUpdate() &#187; Silverlight</title>
		<link>http://dotupdate.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://dotupdate.wordpress.com/osd.xml" title="dotUpdate()" />
		<item>
		<title>Find the color of a point in a LinearGradientBrush</title>
		<link>http://dotupdate.wordpress.com/2008/01/28/find-the-color-of-a-point-in-a-lineargradientbrush/</link>
		<comments>http://dotupdate.wordpress.com/2008/01/28/find-the-color-of-a-point-in-a-lineargradientbrush/#comments</comments>
		<pubDate>Mon, 28 Jan 2008 12:54:22 +0000</pubDate>
		<dc:creator>dotupdate</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://dotupdate.wordpress.com/2008/01/28/find-the-color-of-a-point-in-a-lineargradientbrush/</guid>
		<description><![CDATA[I became intrigued by the problem of finding the color at any point of a rectangle that has been filled with a LinearGradientBrush.&#160; There is are techniques for doing this by essentially sampling the pixel at a rendered point but I wanted to approach the problem algorithmically.
I developed the following method which receives a rectangle [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotupdate.wordpress.com&blog=1510168&post=24&subd=dotupdate&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I became intrigued by the problem of finding the color at any point of a rectangle that has been filled with a LinearGradientBrush.&nbsp; There is are techniques for doing this by essentially sampling the pixel at a rendered point but I wanted to approach the problem algorithmically.</p>
<p>I developed the following method which receives a rectangle object that has been filled with a LinearGradientBrush and a point relative to the upper left corner of the rectangle and returns the calculated color at that point.&nbsp; There are no restrictions on the Start/Stop Points or the number of GradientStops.&nbsp; The ColorInterpolationMode can be either SRgbLinearInterpolation (default) or ScRgbLinearInterpolation.&nbsp; The GradientSpreadMethod is required to be the default (Pad).</p>
<p>There are two methods below:</p>
<p>Color GetColorAtPoint(Rectangle r, Point p) &#8211; finds the color at any point of a LinearGradientBrush filled rectangle.</p>
<p>private Double dist(Point px, Point po, Point pf) &#8211; helper method for GetColorAtPoint.</p>
<p>To use, copy both methods to your class and call GetColorAtPoint() with a rectangle and point.</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#008000;">//Calculates the color of a point in a rectangle that is filled</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#008000;">//with a LinearGradientBrush.</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">private</span> Color GetColorAtPoint(Rectangle theRec, Point thePoint)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">{</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#008000;">//Get properties</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    LinearGradientBrush br = (LinearGradientBrush)theRec.Fill;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">&nbsp;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">double</span> y3 = thePoint.Y;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">double</span> x3 = thePoint.X;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">&nbsp;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">double</span> x1 = br.StartPoint.X * theRec.ActualWidth;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">double</span> y1 = br.StartPoint.Y * theRec.ActualHeight;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    Point p1 = <span style="color:#0000ff;">new</span> Point(x1, y1); <span style="color:#008000;">//Starting point</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">&nbsp;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">double</span> x2 = br.EndPoint.X * theRec.ActualWidth;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">double</span> y2 = br.EndPoint.Y * theRec.ActualHeight;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    Point p2 = <span style="color:#0000ff;">new</span> Point(x2, y2);  <span style="color:#008000;">//End point</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">&nbsp;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#008000;">//Calculate intersecting points </span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    Point p4 = <span style="color:#0000ff;">new</span> Point(); <span style="color:#008000;">//with tangent</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">&nbsp;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">if</span> (y1 == y2) <span style="color:#008000;">//Horizontal case</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        p4 = <span style="color:#0000ff;">new</span> Point(x3, y1);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">&nbsp;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">else</span> <span style="color:#0000ff;">if</span> (x1 == x2) <span style="color:#008000;">//Vertical case</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">        p4 = <span style="color:#0000ff;">new</span> Point(x1, y3);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">&nbsp;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">else</span> <span style="color:#008000;">//Diagnonal case</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">double</span> m = (y2 - y1) / (x2 - x1);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">double</span> m2 = -1 / m;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">double</span> b = y1 - m * x1;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">double</span> c = y3 - m2 * x3;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">&nbsp;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">double</span> x4 = (c - b) / (m - m2);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">double</span> y4 = m * x4 + b;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">        p4 = <span style="color:#0000ff;">new</span> Point(x4, y4);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">&nbsp;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#008000;">//Calculate distances relative to the vector start</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">double</span> d4 = dist(p4, p1, p2);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">double</span> d2 = dist(p2, p1, p2);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">&nbsp;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">double</span> x = d4 / d2;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">&nbsp;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#008000;">//Clip the input if before or after the max/min offset values</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">double</span> max = br.GradientStops.Max(n =&gt; n.Offset);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">if</span> (x &gt; max)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        x = max;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">double</span> min = br.GradientStops.Min(n =&gt; n.Offset);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">if</span> (x &lt; min)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">        x = min;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">&nbsp;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#008000;">//Find gradient stops that surround the input value</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    GradientStop gs0 = br.GradientStops.Where(n =&gt; n.Offset &lt;= x).OrderBy(n =&gt; n.Offset).Last();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    GradientStop gs1 = br.GradientStops.Where(n =&gt; n.Offset &gt;= x).OrderBy(n =&gt; n.Offset).First();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">&nbsp;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">float</span> y = 0f;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">if</span> (gs0.Offset != gs1.Offset)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">        y = (<span style="color:#0000ff;">float</span>)((x - gs0.Offset) / (gs1.Offset - gs0.Offset));</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">&nbsp;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#008000;">//Interpolate color channels</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    Color cx = <span style="color:#0000ff;">new</span> Color();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">if</span> (br.ColorInterpolationMode == ColorInterpolationMode.ScRgbLinearInterpolation)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">float</span> aVal = (gs1.Color.ScA - gs0.Color.ScA) * y + gs0.Color.ScA;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">float</span> rVal = (gs1.Color.ScR - gs0.Color.ScR) * y + gs0.Color.ScR;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">float</span> gVal = (gs1.Color.ScG - gs0.Color.ScG) * y + gs0.Color.ScG;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">float</span> bVal = (gs1.Color.ScB - gs0.Color.ScB) * y + gs0.Color.ScB;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        cx = Color.FromScRgb(aVal, rVal, gVal, bVal);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">else</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">byte</span> aVal = (<span style="color:#0000ff;">byte</span>)((gs1.Color.A - gs0.Color.A) * y + gs0.Color.A);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">byte</span> rVal = (<span style="color:#0000ff;">byte</span>)((gs1.Color.R - gs0.Color.R) * y + gs0.Color.R);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">byte</span> gVal = (<span style="color:#0000ff;">byte</span>)((gs1.Color.G - gs0.Color.G) * y + gs0.Color.G);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">byte</span> bVal = (<span style="color:#0000ff;">byte</span>)((gs1.Color.B - gs0.Color.B) * y + gs0.Color.B);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        cx = Color.FromArgb(aVal, rVal, gVal, bVal);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">return</span> cx;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">}</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">&nbsp;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#008000;">//Helper method for GetColorAtPoint</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#008000;">//Returns the signed magnitude of a point on a vector with origin po and pointing to pf</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">double</span> dist(Point px, Point po, Point pf)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">{</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">double</span> d = Math.Sqrt((px.Y - po.Y) * (px.Y - po.Y) + (px.X - po.X) * (px.X - po.X));</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">if</span> (((px.Y &lt; po.Y) &amp;&amp; (pf.Y &gt; po.Y)) ||</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">        ((px.Y &gt; po.Y) &amp;&amp; (pf.Y &lt; po.Y)) ||</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        ((px.Y == po.Y) &amp;&amp; (px.X &lt; po.X) &amp;&amp; (pf.X &gt; po.X)) ||</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">        ((px.Y == po.Y) &amp;&amp; (px.X &gt; po.X) &amp;&amp; (pf.X &lt; po.X)))</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">        d = -d;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">return</span> d;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">}</pre>
</div>
</div>
<p>The method works by projecting the input point onto the vector that is described by the LinearGradientBrush Start/Stop points.&nbsp; There are special cases for horizontal and vertical lines.&nbsp; The width of the line is calculated as well as the relative distance of the projected point from the StartPoint (using the dist() method).&nbsp; The GradientStops are ordered and two stops are found that are the closest the projected point.&nbsp; The color channel values are interpolated based on the relative position of the projected point and these two stops.&nbsp; The values are assembled into a color and returned.</p>
<p>You can download a demo of this method <a href="http://home.comcast.net/~boddie/HelpWithColorPick.zip">here.</a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dotupdate.wordpress.com/24/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dotupdate.wordpress.com/24/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotupdate.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotupdate.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dotupdate.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dotupdate.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dotupdate.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dotupdate.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dotupdate.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dotupdate.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dotupdate.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dotupdate.wordpress.com/24/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotupdate.wordpress.com&blog=1510168&post=24&subd=dotupdate&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://dotupdate.wordpress.com/2008/01/28/find-the-color-of-a-point-in-a-lineargradientbrush/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/37b04d480ecd15e5890aa8cd26eb184a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dotupdate</media:title>
		</media:content>
	</item>
		<item>
		<title>Kaxaml: The XAML editor for me</title>
		<link>http://dotupdate.wordpress.com/2008/01/11/kaxaml-the-xaml-editor-for-me/</link>
		<comments>http://dotupdate.wordpress.com/2008/01/11/kaxaml-the-xaml-editor-for-me/#comments</comments>
		<pubDate>Fri, 11 Jan 2008 13:38:36 +0000</pubDate>
		<dc:creator>dotupdate</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://dotupdate.wordpress.com/2008/01/11/kaxaml-the-xaml-editor-for-me/</guid>
		<description><![CDATA[I highly recommend Kaxaml for your XAML editor.&#160; Not only does it look and work great but it also has some very useful features not found on other XAML editors.
I won&#8217;t bother listing all of the features that you can read about here, but my favorites are Intellisense support (with code completion!), snippets including a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotupdate.wordpress.com&blog=1510168&post=22&subd=dotupdate&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I highly recommend Kaxaml for your XAML editor.&nbsp; Not only does it look and work great but it also has some very useful features not found on other XAML editors.</p>
<p>I won&#8217;t bother listing all of the features that you can read about <a href="http://notstatic.com/archives/121">here</a>, but my favorites are Intellisense support (with code completion!), snippets including a generous collection of simple control styles and the ability to create and save your own, drag and drop, a beautiful color picker, and a XAML scrubber.</p>
<p>With other XAML editors, I thought, &#8220;what&#8217;s the point? Just use the designer in Visual Studio.&#8221;&nbsp; But Kaxaml is one tool that I will use <em>first</em> to build and customize my UIs&#8211;largely due to its snippets, colorpicker and ease of use.&nbsp; (I don&#8217;t use Blend.)</p>
<p>Download Kaxaml here: <a href="http://www.kaxaml.com">www.kaxaml.com</a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dotupdate.wordpress.com/22/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dotupdate.wordpress.com/22/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotupdate.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotupdate.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dotupdate.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dotupdate.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dotupdate.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dotupdate.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dotupdate.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dotupdate.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dotupdate.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dotupdate.wordpress.com/22/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotupdate.wordpress.com&blog=1510168&post=22&subd=dotupdate&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://dotupdate.wordpress.com/2008/01/11/kaxaml-the-xaml-editor-for-me/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/37b04d480ecd15e5890aa8cd26eb184a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dotupdate</media:title>
		</media:content>
	</item>
		<item>
		<title>Using WMP and Quicktime with Silverlight</title>
		<link>http://dotupdate.wordpress.com/2007/08/15/using-wmp-and-quicktime-with-silverlight/</link>
		<comments>http://dotupdate.wordpress.com/2007/08/15/using-wmp-and-quicktime-with-silverlight/#comments</comments>
		<pubDate>Wed, 15 Aug 2007 12:47:45 +0000</pubDate>
		<dc:creator>dotupdate</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://dotupdate.wordpress.com/2007/08/15/using-wmp-and-quicktime-with-silverlight/</guid>
		<description><![CDATA[Silverlight has a MediaElement control  which can be used for most cross-platform media display tasks.  I was disappointed that my application can not use it because it currently does not support playback at a variable rate.
My application requires a media player that can playback speech mp3 files at rates varying from 0.5 to 1.0x realtime.  Windows [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotupdate.wordpress.com&blog=1510168&post=7&subd=dotupdate&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Silverlight has a MediaElement control  which can be used for most cross-platform media display tasks.  I was disappointed that my application can not use it because it currently does not support playback at a variable rate.</p>
<p>My application requires a media player that can playback speech mp3 files at rates varying from 0.5 to 1.0x realtime.  Windows Media Player (WMP) is especially good at this in that the playback at reduced speed does not modify the pitch of the speech.</p>
<p>My solution to this problem is to instantiate a WMP or Quicktime player in the page depending on the platform and control the player from Silverlight C# code through a Javascript layer.</p>
<p>First, the player is installed in the body of the html page like this:</p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">&lt;!--Player goes here--&gt;
     &lt;div id=<span style="color:#006080;">"PlayerDiv"</span>&gt;&lt;/div&gt;    

 &lt;script type=<span style="color:#006080;">"text/javascript"</span>&gt;  

 <span style="color:#008000;">//Installs the player</span>
 var wmp;
 var qtp;
 document.getElementById(<span style="color:#006080;">'PlayerDiv'</span>).innerHTML = playerCreate(<span style="color:#006080;">'news clip.mp3'</span>);
 <span style="color:#0000ff;">if</span>(useWmp){
 wmp = document.getElementById(<span style="color:#006080;">'wmPlayer'</span>);
 }<span style="color:#0000ff;">else</span>{
 qtp = document.qtPlayer;
 }    

 &lt;/script&gt;</pre>
<p>The <em>playerCreate</em> function is a bit of Javascript that determines the browser capabilities and installs either a WMP or Quicktime player.  If IE is detected or Firefox is detected with the WMP component, then WMP is installed else use Quicktime.  (WMP is preferred over Quicktime for this application because while Quicktime has a rate control, the pitch is not preserved for mp3 playback.)  Here is the <em>playerCreate</em> Javascript:</p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">var useWmp;
    function playerCreate(url) {
    <span style="color:#008000;">//Find browser</span>
var userAgent = navigator.userAgent.toLowerCase();
var is_opera  = (userAgent.indexOf(<span style="color:#006080;">'opera'</span>) != -1);
var is_saf    = ((userAgent.indexOf(<span style="color:#006080;">'applewebkit'</span>) != -1) || (navigator.vendor == <span style="color:#006080;">"Apple Computer, Inc."</span>));
var is_webtv  = (userAgent.indexOf(<span style="color:#006080;">'webtv'</span>) != -1);
var is_ie     = ((userAgent.indexOf(<span style="color:#006080;">'msie'</span>) != -1) &amp;&amp; (!is_opera) &amp;&amp; (!is_saf) &amp;&amp; (!is_webtv));
var is_ie4    = ((is_ie) &amp;&amp; (userAgent.indexOf(<span style="color:#006080;">"msie 4."</span>) != -1));
var is_moz    = ((navigator.product == <span style="color:#006080;">'Gecko'</span>) &amp;&amp; (!is_saf));
var is_kon    = (userAgent.indexOf(<span style="color:#006080;">'konqueror'</span>) != -1);
var is_ns     = ((userAgent.indexOf(<span style="color:#006080;">'compatible'</span>) == -1) &amp;&amp; (userAgent.indexOf(<span style="color:#006080;">'mozilla'</span>) != -1) &amp;&amp; (!is_opera) &amp;&amp; (!is_webtv) &amp;&amp; (!is_saf));
var is_ns4    = ((is_ns) &amp;&amp; (parseInt(navigator.appVersion) == 4));   

    var str = <span style="color:#006080;">""</span>;
    <span style="color:#0000ff;">if</span> (is_ie) {
    <span style="color:#008000;">// create the WMP for IE </span>
    useWmp = <span style="color:#0000ff;">true</span>;
    str = <span style="color:#006080;">'&lt;object id="wmPlayer" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="0" height="0"&gt;'</span>;
<span style="color:#008000;">//    str += '&lt;param name="URL" value="'+url+'" /&gt;'; </span>
    str += <span style="color:#006080;">'&lt;param name="uiMode" value="none"&gt;'</span>;
    str += <span style="color:#006080;">'&lt;param name="autoStart" value="false"&gt;'</span>;
    str += <span style="color:#006080;">'&lt;/object&gt;'</span>;
    <span style="color:#0000ff;">return</span> str;  

    }<span style="color:#0000ff;">else</span> <span style="color:#0000ff;">if</span>(is_moz &amp;&amp; navigator.plugins[<span style="color:#006080;">"Microsoft® Windows Media Player Firefox Plugin"</span>]){
    <span style="color:#008000;">// create WMP for FF. </span>
    useWmp = <span style="color:#0000ff;">true</span>;
    str = <span style="color:#006080;">'&lt;object id="wmPlayer" type="application/x-ms-wmp" data="'</span>+url+<span style="color:#006080;">'" width="0" height="0"&gt;'</span>;
<span style="color:#008000;">//    str += '&lt;param name="URL" value="'+url+'" /&gt;'; </span>
    str += <span style="color:#006080;">'&lt;param name="uiMode" value="none"&gt;'</span>;
    str += <span style="color:#006080;">'&lt;param name="autoStart" value="false"&gt;'</span>;
    str += <span style="color:#006080;">'&lt;/object&gt;'</span>;
    <span style="color:#0000ff;">return</span> str;      

    }<span style="color:#0000ff;">else</span>{
    <span style="color:#008000;">//try to create the qtp as last resort</span>
    useWmp = <span style="color:#0000ff;">false</span>;
    str =<span style="color:#006080;">'&lt;object id="qtPlayer" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab"  width="0" height="0" &gt;'</span>;
<span style="color:#008000;">//    str += '&lt;param name="src" value="'+url+'"&gt;';</span>
    str += <span style="color:#006080;">'&lt;param name="autoplay" value="false"&gt;'</span>;
    str += <span style="color:#006080;">'&lt;embed  name="qtPlayer" width="0" height="0" src="qtStartUp.mp3" mce_src="qtStartUp.mp3" AUTOPLAY="false" TYPE="audio/mpeg" PLUGINSPAGE="www.apple.com/quicktime/download" enablejavascript="true"&gt;'</span>;
    str += <span style="color:#006080;">'  &lt;/embed&gt; '</span>;
    str += <span style="color:#006080;">'&lt;/object&gt; '</span>;
    <span style="color:#0000ff;">return</span> str;  

}
}</pre>
<p>A set of Javascript functions were defined in order to create a common API for the Silverlight C# code to use.</p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">function playerPlay(){
<span style="color:#0000ff;">if</span>(useWmp){
wmp.controls.play();
}<span style="color:#0000ff;">else</span>{
qtp.Play();
}
}  

function playerStop(){
<span style="color:#0000ff;">if</span>(useWmp){
wmp.controls.stop();
}<span style="color:#0000ff;">else</span>{
qtp.Stop();
qtp.Rewind();
}
}   

function playerPause(){
<span style="color:#0000ff;">if</span>(useWmp){
wmp.controls.pause();
}<span style="color:#0000ff;">else</span>{
qtp.Stop();
}
}   

function playerUrl(url){  

<span style="color:#0000ff;">if</span>(useWmp){  

wmp.URL = url;
}<span style="color:#0000ff;">else</span>{
qtp.SetURL(url);
qtp.Stop();
}
}   

function playerGetPosition(){
<span style="color:#0000ff;">if</span>(useWmp){
<span style="color:#0000ff;">return</span> wmp.controls.currentPosition;
}<span style="color:#0000ff;">else</span>{
<span style="color:#0000ff;">return</span> qtp.GetTime()/600;
}
}   

function playerSetPosition(t){
<span style="color:#0000ff;">if</span>(useWmp){
wmp.controls.currentPosition = t;
}<span style="color:#0000ff;">else</span>{
qtp.SetTime(t*600); <span style="color:#008000;">//int</span>
}
}   

function playerSetVolume(v){
<span style="color:#0000ff;">if</span>(useWmp){
wmp.settings.volume = v;
}<span style="color:#0000ff;">else</span>{
qtp.SetVolume(v); <span style="color:#008000;">//int 0-100</span>
}
}   

function playerSetRate(r){
<span style="color:#0000ff;">if</span>(useWmp){
wmp.settings.rate = r;
}<span style="color:#0000ff;">else</span>{
qtp.SetRate(r); <span style="color:#008000;">//flost 0.5-1.0</span>
}
}</pre>
<p>These functions are called by a single event handler that manages calls from the Silverlight C# code using the techniques for C# to Javascript communications described in the <a href="http://silverlight.net/quickstarts/">QuickStart</a> guide.</p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#008000;">//Event handler for Silverlight control</span>
function agEventHandler(sender, args){
<span style="color:#0000ff;">switch</span> (args.Name){
<span style="color:#0000ff;">case</span> <span style="color:#006080;">'stop'</span>:
playerStop();
<span style="color:#0000ff;">break</span>;
<span style="color:#0000ff;">case</span> <span style="color:#006080;">'play'</span>:
playerPlay();
<span style="color:#0000ff;">break</span>;
<span style="color:#0000ff;">case</span> <span style="color:#006080;">'pause'</span>:
playerPause();
<span style="color:#0000ff;">break</span>;
<span style="color:#0000ff;">case</span> <span style="color:#006080;">'url'</span>:
playerUrl(args.Sdata);
<span style="color:#0000ff;">break</span>;
<span style="color:#0000ff;">case</span> <span style="color:#006080;">'position'</span>:
playerSetPosition(args.Data);
<span style="color:#0000ff;">break</span>;
<span style="color:#0000ff;">case</span> <span style="color:#006080;">'volume'</span>:
playerSetVolume(args.Data);
<span style="color:#0000ff;">break</span>;
<span style="color:#0000ff;">case</span> <span style="color:#006080;">'rate'</span>:
playerSetRate(args.Data);
<span style="color:#0000ff;">break</span>;  

<span style="color:#0000ff;">default</span>:
}</pre>
<p>The event handler arguments are defined on the C# side as follows:</p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#008000;">//This class defines the arguments for the control's events</span>
[Scriptable]
<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> CustomEventArgs : EventArgs
{
    <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">double</span> data;
    <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">string</span> name;
    <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">string</span> sdata;  

    <span style="color:#0000ff;">public</span> CustomEventArgs(<span style="color:#0000ff;">string</span> iname, <span style="color:#0000ff;">double</span> idata, <span style="color:#0000ff;">string</span> isdata)
    {
        data = idata;
        name = iname;
        sdata = isdata;
    }  

    [Scriptable]
    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> Name
    {
        get { <span style="color:#0000ff;">return</span> name; }
        set { name = <span style="color:#0000ff;">value</span>; }
    }  

    [Scriptable]
    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">double</span> Data
    {
        get { <span style="color:#0000ff;">return</span> data; }
        set { data = <span style="color:#0000ff;">value</span>; }
    }  

    [Scriptable]
    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> Sdata
    {
        get { <span style="color:#0000ff;">return</span> sdata; }
        set { sdata = <span style="color:#0000ff;">value</span>; }
    }
}//End of <span style="color:#0000ff;">class</span></pre>
<p>This class of custom event arguments allows the passing of a string name which specifies the media player event to control as well as a double and string parameter for data.</p>
<p>The event method is defined in the C# code like this:</p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#008000;">//This defines the event from the control</span>
<span style="color:#008000;">//This is an example of how to fire an event from the control to Javascript</span>
<span style="color:#008000;">//AgEvents(this, new CustomEventArgs("hello", 25, "something"));</span>
[Scriptable]
<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">event</span> EventHandler&lt;CustomEventArgs&gt; AgEvents;</pre>
<p>Finally, here is an example of C# code controlling the media player:</p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">void</span> play()
{
    <span style="color:#0000ff;">if</span> (playerState != (<span style="color:#0000ff;">int</span>)playerStates.paused)
    {
        AgEvents(<span style="color:#0000ff;">this</span>, <span style="color:#0000ff;">new</span> CustomEventArgs(<span style="color:#006080;">"position"</span>, t1, <span style="color:#0000ff;">null</span>));
    }  

    AgEvents(<span style="color:#0000ff;">this</span>, <span style="color:#0000ff;">new</span> CustomEventArgs(<span style="color:#006080;">"play"</span>, 0, <span style="color:#0000ff;">null</span>));</pre>
<p> You can see a demo of this application <a href="http://home.comcast.net/~boddie/TestWMP.html">here</a>.</p>
<p> You can download the demo project <a href="http://home.comcast.net/~boddie/MediaDemo.zip">here</a>.</p>
<p>Hopefully, future releases of Silverlight will have a MediaElement with a rate control so that my application can be greatly simplified.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dotupdate.wordpress.com/7/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dotupdate.wordpress.com/7/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotupdate.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotupdate.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dotupdate.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dotupdate.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dotupdate.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dotupdate.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dotupdate.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dotupdate.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dotupdate.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dotupdate.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotupdate.wordpress.com&blog=1510168&post=7&subd=dotupdate&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://dotupdate.wordpress.com/2007/08/15/using-wmp-and-quicktime-with-silverlight/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/37b04d480ecd15e5890aa8cd26eb184a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dotupdate</media:title>
		</media:content>
	</item>
		<item>
		<title>Timer for Silverlight 1.1</title>
		<link>http://dotupdate.wordpress.com/2007/08/13/timer-for-silverlight-11/</link>
		<comments>http://dotupdate.wordpress.com/2007/08/13/timer-for-silverlight-11/#comments</comments>
		<pubDate>Mon, 13 Aug 2007 15:27:22 +0000</pubDate>
		<dc:creator>dotupdate</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://dotupdate.wordpress.com/2007/08/13/timer-for-silverlight-11/</guid>
		<description><![CDATA[I needed a timer control for a Silverlight 1.1 application but it is not supported in the Alpha release.  My solution was to use the timer in Javascript and have it send events to the C# code.
Event sending from Javascript to C# is described in the Silverlight QuickStart document.
Here is the initialization of the timer [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotupdate.wordpress.com&blog=1510168&post=5&subd=dotupdate&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I needed a timer control for a Silverlight 1.1 application but it is not supported in the Alpha release.  My solution was to use the timer in Javascript and have it send events to the C# code.</p>
<p>Event sending from Javascript to C# is described in the Silverlight QuickStart document.</p>
<p>Here is the initialization of the timer in Javascript:</p>
<pre><span style="color:#008000;">//initialize silverlight event hookup and start timer
</span><span style="color:#0000ff;">var</span> control;
<span style="color:#0000ff;">function</span> init(sender){
control = sender;
control.Content.basic.AgEvents = agEventHandler;
setInterval(jtimer,10);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a>In this routine, the timer is set to execute the <em>jtimer</em> function every 10 ms.  (The agEvents statement is used for sending information <em>to</em> the Javascript.  I will describe this later.)</p>
<p>The <em>init</em> function is called by the <em>CreateSilverlight</em> function in the page.js file:</p>
<pre><span style="color:#008000;">// JScript source code 

//contains calls to silverlight.js, example below loads Page.xaml
</span><span style="color:#0000ff;">function</span> createSilverlight()
{
    Silverlight.createObjectEx({
        source: <span style="color:#a31515;">"Page.xaml"</span>,
        parentElement: document.getElementById(<span style="color:#a31515;">"SilverlightControlHost"</span>),
        id: <span style="color:#a31515;">"SilverlightControl"</span>,
        properties: {
            width: <span style="color:#a31515;">"100%"</span>,
            height: <span style="color:#a31515;">"100%"</span>,
            version: <span style="color:#a31515;">"1.1"</span>,
            enableHtmlAccess: <span style="color:#a31515;">"true"</span>,
            isWindowless: <span style="color:#a31515;">"True"
</span>        },
        events: {
        onLoad: init
        }
    }); 

    <span style="color:#008000;">// Give the keyboard focus to the Silverlight control by default
</span>    document.body.onload = <span style="color:#0000ff;">function</span>() {
      <span style="color:#0000ff;">var</span> silverlightControl = document.getElementById(<span style="color:#a31515;">'SilverlightControl'</span>);
      <span style="color:#0000ff;">if</span> (silverlightControl)
      silverlightControl.focus();
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Here is the <em>jtimer</em> function:</p>
<pre><span style="color:#008000;">//Send position of player to silverlight based on a timer
</span><span style="color:#0000ff;">function</span> jtimer(){
control.Content.basic.timer(playerGetPosition());
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>In this function, a function <em>timer </em>is called in the C# code with a value that is passed as an argument.</p>
<p>On the C# side, the <em>timer</em> function looks like this:</p>
<pre>        <span style="color:#008000;">//This receives the timer event from Javascript
</span>        [<span style="color:#2b91af;">Scriptable</span>]
        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> timer(<span style="color:#0000ff;">double</span> arg)
        {
            tnow = arg;</pre>
<p>&#8230;</p>
<p>Note the &#8216;[Scriptable]&#8216; tag which makes the timer function usable from Javascript.</p>
<p>Finally, the page load event needs some boilerplate to hook the Javascript to C# communications:</p>
<pre>        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> Page_Loaded(<span style="color:#0000ff;">object</span> o, <span style="color:#2b91af;">EventArgs</span> e)
        {
            <span style="color:#008000;">// Required to initialize variables
</span>            InitializeComponent();
            <span style="color:#008000;">//Register the scriptable endpoints
</span>            <span style="color:#2b91af;">WebApplication</span>.Current.RegisterScriptableObject(<span style="color:#a31515;">"basic"</span>, <span style="color:#0000ff;">this</span>);</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dotupdate.wordpress.com/5/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dotupdate.wordpress.com/5/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotupdate.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotupdate.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dotupdate.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dotupdate.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dotupdate.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dotupdate.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dotupdate.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dotupdate.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dotupdate.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dotupdate.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotupdate.wordpress.com&blog=1510168&post=5&subd=dotupdate&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://dotupdate.wordpress.com/2007/08/13/timer-for-silverlight-11/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/37b04d480ecd15e5890aa8cd26eb184a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dotupdate</media:title>
		</media:content>
	</item>
	</channel>
</rss>