<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://labs.coldacid.net" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>Chris Charabaruk: Code Snippets</title>
 <link>http://labs.coldacid.net/code</link>
 <description>Loose code and work too small to warrant a proper project.</description>
 <language>en</language>
<item>
 <title>HTML5 canvas plasma demo</title>
 <link>http://labs.coldacid.net/code/html5-canvas-plasma-demo</link>
 <description>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Canvas Plasma&lt;/title&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
var ctx;
var tid = null;

function body_Load() {
    var canvas = document.getElementById(&#039;plasma&#039;);
    if (canvas.getContext) {
        ctx = canvas.getContext(&#039;2d&#039;);
        
        traceDiv = document.getElementById(&#039;trace&#039;);
    }
    else {
        alert(&quot;Couldn&#039;t get the canvas, sorry :(&quot;);
    }
}

function startButton_Click() {
    if (tid != null)
        clearInterval(tid);
    
    tid = setInterval(&quot;drawFrame()&quot;, 50);
}

function stepButton_Click() {
    drawFrame();
}

function stopButton_Click() {
    clearInterval(tid);
}

function dist(a, b, c, d) {
    return Math.sqrt((a - c) * (a - c) + (b - d) * (b - d));
}

var frameNumber = 0;
function drawFrame(single) {
    var f = 0;
    
    var r = 0;
    var g = 0;
    var b = 0;
    
    for (var x = 0; x &lt; 320; x++) {
        for (var y = 0; y &lt; 240; y++) {
            f = Math.cos(dist(x + frameNumber, y, 128.0, 128.0) / 8.0)
                + Math.sin(dist(x, y, 64.0, 64.0) / 8.0)
                + Math.sin(dist(x, y + frameNumber / 7, 192.0, 64) / 7.0)
                + Math.cos(dist(x, y, 192.0, 100.0) / 8.0);
            f = Math.round(f * 65536);

            r = (f &amp; 16711680) / 65536;
            g = (f &amp; 65280) / 256;
            b = (f &amp; 255);
            
            ctx.fillStyle = &#039;rgb(&#039; + r + &#039;,&#039; + g + &#039;,&#039; + b + &#039;)&#039;;
            ctx.fillRect(x,y,1,1);
        }
    }
    
    frameNumber++;
}
&lt;/script&gt;

&lt;style type=&quot;text/css&quot;&gt;canvas{border:1px solid black;}&lt;/style&gt;
&lt;/head&gt;

&lt;body onload=&quot;body_Load();&quot;&gt;
&lt;div id=&quot;canvas&quot;&gt;
    &lt;canvas id=&quot;plasma&quot; width=&quot;320&quot; height=&quot;240&quot;&gt;&lt;/canvas&gt;
    &lt;div id=&quot;controls&quot;&gt;
        &lt;input type=&quot;button&quot; name=&quot;start&quot; id=&quot;startButton&quot; value=&quot;Start&quot; onclick=&quot;startButton_Click()&quot;/&gt;
        &lt;input type=&quot;button&quot; name=&quot;step&quot; id=&quot;stepButton&quot; value=&quot;Step&quot; onclick=&quot;stepButton_Click()&quot;/&gt;
        &lt;input type=&quot;button&quot; name=&quot;stop&quot; id=&quot;stopButton&quot; value=&quot;Stop&quot; onclick=&quot;stopButton_Click()&quot;/&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</description>
 <category domain="http://labs.coldacid.net/code/tags/canvas">canvas</category>
 <category domain="http://labs.coldacid.net/code/tags/html5">HTML5</category>
 <category domain="http://labs.coldacid.net/code/tags/javascript">JavaScript</category>
 <enclosure url="http://labs.coldacid.net/system/files/plasma.canvas.html" length="2104" type="text/html" />
 <pubDate>Sun, 12 Sep 2010 21:07:19 -0700</pubDate>
 <dc:creator>Chris Charabaruk</dc:creator>
 <guid isPermaLink="false">30 at http://labs.coldacid.net</guid>
</item>
<item>
 <title>Simple scene library for Ren&#039;Py</title>
 <link>http://labs.coldacid.net/code/simple-scene-library-renpy</link>
 <description># Copyright (c) 2010 Karl Knechtel, Chris Charabaruk
# Licensed under the zlib/libpng license
# see http://www.opensource.org/licenses/zlib-license.php

# Documentation is in-line with the source, or visit
# http://labs.coldacid.net/code/simple-scene-library-renpy
# for further details.

init python:
    
    # show_scene_ actually plays the scene, show_scene lets us use it
    # from a button.
    def show_scene_(scene_id):
        
        # Put a halt to music, in case the scene starts without any
        renpy.music.stop(fadeout=0.0)
        
        # Now we actually start running the scene!
        renpy.call_in_new_context(scene_id)
        
        # Restore the main menu music; if it&#039;s the same as the scene&#039;s
        # music, restart it from the beginning.
        renpy.music.play(config.main_menu_music, if_changed=False)
        
        # This is what ui.interact() will return below.
        return True
    
    show_scene = renpy.curry(show_scene_)
    
    # Call this with a button name and a scene label to define a scene button.
    def scene_button(name, scene_id):
        
        # This lets us lock out scenes that the player hasn&#039;t read yet.
        # If we&#039;re in developer mode, let us play scenes anyway.
        if not (renpy.seen_label(scene_id) or config.developer):
            name = &#039;[Locked]&#039;
            clicked = None
        else:
            clicked = show_scene(scene_id)
        
        # Now make the actual button.
        ui.textbutton(
            name,
            clicked=clicked,
            xalign = 0.5,
            xfill=True,
            size_group=&#039;library&#039;)
    
    # Add the scene library to the main menu.
    config.main_menu.insert(2, (&#039;Scenes&#039;, ui.jumps(&#039;scene_library&#039;, &quot;main_game_transition&quot;), &quot;True&quot;))

# scene_library is the entry point to our scene library, and does any needed
# setup before our UI loop.
label scene_library:
    python:
        _game_menu_screen = None
        
label scene_library_loop:
    python:
        # music.play and the first two ui calls let us continue the main
        # menu feel.
        renpy.music.play(config.main_menu_music, if_changed=True)
        ui.window(style=&#039;mm_root&#039;)
        ui.null()
        
        # This builds our dialog containing the scene list.
        ui.frame(xalign=0.5, yalign=0.15, xanchor=&#039;center&#039;, yanchor=&#039;top&#039;)
        ui.side([&#039;c&#039;, &#039;r&#039;], spacing=5)
        vp = ui.viewport(draggable=True, clipping=True, mousewheel=True, xmaximum=260, ymaximum=390)
        ui.vbox()
        
        # Now we can actually list out all the playable scenes we want to
        # make available to the player.  Each scene is added to the list with
        # a call to the scene_button function defined earlier.
        
        # To add a scene, you call scene_button with the readable name
        # first, and then the name on the label statement which is at
        # the head of the scene.  For example, a scene titled &quot;Meet Lucy&quot;,
        # which starts at label meet_lucy, would be added to the list
        # like so:
        #   scene_button(&#039;Meet Lucy&#039;, &#039;meet_lucy&#039;)
        
        # If you need to do any setup before a scene, it is best to set up
        # an alternate label that includes the Ren&#039;Py code needed to
        # prepare the scene for play, then jumps to the proper label for
        # the scene.  For example, if &quot;Meet Lucy&quot; won&#039;t work right unless
        # variable x is set to 1, you could do something like this:
        #   label meet_lucy_init:
        #       $ x = 1
        #       jump meet_lucy
        #
        # You would then use meet_lucy_init in the scene list below.
        
        ## SCENE LIST STARTS HERE
        
        #scene_button(&#039;Scene Name&#039;, &#039;label_name&#039;)
        
        ## SCENE LIST ENDS HERE
        
        # This wraps up our dialog.
        ui.close()
        ui.bar(adjustment=vp.yadjustment, style=&#039;vscrollbar&#039;, yalign=0.9)
        ui.close()
        
        # And now, a button to bring us back to the main menu.
        ui.textbutton(&#039;Return&#039;, xalign = 0.5, ypos = 450, clicked = ui.returns(False), size_group = &#039;scene&#039;)
    
    # This pretty much says to take us back to the main menu if &#039;Return&#039; is
    # clicked, or rebuild the dialog if we&#039;ve just come back from a scene.
    if ui.interact():
        jump scene_library_loop
    else:
        $ renpy.full_restart(transition=None)
</description>
 <category domain="http://labs.coldacid.net/code/tags/hack">hack</category>
 <category domain="http://labs.coldacid.net/code/tags/renpy">Ren&amp;#039;Py</category>
 <pubDate>Thu, 18 Mar 2010 23:32:11 -0700</pubDate>
 <dc:creator>Chris Charabaruk</dc:creator>
 <guid isPermaLink="false">22 at http://labs.coldacid.net</guid>
</item>
<item>
 <title>Automatic directories for audio in Ren&#039;Py</title>
 <link>http://labs.coldacid.net/code/automatic-directories-audio-renpy</link>
 <description># No code yet, may add later</description>
 <category domain="http://labs.coldacid.net/code/tags/audio">audio</category>
 <category domain="http://labs.coldacid.net/code/tags/hack">hack</category>
 <category domain="http://labs.coldacid.net/code/tags/renpy">Ren&amp;#039;Py</category>
 <pubDate>Thu, 18 Mar 2010 21:04:59 -0700</pubDate>
 <dc:creator>Chris Charabaruk</dc:creator>
 <guid isPermaLink="false">21 at http://labs.coldacid.net</guid>
</item>
<item>
 <title>Ren&#039;Py settings for SciTE 2.0.3+</title>
 <link>http://labs.coldacid.net/code/renpy-settings-scite-203</link>
 <description># Define SciTE settings for Ren&#039;Py files.

file.patterns.rpy=*.rpy;*.rpym

shbang.renpy=rpy

filter.renpy=Ren&#039;Py (rpy rpym)|$(file.patterns.rpy)|

lexer.$(file.patterns.rpy)=python
lexer.renpy.strings.over.newline=1

keywordclass.renpy=at call expression hide image init jump label menu \
onlayer python scene set show with \
and assert break class continue def del elif \
else except exec finally for from global if import in is lambda None \
not or pass print raise return try while yield $ \
play queue stop sound music fadeout fadein channel \
voice sustain nvl clear window

keywords.$(file.patterns.rpy)=$(keywordclass.renpy)

statement.indent.$(file.patterns.rpy)=10 :
statement.end.$(file.patterns.rpy)=

statement.lookback.$(file.patterns.rpy)=0
block.start.$(file.patterns.rpy)=
block.end.$(file.patterns.rpy)=

view.indentation.examine.*.rpy=2

tab.timmy.whinge.level=1

comment.block.renpy=## 

# Ren&#039;Py styles
# White space
style.renpy.0=fore:#808080
# Comment
style.renpy.1=$(colour.comment),$(font.comment)
# Number
style.renpy.2=
# String
style.renpy.3=$(colour.string)
# Single quoted string
style.renpy.4=$(colour.string)
# Keyword
style.renpy.5=$(colour.keyword)
# Triple quotes
style.renpy.6=$(colour.string)
# Triple double quotes
style.renpy.7=$(colour.string)
# Class name definition
style.renpy.8=fore:#0000FF,bold
# Function or method name definition
style.renpy.9=fore:#007F7F,bold
# Operators
style.renpy.10=bold
# Identifiers
style.renpy.11=fore:#007F7F,bold
# Comment-blocks
style.renpy.12=$(colour.comment)
# End of line where string is not closed
style.renpy.13=fore:#000000,$(font.monospace),back:#E0C0E0,eolfilled
# Highlighted identifiers
style.renpy.14=fore:#407090
# Decorators
style.renpy.15=
# Matched Operators
style.renpy.34=fore:#0000FF,bold
style.renpy.35=fore:#FF0000,bold
# Braces are only matched in operator style
braces.renpy.style=10

command.name.0.*.rpy=Start Ren&#039;Py
if PLAT_WIN
    command.0.*.rpy=&quot;$(RENPY_BASE)/renpy.exe&quot; &quot;$(FileDir)/..&quot;
if PLAT_GTK
    command.0.*.rpy=&quot;$(RENPY_BASE)/renpy.sh&quot; &quot;$(FileDir)/..&quot;
command.subsystem.0.*.rpy=1
command.is.filter.0.*.rpy=1

#extension.$(file.patterns.rpy)=renpy.lua

</description>
 <category domain="http://labs.coldacid.net/code/tags/configuration">configuration</category>
 <category domain="http://labs.coldacid.net/code/tags/renpy">Ren&amp;#039;Py</category>
 <category domain="http://labs.coldacid.net/code/tags/scite">SciTE</category>
 <pubDate>Wed, 17 Mar 2010 10:03:41 -0700</pubDate>
 <dc:creator>Chris Charabaruk</dc:creator>
 <guid isPermaLink="false">20 at http://labs.coldacid.net</guid>
</item>
<item>
 <title>Number rounding extension method</title>
 <link>http://labs.coldacid.net/code/number-rounding-extension-method</link>
 <description>using System;

namespace TestApp
{
    public static class ExtensionMethods
    {
        public static int RoundOff (this int i)
        {
            return ((int)Math.Round(i / 10.0)) * 10;
        }
    }

    public class TestApp
    {
        public static void Main()
        {
            int roundedNumber = 236.RoundOff(); // returns 240
            int roundedNumber2 = 11.RoundOff(); // returns 10
            Console.WriteLine(roundedNumber);
            Console.WriteLine(roundedNumber2);
        }
    }
}
</description>
 <category domain="http://labs.coldacid.net/code/tags/net-35">.NET 3.5</category>
 <category domain="http://labs.coldacid.net/code/tags/extension-method">extension method</category>
 <category domain="http://labs.coldacid.net/code/tags/sample">sample</category>
 <category domain="http://labs.coldacid.net/code/tags/stack-overflow">Stack Overflow</category>
 <enclosure url="http://labs.coldacid.net/system/files/roundoff.exe" length="4096" type="application/x-msdos-program" />
 <pubDate>Tue, 03 Mar 2009 07:32:24 -0800</pubDate>
 <dc:creator>Chris Charabaruk</dc:creator>
 <guid isPermaLink="false">2 at http://labs.coldacid.net</guid>
</item>
<item>
 <title>Anti-Telus filter</title>
 <link>http://labs.coldacid.net/code/anti-telus-filter</link>
 <description>#!/usr/bin/perl

$user = &#039;REDIR@EXAMPLE.ADDRESS&#039;;
$pass = &#039;PASSWORD&#039;;
$host = &#039;POP3.SERVER.ADDRESS&#039;;

%redir = (
    &#039;sender@example.address&#039; =&gt; &#039;reciever@example.address&#039;
);

$sendmail = &#039;/usr/sbin/sendmail -t&#039;;
#DEBUG $sendmail = &#039;cat &gt;&gt;test2.txt&#039;;

use Mail::POP3Client;

# connection
$pop = new Mail::POP3Client(HOST =&gt; $host);
$pop-&gt;User($user);
$pop-&gt;Pass($pass);

$pop-&gt;Connect() &gt;= 0 || die $pop-&gt;Message();

# loop through messages
for ($i = 1; $i &lt;= $pop-&gt;Count(); $i++) {
    %head = ();

    foreach ($pop-&gt;Head($i)) {
        /^(From|Subject|Content-type):\s+(.+)/i;
        $head{lc($1)} = $2;
    }
#    print &quot;From: $head{&#039;from&#039;}\n&quot;;
#    print &quot;Subject: $head{&#039;subject&#039;}\n&quot;;
#    print &quot;Content-type: $head{&#039;content-type&#039;}\n&quot;;

    $head{&#039;from&#039;} =~ /&lt;(.+)&gt;$/;
    $email = $1;

    $body = $pop-&gt;Body($i);
    $body =~ s/Email on the go, sent by TELUS//mi;

    open(SENDMAIL, &quot;|$sendmail&quot;) or die &quot;Cannot open $sendmail: $!\n&quot;;
    print SENDMAIL &quot;From: $head{&#039;from&#039;}\nReply-to: $head{&#039;from&#039;}\n&quot;;
    print SENDMAIL &quot;Subject: $head{&#039;subject&#039;}\n&quot;;
    print SENDMAIL &quot;To: $redir{$email}\n&quot;;
    print SENDMAIL &quot;Content-type: $head{&#039;content-type&#039;}\n\n&quot;;
    print SENDMAIL $body;
    close(SENDMAIL);

#    print $body
#    print &quot;\n\n=====\n\n&quot;;

    $pop-&gt;Delete($i);
}

$pop-&gt;Close;
</description>
 <category domain="http://labs.coldacid.net/code/tags/email">email</category>
 <category domain="http://labs.coldacid.net/code/tags/filter">filter</category>
 <category domain="http://labs.coldacid.net/code/tags/hack">hack</category>
 <category domain="http://labs.coldacid.net/code/tags/mobile-email">mobile email</category>
 <category domain="http://labs.coldacid.net/code/tags/telus">Telus</category>
 <pubDate>Fri, 13 Feb 2009 12:37:47 -0800</pubDate>
 <dc:creator>Chris Charabaruk</dc:creator>
 <guid isPermaLink="false">1 at http://labs.coldacid.net</guid>
</item>
</channel>
</rss>

