<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
     >
  <channel>
    <title>mwcz</title>
    <link>http://mwcz.org</link>
    <description>I finally created a blog.</description>
    <pubDate>Fri, 03 Aug 2012 02:01:15 GMT</pubDate>
    <generator>Blogofile</generator>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>ColorPal Output Update</title>
      <link>http://mwcz.org/2012/07/30/colorpal-output-update</link>
      <pubDate>Mon, 30 Jul 2012 22:30:00 EDT</pubDate>
      <category><![CDATA[color]]></category>
      <category><![CDATA[javascript]]></category>
      <category><![CDATA[html5]]></category>
      <guid>b'0RV63il0gQrfDlxk72F62IT9wy8='</guid>
      <description>ColorPal Output Update</description>
      <content:encoded><![CDATA[<p>Hi pals.</p>
<p>This is a speedy post about a speedy update I made today to
<a href="/projects/colorpal/">ColorPal</a>, an HTML5 tool I wrote that automatically
generates color palettes from a photograph.</p>
<p>Previously, ColorPal provided hex codes for each color extracted from the
image.  Eight individual &lt;input&gt;s at the bottom of the page displayed the
colors.</p>
<p>I use ColorPal a lot (my own dogfood and all that) when designing websites, and
it wasn't long before I got fed up (hah!) with copying values from eight
freaking inputs.</p>
<p>Blah:</p>
<p><img alt="Before" src="/img/022/before.png" /></p>
<p>Huzzah:</p>
<p><img alt="After" src="/img/022/after.png" /></p>
<p>All eight colors can be pasted out in one fell swoop of the mouse.</p>
<p><a href="/projects/colorpal/">ColorPal Demo</a></p>
<p>Works in Chrome and Firefox.</p>
<p><strong>Pro tip</strong>: you can edit the values in the textarea and the palette will be
instantly updated to reflect your edits.  This means you can also paste in
a list of colors (hex format only for the time being).</p>
<p>If you it out ColorPal, shoot me an <a href="mailto:mwc@clayto.org">email</a> or give me a
shout on <a href="https://twitter.com/#!/mwcz">Twitter</a> with your thoughts.</p>
<p>Here are github repos for <a href="https://github.com/mwcz/ColorPal">ColorPal</a> and
<a href="https://github.com/mwcz/median-cut-js">median-cut.js</a>.</p>
<p>This minor update is part of the groundwork for some exciting changes to my
sorely neglected <a href="http://clayto.com/">photography site</a>.  I still post photos
regularly, but haven't given it any fresh coding love for about two years.  See
how the colorscheme of each page matches the photograph?  That's done with an
older palette generation implementation (which I should write about someday),
but pretty soon it'll be ColorPal pumping out those swatches.<br />
</p>
<p>That's not the only change.  If you're not excited, don't worry.  I am!</p>
<p><a href="/projects/colorpal"><img src="/img/019/colorpal_logo.png" alt="ColorPal logo" title=""
/></a></p>
<p>Happy coloring!</p>]]></content:encoded>
    </item>
    <item>
      <title>Canvas Indexed Color</title>
      <link>http://mwcz.org/2012/07/03/canvas-indexed-color</link>
      <pubDate>Tue, 03 Jul 2012 23:44:50 EDT</pubDate>
      <category><![CDATA[color]]></category>
      <category><![CDATA[canvas]]></category>
      <category><![CDATA[html5]]></category>
      <guid>b'XpdBlTOAZ6TCkjP7EhUPkKW6KX0='</guid>
      <description>Canvas Indexed Color</description>
      <content:encoded><![CDATA[<p><a href="/projects/canvas_indexed_color" title="Canvas Indexed Color demo">Canvas Indexed Color demo</a></p>
<p><img alt="White SVG Tiger" src="/img/021/hahatiger.png" /></p>
<p>First, I converted the classic SVG tiger into a set of canvas drawing
instructions using <a href="http://professorcloud.com/svg-to-canvas/" title="Professor Cloud&apos;s conversion
tool">Professor Cloud's conversion
tool</a>.  The output looks like
<a href="https://github.com/mwcz/mwcz/blob/master/blog/projects/canvas_indexed_color/vector_images.js" title="Tiger image">this</a>.  Each call to <code>cvm.getColor()</code> used to be a string literal.<br />
</p>
<p>Vim's regex saved me from having to edit <em>100,000</em> lines of canvas instructions
by hand to replace the color strings.</p>
<p>The demo uses <a href="http://knockoutjs.com/" title="Knockout JS">Knockout</a> for handling all
the update/draw events as well as updating the URL hash.  Check the <a href="https://github.com/mwcz/mwcz/blob/master/blog/projects/canvas_indexed_color/cic.js" title="source code">source
code</a> and you'll see that there <em>isn't</em> a vast tangled nest of event
wirings.  Each time one of the colors in this ViewModel is changed, Knockout
automatically triggers the canvas redraw and the updates the URL with the new
palette.  Conversely, if the URL is changed, the ViewModel will update itself
with the new value.  Knockout calls this a "two-way data binding".<br />
</p>
<p>I replaced the aforementioned color strings with calls to <code>cvm</code>, which is a
Knockout ViewModel (in this case, an object that holds all the color data).
<code>cvm</code> is populated with the SVG tiger's default colors.</p>
<div class="pygments_murphy syntax_highlight"><pre>    <span class="nx">ko</span><span class="p">.</span><span class="nx">observableArray</span><span class="p">(</span> <span class="p">[</span><br/>        <span class="p">{</span> <span class="nx">hex</span><span class="o">:</span> <span class="nx">ko</span><span class="p">.</span><span class="nx">observable</span><span class="p">(</span><span class="s2">&quot;#000000&quot;</span><span class="p">)</span> <span class="p">},</span><br/>        <span class="p">{</span> <span class="nx">hex</span><span class="o">:</span> <span class="nx">ko</span><span class="p">.</span><span class="nx">observable</span><span class="p">(</span><span class="s2">&quot;#323232&quot;</span><span class="p">)</span> <span class="p">},</span><br/>        <span class="c1">// .... many more ....</span><br/>        <span class="p">{</span> <span class="nx">hex</span><span class="o">:</span> <span class="nx">ko</span><span class="p">.</span><span class="nx">observable</span><span class="p">(</span><span class="s2">&quot;#ff727f&quot;</span><span class="p">)</span> <span class="p">},</span><br/>        <span class="p">{</span> <span class="nx">hex</span><span class="o">:</span> <span class="nx">ko</span><span class="p">.</span><span class="nx">observable</span><span class="p">(</span><span class="s2">&quot;#ffffff&quot;</span><span class="p">)</span> <span class="p">}</span><br/>    <span class="p">]);</span><br/></pre></div>

<p>Play with the colors, then copy the URL and send your tiger to your friends.  :]</p>
<p><a href="/projects/canvas_indexed_color/#4c0000,#659900,#666666,#992600,#999999,#99cc32,#a51926,#a5264c,#b23259,#b26565,#b2b2b2,#cc3f4c,#cc7226,#cccccc,#e5668c,#e59999,#e5e5b2,B5E8E6,#ea8c4d,#ea8e51,#eb955c,#ec9961,#eea575,#efaa7c,#f1b288,#f2b892,#f3bf9c,#f4c6a8,#f5ccb0,#f8d8c4,#f8dcc8,#f9e2d3,#fae5d7,#fcf2eb,#ff727f,#ffffcc,#ffffff,#000000,#323232" title="Crazy tiger">Crazy tiger</a>.</p>
<p>Please note, this is only a simulation of an indexed color palette.  It is not
a true, usable, indexed-color API for canvas (someday, maybe!).</p>
<p>The color picker is <a href="https://github.com/mattfarina/farbtastic" title="Farbtastic">Farbtastic</a>.  If you're interested in reading more about the history of
indexed color, and a <strong>mind-blowing</strong> canvas demo, go
<a href="http://www.effectgames.com/effect/article.psp.html/joe/Old_School_Color_Cycling_with_HTML5" title="Color cycling in HTML5 canvas">here</a>.</p>]]></content:encoded>
    </item>
    <item>
      <title>Bootstrap & Blogofile</title>
      <link>http://mwcz.org/2012/07/03/bootstrap---blogofile</link>
      <pubDate>Tue, 03 Jul 2012 22:44:50 EDT</pubDate>
      <category><![CDATA[bootstrap]]></category>
      <guid>b'XJkbd3rjOIr4gmpxrfE0BwsB9Ro='</guid>
      <description>Bootstrap & Blogofile</description>
      <content:encoded><![CDATA[<p>Surprise.</p>
<p>I've converted the site layout to
<a href="http://twitter.github.com/bootstrap" title="Twitter Bootstrap">Bootstrap</a>.</p>
<p>Massive appreciation to <a href="http://douglatornell.ca" title="Doug
Latornell">Doug Latornell</a> for continuing development of Blogofile, the static blog compiler
that powers this site.  His contributions can be found at the official <a href="https://github.com/EnigmaCurry/blogofile/" title="Blogofile GitHub repo">blogofile GitHub repo</a>.</p>
<p>Converting the layout to Bootstrap was a breeze thanks to the Mako templates.</p>
<p>This is a lackluster post, but I didn't want to let such a big layout change go
by without mention.  Customization of the default Bootstrap theme is coming.</p>
<p><em>Update</em> Thanks Doug, I've updated the GitHub link to point to the official repo.</p>]]></content:encoded>
    </item>
    <item>
      <title>AFOMFT</title>
      <link>http://mwcz.org/2012/05/10/afomft</link>
      <pubDate>Thu, 10 May 2012 21:47:00 EDT</pubDate>
      <category><![CDATA[git]]></category>
      <category><![CDATA[python]]></category>
      <category><![CDATA[javascript]]></category>
      <category><![CDATA[amiga]]></category>
      <category><![CDATA[django]]></category>
      <category><![CDATA[html5]]></category>
      <category><![CDATA[linux]]></category>
      <category><![CDATA[stackoverflow]]></category>
      <category><![CDATA[vim]]></category>
      <category><![CDATA[bash]]></category>
      <guid>b'I0R9UD37xiEgR9UDoUEtFlF7QZU='</guid>
      <description>AFOMFT</description>
      <content:encoded><![CDATA[<p><img alt="A few of my favorite things" src="/img/017/afomft.png" /></p>]]></content:encoded>
    </item>
    <item>
      <title>ColorPal palettes improved!</title>
      <link>http://mwcz.org/2012/02/10/colorpal-palettes-improved!</link>
      <pubDate>Fri, 10 Feb 2012 22:44:50 EST</pubDate>
      <category><![CDATA[color]]></category>
      <category><![CDATA[javascript]]></category>
      <category><![CDATA[html5]]></category>
      <guid>b'oJHLN3K51SbbtoLgDiDfCafURCk='</guid>
      <description>ColorPal palettes improved!</description>
      <content:encoded><![CDATA[<p><a href="/projects/colorpal"><img src="/img/019/colorpal_logo.png" alt="ColorPal logo" title=""
/></a></p>
<p>In my <a href="/2012/01/16/colorpal-alpha/">last post</a> on the subject, I introduced
ColorPal, my HTML5 color palette generation tool.  It didn't perform well with
certain types of images, so I fixed it. :)</p>
<p>Color palettes will now match the image even better.  Especially for images
with infrequent but <em>important</em> colors.  Here's a comparison of the old and new
methods, on an image that is mostly black:</p>
<p><img alt="comparison of palettes generated with median and
mean" src="/img/019/median-mean-comparison.png" /></p>
<p>You can see that with the old method, the black pixels definitely took over the
palette.</p>
<p><a href="/projects/colorpal">Try it with your own images!</a></p>
<p>For some images, this change won't affect palettes at all.  But for images with
low-population, high-importance colors, like the one above, palettes will look
<em>much</em> better!</p>
<p>Technical discussion below.</p>
<h1>Why the 'median' in median-cut?</h1>
<p>Median Cut is an algorithm typically used to reduce the number of colors in an
image.</p>
<p><img alt="16 million colors versus 16 colors" src="/img/019/median-cut-example.png" /></p>
<p>The steps to perform median cut are fairly straightforward:</p>
<ol>
<li>
<p>Find the smallest box which contains all the colors.</p>
</li>
<li>
<p>Sort the enclosed colors along the longest axis of the box.</p>
</li>
<li>
<p>Split the box into two regions at median of the sorted list.</p>
</li>
<li>
<p>Repeat the above process until the original color space has been divided
     into N regions where N is the number of colors you want.</p>
</li>
</ol>
<p>Not too bad, right?  The question remains, though, why median?</p>
<p>Mean, median, and mode all attempt to measure the
<a href="http://en.wikipedia.org/wiki/Location_parameter">location</a> of a probability
distribution.  Worded more intuitively... they try to find the center of a set
of numbers.  They just employ different definitions of "center".</p>
<p><img alt="mean, median, and mode graphed" src="/img/019/mean_median_mode.png" />
(Thanks <a href="http://en.wikipedia.org/wiki/Median">Wikipedia</a>)</p>
<p>As you can see clearly in the dashed data set, the mean tends to follow the
tail.  In other words, mean is useful when outliers are important.  When
generating a color palette, outliers are essential.  Outliers may be the small
streak of color in an otherwise drab sky, or a small red rose in the middle of
a green field.</p>
<p>The goal of median cut is <em>not</em> to generate a color palette.  It's to
efficiently reduce the number of colors in an image.  Since my goal with
ColorPal is to find good-lookin' colors, I've modified the algorithm to split
boxes at the <strong>mean</strong> instead of the <strong>median</strong>.  Mean splits boxes closer to
the outliers, which keeps low-population colors nicely segregated from the
high-population colors.</p>
<p>Thus step 3 becomes:</p>
<div class="pygments_murphy syntax_highlight">
  3. Split the box into two regions at the <b>mean</b> of the sorted list.
</div>

<p>The result?  Mean Cut!  Also: awesome color palettes.</p>
<h2>Interesting side-note on color distributions</h2>
<p>Interestingly, photographs of nature tend to have nice, close-to-normal color
distributions.  A "normal distribution" is a formal term for a "bell curve".</p>
<p>Check out the red, green, and blue distributions for a photograph of a forest.</p>
<p><img alt="Forest red   pixel distribution" src="/img/019/forest_r.png" />
<img alt="Forest green pixel distribution" src="/img/019/forest_g.png" />
<img alt="Forest blue  pixel distribution" src="/img/019/forest_b.png" /></p>
<p>For comparison, here are the distributions for a shot of NYC.</p>
<p><img alt="City   red   pixel distribution" src="/img/019/city_r.png" />
<img alt="City   green pixel distribution" src="/img/019/city_g.png" />
<img alt="City   blue  pixel distribution" src="/img/019/city_b.png" /></p>
<h2>What's next for ColorPal?  Color spaces.</h2>
<p>Mean-cut improves ColorPal dramatically for some images, but it's still not
perfect.  Perfection probably won't be attainable, but there are still many
ways to improve.</p>
<p>My next test will be converting to a color space other than RGB.</p>
<p>HSL is common, and the RGB/HSL conversion formulas are in my very old JSImage
project.  It may not turn out well, though, as the concept of "widest box"
loses most of its meaning when each axis is a completely different unit of
measure.  Although, in a way, I suppose RGB has a similar problem.  Red, Green,
and Blue could be thought of as unrelated units.  I'll try it out and see how
it goes.  I wonder if my old JavaScript RGB/HSL code has rotted away yet...</p>
<p>Most likely, I'll skip HSL and try one of the
<a href="http://en.wikipedia.org/wiki/Lab_color_space">Lab</a> color spaces first, since
they are not composed linear values like RGB and HSL.  Instead, Lab color
spaces use non-linear scales that closely match human perception of color.
Perfect for my purposes.  How fortuitious for me that CIE invented it way back
in 1931.</p>
<p>There are three "Lab" color spaces: XYZ, Hunter-Lab,
and CIELAB.  XYZ will be first to the plate, since the RGB/XYZ formulas are
the simplest.  Let me take this opportune moment to plug
<a href="http://www.easyrgb.com/index.php?X=MATH">EasyRGB.com</a>.</p>
<h2>Lend a hand</h2>
<p>As I've mentioned before, ColorPal is in an early stage of development.  It
needs testing in multiple browsers, and I'm very interested in hearing people's
feedback.  Code development help is welcomed too.</p>
<p>If you try out ColorPal, shoot me an <a href="mailto:mwc@clayto.org">email</a>
or give me a shout on <a href="https://twitter.com/#!/mwcz">Twitter</a> with your
thoughts.  Thanks!</p>
<p>Here are github repos for <a href="https://github.com/mwcz/ColorPal">ColorPal</a> and
<a href="https://github.com/mwcz/median-cut-js">median-cut.js</a>.</p>]]></content:encoded>
    </item>
    <item>
      <title>Toy javascript unit tester</title>
      <link>http://mwcz.org/2012/01/30/toy-javascript-unit-tester</link>
      <pubDate>Mon, 30 Jan 2012 21:50:00 EST</pubDate>
      <category><![CDATA[javascript]]></category>
      <category><![CDATA[testing]]></category>
      <guid>b'JwjZs5NqO7otncYUL432iLrBdFI='</guid>
      <description>Toy javascript unit tester</description>
      <content:encoded><![CDATA[<p>Last week I was sitting in a meeting, thinking about unit testing frameworks (not the subject of the meeting), and had a minor epiphany.  In the back of my mind, there has always been a bit of uncertainty as to how unit testing frameworks work.  Deep in my brain I knew there was something I didn't understand, but I wasn't quite sure what.  I never gave it any conscious thought.</p>
<p>As a developer, I'd use a framework, write a bunch of <code>testBlahFoo</code> functions, and the framework would magically run them.<br />
</p>
<p>Then I realized... "Oh, it's just introspection."</p>
<div class="pygments_murphy syntax_highlight"><pre><span class="c1">// Create an object with three properties, all functions</span><br/><span class="kd">var</span> <span class="nx">myfuncs</span> <span class="o">=</span> <span class="p">{</span><br/>    <span class="nx">func1</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;func it up&quot;</span><span class="p">);</span> <span class="p">},</span><br/>    <span class="nx">func2</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;func&#39;s old brother&quot;</span><span class="p">);</span> <span class="p">},</span><br/>    <span class="nx">func3</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;bring back da func&quot;</span><span class="p">);</span> <span class="p">}</span><br/><span class="p">};</span><br/><br/><span class="c1">// Run all functions attached to myfuncs</span><br/><span class="k">for</span><span class="p">(</span> <span class="nx">func</span> <span class="k">in</span> <span class="nx">myfuncs</span> <span class="p">)</span> <span class="p">{</span><br/>    <span class="nx">myfuncs</span><span class="p">[</span> <span class="nx">func</span> <span class="p">]();</span><br/><span class="p">}</span><br/><br/><span class="c1">// Outputs:</span><br/><span class="c1">// </span><br/><span class="c1">// func it up</span><br/><span class="c1">// func&#39;s old brother</span><br/><span class="c1">// bring back da func</span><br/></pre></div>

<p>So simple.  Later, I wrote a very basic unit testing framework, purely as an educational excercise.</p>
<p>The guts are almost simple as the example above.</p>
<div class="pygments_murphy syntax_highlight"><pre><span class="kd">var</span> <span class="nx">JTestSuite</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>                                                                                                                                                                         <br/><br/>    <span class="kd">var</span> <span class="nx">version</span> <span class="o">=</span> <span class="mf">0.1</span><span class="p">,</span><br/>        <span class="nx">jt</span><span class="p">,</span><br/><br/>    <span class="nx">init</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">callback</span> <span class="p">)</span> <span class="p">{</span><br/>        <span class="nx">jt</span> <span class="o">=</span> <span class="nx">JTests</span><span class="p">();</span><br/>        <span class="nx">jt</span><span class="p">.</span><span class="nx">init</span><span class="p">(</span> <span class="nx">callback</span> <span class="p">);</span><br/>    <span class="p">},</span><br/><br/>    <span class="nx">run</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br/>        <span class="kd">var</span> <span class="nx">fn</span><span class="p">,</span><br/>            <span class="nx">prop</span><span class="p">;</span><br/><br/>        <span class="c1">// Run all functions that begin with &quot;test&quot;</span><br/>        <span class="k">for</span><span class="p">(</span> <span class="nx">prop</span> <span class="k">in</span> <span class="k">this</span> <span class="p">)</span> <span class="p">{</span><br/>            <span class="k">if</span><span class="p">(</span> <span class="k">typeof</span> <span class="k">this</span><span class="p">[</span><span class="nx">prop</span><span class="p">]</span> <span class="o">===</span> <span class="s2">&quot;function&quot;</span> <span class="p">)</span> <span class="p">{</span><br/>                <span class="k">if</span><span class="p">(</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span> <span class="o">===</span> <span class="s2">&quot;test&quot;</span> <span class="p">)</span> <span class="p">{</span><br/>                    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span> <span class="k">this</span><span class="p">[</span><span class="nx">prop</span><span class="p">](</span><span class="nx">jt</span><span class="p">)</span> <span class="p">);</span><br/>                <span class="p">}</span><br/>            <span class="p">}</span><br/>        <span class="p">}</span><br/>    <span class="p">};</span><br/><br/>    <span class="k">if</span><span class="p">(</span> <span class="o">!</span><span class="p">(</span><span class="k">this</span> <span class="k">instanceof</span> <span class="nx">JTestSuite</span><span class="p">)</span> <span class="p">)</span> <span class="k">return</span> <span class="k">new</span> <span class="nx">JTestSuite</span><span class="p">();</span><br/><br/>    <span class="k">return</span> <span class="p">{</span><br/>        <span class="nx">init</span> <span class="o">:</span> <span class="nx">init</span><span class="p">,</span><br/>        <span class="nx">run</span>  <span class="o">:</span> <span class="nx">run</span><br/>    <span class="p">};</span><br/><br/><span class="p">};</span><br/></pre></div>

<p>In the <code>run</code> function's loop, I first check that each property is a function before attemping to run it.  This avoids a <code>called_non_callable TypeError</code>.  I then check that the name of the property begins with the string "test".  When dynamically calling functions, it's usually a good idea to call <code>obj.hasOwnProperty(propname)</code> to ensure the property wasn't inherited from some unknown source (especially if the property you wanted to reference may have been <code>delete</code>d, but a property with the same name is now surfacing from a parent object), but in this case I'd like to leave open the possibility for inherited Test Suites.</p>
<p>The <code>JTests</code> object created in the <code>init</code> function is a separate object that contains a bunch of assertions.  It looks something like this:</p>
<div class="pygments_murphy syntax_highlight"><pre><span class="kd">var</span> <span class="nx">JTests</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span><br/><br/>    <span class="kd">var</span> <span class="nx">version</span> <span class="o">=</span> <span class="mf">0.1</span><span class="p">,</span><br/>        <span class="nx">result_callback</span><span class="p">,</span><br/><br/>    <span class="nx">init</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">callback</span> <span class="p">)</span> <span class="p">{</span><br/>        <span class="nx">result_callback</span> <span class="o">=</span> <span class="nx">callback</span><span class="p">;</span><br/>    <span class="p">}</span><br/><br/>    <span class="nx">assertTrue</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">a</span> <span class="p">)</span> <span class="p">{</span><br/>        <span class="kd">var</span> <span class="nx">result</span> <span class="o">=</span> <span class="nx">a</span> <span class="o">===</span> <span class="kc">false</span><span class="p">;</span><br/>        <span class="nx">result_callback</span><span class="p">(</span> <span class="p">{</span>                                                                                                                                                                         <br/>            <span class="nx">name</span>   <span class="o">:</span> <span class="s2">&quot;assertTrue&quot;</span><span class="p">,</span><br/>            <span class="nx">args</span>   <span class="o">:</span> <span class="p">[</span> <span class="nx">a</span> <span class="p">],</span><br/>            <span class="nx">result</span> <span class="o">:</span> <span class="nx">result</span><br/>        <span class="p">}</span> <span class="p">);</span><br/>    <span class="p">};</span><br/>    <br/>    <span class="k">return</span> <span class="p">{</span><br/>        <span class="nx">assertTrue</span> <span class="o">:</span> <span class="nx">assertTrue</span><br/>    <span class="p">};</span><br/><br/><span class="p">};</span><br/></pre></div>

<p>...except with many more assertions. :)</p>
<p>Both <code>JTestSuite</code> and <code>JTests</code> use the <a href="http://stackoverflow.com/a/5647397/215148">revealing module pattern</a>. </p>
<p>I have a lot of ideas for improvement, like <a href="http://en.wikipedia.org/wiki/Don&apos;t_repeat_yourself">DRY</a>ing up the assertions, so the <code>{name,args,result}</code> object doesn't have to be defined in each assertion.  But <a href="http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#JavaScript">many others</a> have already done a great job, and I don't want to reinvent too many wheels.</p>
<div class="pygments_murphy syntax_highlight"><pre><span class="c1">// Define a custom handler for the result</span><br/><span class="kd">function</span> <span class="nx">test_callback</span><span class="p">(</span> <span class="nx">result</span> <span class="p">)</span> <span class="p">{</span><br/>    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">result</span><span class="p">);</span><br/><span class="p">}</span><br/><br/><span class="c1">// Create a new JTestSuite object and initialize it with the callback</span><br/><span class="kd">var</span> <span class="nx">TestSuite</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">JTestSuite</span><span class="p">();</span><br/><span class="nx">TestSuite</span><span class="p">.</span><span class="nx">init</span><span class="p">(</span> <span class="nx">test_callback</span> <span class="p">);</span><br/><br/><span class="c1">// Create some tests</span><br/><span class="nx">TestSuite</span><span class="p">.</span><span class="nx">testMath</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">jt</span><span class="p">)</span> <span class="p">{</span><br/>    <span class="nx">jt</span><span class="p">.</span><span class="nx">assertEquals</span><span class="p">(</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">4</span> <span class="p">);</span><br/><span class="p">};</span><br/><br/><span class="nx">TestSuite</span><span class="p">.</span><span class="nx">testFalsy</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">jt</span><span class="p">)</span> <span class="p">{</span><br/>    <span class="c1">// [], &quot;&quot;, and &quot;0&quot; all == false in javascript</span><br/>    <span class="nx">jt</span><span class="p">.</span><span class="nx">assertFalsy</span><span class="p">(</span> <span class="p">[]</span> <span class="p">);</span><br/>    <span class="nx">jt</span><span class="p">.</span><span class="nx">assertFalsy</span><span class="p">(</span> <span class="s2">&quot;&quot;</span> <span class="p">);</span><br/>    <span class="nx">jt</span><span class="p">.</span><span class="nx">assertFalsy</span><span class="p">(</span> <span class="s2">&quot;0&quot;</span> <span class="p">);</span><br/><span class="p">};</span><br/><br/><span class="c1">// Run all the tests</span><br/><span class="nx">TestSuite</span><span class="p">.</span><span class="nx">run</span><span class="p">();</span><br/></pre></div>

<p>The callback allows a user to determine how the results from the unit test should be processed or displayed.  Results could be printed to console.log, displayed on a page by adding them to the DOM, sent to a Web service with AJAX, or any arbitrary service with Web Sockets.</p>
<p>After writing this toy framework, and explaining it to a colleague, I was told that the Rails unit tester uses almost the exact same approach.  I'd wager a few bucks that JUnit uses the Reflection API's <code>Class.getMethods()</code> to find and run all the methods added to a TestCase class.  Only a few bucks, though.</p>]]></content:encoded>
    </item>
    <item>
      <title>ColorPal Alpha</title>
      <link>http://mwcz.org/2012/01/16/colorpal-alpha</link>
      <pubDate>Mon, 16 Jan 2012 22:15:50 EST</pubDate>
      <category><![CDATA[color]]></category>
      <category><![CDATA[javascript]]></category>
      <category><![CDATA[html5]]></category>
      <guid>b'yjHyVy_AvOOJ3jMuXx8IXMnMgH4='</guid>
      <description>ColorPal Alpha</description>
      <content:encoded><![CDATA[<p>Introducing ColorPal!</p>
<p><img alt="ColorPal screenshot" src="/img/015/ss.png" /></p>
<p>I've been toying with color palette generation for years.  My photography website has always had some form of dynamic palette, so the theme of each page matches the photo.  You can see the current iteration <a href="http://clayto.com/">here</a>:</p>
<p><img src="/img/015/tarsi.png" alt="clayto.com" title="My photo, 'Tarsi', at clayto.com" /></p>

<p>I owe the idea of photo-matching website palettes to <a href="http://noahgrey.com">Noah Grey</a>.  Each iteration of my photo site has gotten a slightly more advanced palette generation tool.  You can see murmurs of them in the <a href="http://localhost:8080/2011/11/16/html5-canvas-eyedropper/">old</a> <a href="http://localhost:8080/2011/11/16/html5-canvas-area-selection-averaging/">canvas</a> <a href="http://localhost:8080/2011/11/17/html5-tool-for-creating-color-palettes-from-an-image/">demos</a> I've posted.  Sooner or later I'll make a more thorough post about the tools I've created for each iteration of the site.  Anyway, back to ColorPal.</p>
<p>ColorPal is an HTML5 palette generation tool.  Currently it's in a very early stage, and I've only tested it in Chrome.  I have many ideas for improvements, and I'd love to hear anyone's ideas as well.<br />
</p>
<p><img src="/img/015/colorpal_logo.png" alt="ColorPal logo" title="" style="float: right; margin: 8px" /></p>

<p>It's powered by <a href="http://en.wikipedia.org/wiki/Canvas_element">canvas</a>, <a href="https://developer.mozilla.org/en/data_URIs">data URIs</a>, <a href="http://dev.w3.org/2006/webapi/FileAPI/">File API</a>, and my homespun <a href="http://en.wikipedia.org/wiki/Median_cut">median-cut</a> implementation, which I'm calling <em>median-cut.js</em>.</p>
<p>Bear in mind that while I've kept median-cut.js nice and clean, I did hammer out ColorPal as quickly as possible, so it's a mess.  Why?  I expect median-cut.js to be useful to other programmers, but ColorPal is just a tool for designers.  Or it will be, anyway. :)</p>
<p>Here are github repos for <a href="https://github.com/mwcz/ColorPal">ColorPal</a> and <a href="https://github.com/mwcz/median-cut-js">median-cut.js</a>.</p>
<p>And, as a reward for reading to the end, a <a href="/projects/colorpal/">live demo</a>!</p>]]></content:encoded>
    </item>
    <item>
      <title>The N9, QR-Codes, and KeePassX</title>
      <link>http://mwcz.org/2012/01/08/the-n9,-qr-codes,-and-keepassx</link>
      <pubDate>Sun, 08 Jan 2012 22:27:50 EST</pubDate>
      <category><![CDATA[n9]]></category>
      <category><![CDATA[keepass]]></category>
      <category><![CDATA[general]]></category>
      <guid>b'av6euvqkN_75DCjB7lp8jqUPiDw='</guid>
      <description>The N9, QR-Codes, and KeePassX</description>
      <content:encoded><![CDATA[<p><em>Update: I happened across a <a href="http://keepass.info/plugins.html#qrcodegen">KeePass
plugin</a> (not the same as KeePassX)
that displays passwords as QR codes, just like my hack below.</em></p>
<p>Poor, glorious N9.</p>
<p>It's impossible not to love the N9 if you're a geek, especially a programming,
bash-loving, Linux-hankering geek.  It's like a nerd talisman.  This post isn't
about the N9, specifically, so I'll save the love-fest for another time.</p>
<p>This post is about how difficult it can be to get text (especially passwords)
onto a smartphone securely.  I've come to like the approach of using QR codes.
Most (all?) smartphones have a barcode scanner, and QR codes are one of <a href="http://en.wikipedia.org/wiki/Barcode">many
convenient ways</a> to encode text into an
image.  In the case of the N9, it's
<a href="http://store.ovi.com/content/231518">MeeScan</a>.</p>
<p><img alt="meescan" src="/img/011/meescan.png" /></p>
<p><a href="http://fukuchi.org/works/qrencode/index.html.en">qrencode</a> is an extremely
easy to use encoder.  Pass it a string, it produces an image.</p>
<pre><code>qrencode "mypassword" -o mypass.png
</code></pre>
<p><a href="http://keepassx.org">KeePassX</a> is an excellent password manager.
Unfortunately...</p>
<p><img alt="KeePassX not in N9" src="/img/011/keepass_not_in_n9.png" /></p>
<p>Sadly, there is no KeePassX client on the N9.  At least, not yet.  Even if
there were, I'm skeptical that it's a good idea at all to be carrying around a
database of personal passwords on a smartphone.  It's encrypted, sure, but
touchscreen keyboards encourage weak passphrases.  Who wants to enter a 64+
character passphrase on a <abbr title="Virtual keyboard">vkbd</abbr>?</p>
<p>This evening, I spent about an hour hacking QR-code support into KeePassX.
It's a seriously messy hack, using
<a href="http://en.cppreference.com/w/cpp/utility/program/system">system()</a> to call
<code>qrencode</code>, pass in the password, then call
<a href="http://projects.gnome.org/evince/?guid=ON">evince</a> to view it.  Even worse, I
just tacked it onto the "Copy Password" function, instead of figuring out how
to create a new menu item.  It doesn't even delete the generated image after
viewing.  Definitely nothing more than a proof of concept.</p>
<p>Here it is in action.</p>
<p><a href="/img/011/keepassx_demo.png"><img class="grid_7"
src="/img/011/keepassx_demo.png" alt="KeePassX generating a QR code" title=""
/></a></p>

<div class="clear"></div>

<p>I won't be distributing my mod unless someone <em>really</em> wants it, but it would
be cool to see this available as plugin to any password managers that support
plugins.  It could be handy to have clipboard managers generate QR codes too.</p>]]></content:encoded>
    </item>
    <item>
      <title>HTML5 tool for creating color palettes from an image</title>
      <link>http://mwcz.org/2011/11/17/html5-tool-for-creating-color-palettes-from-an-image</link>
      <pubDate>Thu, 17 Nov 2011 22:25:00 EST</pubDate>
      <category><![CDATA[color]]></category>
      <category><![CDATA[colorpal]]></category>
      <category><![CDATA[canvas]]></category>
      <category><![CDATA[html5]]></category>
      <guid>b'eb0msgpTGJeEpYHR5R-z1pMSuUk='</guid>
      <description>HTML5 tool for creating color palettes from an image</description>
      <content:encoded><![CDATA[<p>For an <a href="http://en.wikipedia.org/wiki/Human%E2%80%93computer_interaction">HCI</a> class project in Fall 2009, I pulled together some of my previous demos to make this integrated tool.  It was just a prototype, and I haven't taken the time to get the code set up and working on this blog.  If anyone is interested, I can dig up the code and send it along.</p>
<iframe width="580" height="550" src="http://www.youtube.com/embed/p9QiGPUiXdc" frameborder="0" allowfullscreen></iframe>

<p>In retrospect, it really could have used some narration...</p>]]></content:encoded>
    </item>
    <item>
      <title>Bouncey returns - more canvas physics</title>
      <link>http://mwcz.org/2011/11/17/bouncey-returns---more-canvas-physics</link>
      <pubDate>Thu, 17 Nov 2011 22:25:00 EST</pubDate>
      <category><![CDATA[canvas]]></category>
      <category><![CDATA[javascript]]></category>
      <category><![CDATA[physics]]></category>
      <category><![CDATA[html5]]></category>
      <category><![CDATA[bouncey]]></category>
      <guid>b'2xt0h5v9PdQpOWIQqUDpRdUg6RE='</guid>
      <description>Bouncey returns - more canvas physics</description>
      <content:encoded><![CDATA[<p>This is a slightly upgraded version of the physics demo I showed in my <a href="/2011/11/17/bouncey---canvas-physics/">last post</a>.</p>
<p>It is still...</p>
<p><quote>"a buggy, rudimentary, just-for-fun javascript physics simulator."</quote></p>
<p>This version has:</p>
<ul>
<li>pre-defined initial states</li>
<li>gravity</li>
<li>friction</li>
</ul>
<p>It still has the "clinging" bug.  I know how to fix it, but didn't deem it important enough to spend time on it. :)</p>
<p>The <a href="https://github.com/mwcz/bouncey/blob/master/bounce.html">code</a> is well commented, so feel free to hack on it.</p>
<p>Click on one of the initial states to begin the simulation.</p>
<style type="text/css">
#cnvs {
    margin: 0 auto;
    border: 1px solid black;
    -webkit-box-shadow: 0px 0px 3px rgba( 0, 0, 0, 0.7 );
       -moz-box-shadow: 0px 0px 3px rgba( 0, 0, 0, 0.7 );
         -o-box-shadow: 0px 0px 3px rgba( 0, 0, 0, 0.7 );
            box-shadow: 0px 0px 3px rgba( 0, 0, 0, 0.7 );
}
</style>

<script type="text/javascript" src="/js/008/bouncey.js"></script>

<p>Initial states:
<button onclick="RANDOM();">Random</button>
<button onclick="POOL();">POOL</button>
<button onclick="HEAD_ON_COLLISION();">HEAD_ON_COLLISION</button></p>
<p><canvas id="cnvs" width="500" height="375"> 
    Sorry, your browser does not support HTML5 canvas.  Lame.
</canvas> </p>
<p><button onclick="paused++;paused%=2;">Pause</button> </p>
<pre>
  velocity sum: <span id="txt_velocity_sum">NONE</span>
x velocity sum: <span id="txt_velocity_sum_x">NONE</span>
y velocity sum: <span id="txt_velocity_sum_y">NONE</span>
</pre>

<p>(The POOL initial state reproduces the clinging bug.)</p>]]></content:encoded>
    </item>
  </channel>
</rss>
