From 3953b0496e101e0ecd80672af45abbd63cf3fd1f Mon Sep 17 00:00:00 2001 From: Freeyorp Date: Tue, 25 Jun 2013 16:42:25 +1200 Subject: Add Jaxad's dye channel detection Set up tests --- public/js/mp/dye.js | 30 ++++++++++++++++++++++++++++++ test/load.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ test/mp/dye.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 public/js/mp/dye.js create mode 100644 test/load.js create mode 100644 test/mp/dye.js diff --git a/public/js/mp/dye.js b/public/js/mp/dye.js new file mode 100644 index 0000000..d4c016b --- /dev/null +++ b/public/js/mp/dye.js @@ -0,0 +1,30 @@ +"use strict"; +var mp = function(mp) { + mp.dye = { + getChannel: getChannel + }; + var channel = [null, "R", "G", "Y", "B", "M", "C", "W"]; + function getChannel(color) { + var r = color[0], g = color[1], b = color[2], + max = Math.max(r, g, b); + + if (max == 0) { + // Black + return { channel: null, intensity: 0 }; + } + + var min = Math.min(r, g, b), intensity = r + g + b; + + var idx; + + if (min != max && (min != 0 || (intensity != max && intensity != 2 * max))) { + // Not pure + idx = 0; + } else { + idx = (r != 0) | ((g != 0) << 1) | ((b != 0) << 2); + } + + return { channel: channel[idx], intensity: max }; + } + return mp; +}(mp || {}); diff --git a/test/load.js b/test/load.js new file mode 100644 index 0000000..d04c647 --- /dev/null +++ b/test/load.js @@ -0,0 +1,44 @@ +process.env.TZ = "UTC"; + +var smash = require("smash"), + jsdom = require("jsdom"); + +module.exports = function() { + var files = [].slice.call(arguments).map(function(d) { return "public/js/" + d; }), + expression = "mp", + sandbox = {}; + + function topic() { + smash.load(files, expression, sandbox, this.callback); + } + + topic.expression = function(_) { + expression = _; + return topic; + }; + + topic.document = function(_) { + var document = jsdom.jsdom(""); + + document.createRange = function() { + return { + selectNode: function() {}, + createContextualFragment: jsdom.jsdom + }; + }; + + sandbox = { + console: console, + document: document, + window: document.createWindow(), + }; + + return topic; + }; + + return topic; +}; + +process.on("uncaughtException", function(e) { + console.trace(e.stack); +}); diff --git a/test/mp/dye.js b/test/mp/dye.js new file mode 100644 index 0000000..a4819ae --- /dev/null +++ b/test/mp/dye.js @@ -0,0 +1,52 @@ +var vows = require("vows"), + load = require("../load"), + assert = require("assert"); + +var suite = vows.describe("mp.dye"); + +suite.addBatch({ + "The manaportal dye": { + topic: load("mp/dye").expression("mp.dye"), + "getChannel": { + topic: function(dye) { return dye.getChannel; }, + "returns null given pure black": function(f) { + assert.equal(f([0,0,0]).channel, null); + }, + "returns R given a pure red": function(f) { + assert.equal(f([255,0,0]).channel, "R"); + assert.equal(f([12,0,0]).channel, "R"); + }, + "returns G given a pure green": function(f) { + assert.equal(f([0,255,0]).channel, "G"); + assert.equal(f([0,50,0]).channel, "G"); + }, + "returns B given a pure blue": function (f) { + assert.equal(f([0,0,255]).channel, "B"); + assert.equal(f([0,0,23]).channel, "B"); + }, + "returns C given a pure cyan": function (f) { + assert.equal(f([0,255,255]).channel, "C"); + assert.equal(f([0,90,90]).channel, "C"); + }, + "returns M given a pure magenta": function (f) { + assert.equal(f([255,0,255]).channel, "M"); + assert.equal(f([62,0,62]).channel, "M"); + }, + "returns Y given a pure yellow": function (f) { + assert.equal(f([255,255,0]).channel, "Y"); + assert.equal(f([70,70,0]).channel, "Y"); + }, + "returns W given a pure white": function (f) { + assert.equal(f([255,255,255]).channel, "W"); + assert.equal(f([100,100,100]).channel, "W"); + }, + "returns null given an impure color": function(f) { + assert.equal(f([12,34,56]).channel, null); + assert.equal(f([55,53,55]).channel, null); + assert.equal(f([0,128,254]).channel, null); + } + } + } +}); + +suite.export(module); -- cgit v1.2.3-60-g2f50