Skip to content

Instantly share code, notes, and snippets.

# For a given Lightness, Hue, RGB channel, and limit (1 or 0),
# return Chroma, such that passing this chroma value will cause the
# given channel to pass the given limit.
maxChroma = (L, H) ->
hrad = H / 360 * 2 * Math.PI
sinH = Math.sin hrad
cosH = Math.cos hrad
sub1 = Math.pow(L + 16, 3) / 1560896
sub2 = if sub1 > 0.008856 then sub1 else L / 903.3
(channel) ->
@boronine
boronine / livefilter.js
Created September 25, 2012 22:06
Backbone collection live filtering (untested)
Backbone.Collection.prototype.liveFilter = function(attributes) {
// Make a clone of the collection instantiated with all the filtered items
var _org = this;
var _new = new this.constructor(this.where(attributes));
// What happens when you add to the original collection?
_org.on('add', function(model) {
// Unless one of the attributes doesn't match
for (key in attributes) {
if (model.get(key) !== attributes[key]) return;
}
@boronine
boronine / generating.coffee
Created August 31, 2012 02:37
Password hashing and verifying with Node.js standard library (PBKDF2 + SHA1)
hasher {}, (err, result) ->
# Save as hex strings
user.salt = result.salt.toString 'hex'
user.key = result.key.toString 'hex'
user.save ->
postmark.send
From: "you@example.com"
To: user.email
Subject: "Thank you for signing up with Example.com"
TextBody: "Your temporary password is #{result.plaintext}"
@boronine
boronine / fiddle.coffee
Created August 9, 2012 06:38
Synchronous and asynchronous form field validation using Ember.js bindings
window.App = Ember.Application.create()
App.Focusable = Ember.Mixin.create
# Don't let events spoil your Kool-Aid!
# Let's get these out of the way by turning them
# into a magical property:
focused: false
focusIn: (event) -> @set 'focused', true
focusOut: (event) -> @set 'focused', false
@boronine
boronine / Run all
Created May 11, 2012 14:31
Run a list of commands (as strings) synchronously one after the other. Useful for Cakefiles
# Execute a list of commands one by one
run_all = (commands) ->
return if commands.length is 0
exec commands[0], (err, stdout, stderr) ->
console.log stdout + stderr
throw err if err
run_all commands.splice(1)