From 921c7dbd5fdd69cf22da3cfc74797ec1478a1675 Mon Sep 17 00:00:00 2001 From: kkapsner Date: Mon, 7 Aug 2017 08:49:49 +0200 Subject: [PATCH] Changed rng interface to use colors instead of separate channels. --- lib/modifiedAPI.js | 34 ++++++++++++++++++++++++++++------ lib/randomSupplies.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/lib/modifiedAPI.js b/lib/modifiedAPI.js index ec246c8..494e3e1 100644 --- a/lib/modifiedAPI.js +++ b/lib/modifiedAPI.js @@ -59,10 +59,22 @@ var {imageData, source} = getImageData(window, context); var desc = imageData.data; var l = desc.length; - var rng = randomSupply.getRng(l, window); - for (var i = 0; i < l; i += 1){ - desc[i] = rng(source[i], i); + var ignoredColors = {}; + var rng = randomSupply.getPixelRng(l, window, ignoredColors); + + for (var i = 0; i < l; i += 4){ + var [r, g, b, a] = rng( + source[i + 0], + source[i + 1], + source[i + 2], + source[i + 3], + i / 4 + ); + desc[i + 0] = r; + desc[i + 1] = g; + desc[i + 2] = b; + desc[i + 3] = a; } var canvas = original.cloneNode(true); context = window.HTMLCanvasElement.prototype.getContext.call(canvas, "2d"); @@ -337,10 +349,20 @@ var xPixels = pixels; var ret = original.apply(this, window.Array.from(arguments)); var l = xPixels.length; - var rng = randomSupply.getRng(l, window); + var rng = randomSupply.getPixelRng(l, window, {}); - for (var i = 0; i < l; i += 1){ - xPixels[i] = rng(xPixels[i], i); + for (var i = 0; i < l; i += 4){ + var [r, g, b, a] = rng( + xPixels[i + 0], + xPixels[i + 1], + xPixels[i + 2], + xPixels[i + 3], + i / 4 + ); + xPixels[i + 0] = r; + xPixels[i + 1] = g; + xPixels[i + 2] = b; + xPixels[i + 3] = a; } return ret; diff --git a/lib/randomSupplies.js b/lib/randomSupplies.js index ba27ae5..eabef49 100644 --- a/lib/randomSupplies.js +++ b/lib/randomSupplies.js @@ -68,6 +68,22 @@ // XOR the bit and the value to alter the last bit of it... or not return value ^ bit; }; + }, + getPixelRng: function(length, window, ignoredColors){ + var rng = this.getRng(length, window); + return function(r, g, b, a, i){ + var index = String.fromCharCode(r, g, b, a); + if (ignoredColors[index]){ + return [r, g, b, a]; + } + var baseIndex = i * 4; + return [ + rng(r, baseIndex + 0), + rng(g, baseIndex + 1), + rng(b, baseIndex + 2), + rng(a, baseIndex + 3) + ]; + } } }; @@ -92,6 +108,22 @@ // XOR the last bit to alter it... or not return value ^ (rnd & 0x01); }; + }, + getPixelRng: function(length, window, ignoredColors){ + var rng = this.getRng(length, window); + return function(r, g, b, a, i){ + var index = String.fromCharCode(r, g, b, a); + if (ignoredColors[index]){ + return [r, g, b, a]; + } + var baseIndex = i * 4; + return [ + rng(r, baseIndex + 0), + rng(g, baseIndex + 1), + rng(b, baseIndex + 2), + rng(a, baseIndex + 3) + ]; + } } }; }()); \ No newline at end of file