blockies-npm
Advanced tools
Comparing version
61
index.js
@@ -1,22 +0,33 @@ | ||
(function() { | ||
// The output is pretty uniform, so it's probably fine | ||
var randseed = 0; | ||
function seedrand(s) { | ||
randseed = 0; | ||
for(var i = 0; i < s.length; i += 2) { | ||
var h = (s.charCodeAt(i) << 8) | s.charCodeAt(i + 1); | ||
randseed ^= h; | ||
module.exports = (function() { | ||
// The random number is a js implementation of the Xorshift PRNG | ||
var randseed = new Array(4); // Xorshift: [x, y, z, w] 32 bit values | ||
function seedrand(seed) { | ||
for (var i = 0; i < randseed.length; i++) { | ||
randseed[i] = 0; | ||
} | ||
for (var i = 0; i < seed.length; i++) { | ||
randseed[i%4] = ((randseed[i%4] << 5) - randseed[i%4]) + seed.charCodeAt(i); | ||
} | ||
} | ||
function rand() { | ||
var n = (Math.sin(randseed++) + 1) / 2; // Move range from -1 - 1 to 0 - 1 | ||
var r = n * 10000; // Remove first few digits since they tend to have low entropy | ||
n = r - Math.floor(r); | ||
return n; | ||
// based on Java's String.hashCode(), expanded to 4 32bit values | ||
var t = randseed[0] ^ (randseed[0] << 11); | ||
randseed[0] = randseed[1]; | ||
randseed[1] = randseed[2]; | ||
randseed[2] = randseed[3]; | ||
randseed[3] = (randseed[3] ^ (randseed[3] >> 19) ^ t ^ (t >> 8)); | ||
return (randseed[3]>>>0) / ((1 << 31)>>>0); | ||
} | ||
function createColor() { | ||
//saturation is the whole color spectrum | ||
var h = Math.floor(rand() * 360); | ||
var s = ((rand() * 50) + 50) + '%'; | ||
var l = ((rand() * 60) + 20) + '%'; | ||
//saturation goes from 40 to 100, it avoids greyish colors | ||
var s = ((rand() * 60) + 40) + '%'; | ||
//lightness can be anything from 0 to 100, but probabilities are a bell curve around 50% | ||
var l = ((rand()+rand()+rand()+rand()) * 25) + '%'; | ||
@@ -38,3 +49,5 @@ var color = 'hsl(' + h + ',' + s + ',' + l + ')'; | ||
for(var x = 0; x < dataWidth; x++) { | ||
row[x] = rand() >= 0.5; | ||
// this makes foreground and background color to have a 43% (1/2.3) probability | ||
// spot color has 13% chance | ||
row[x] = Math.floor(rand()*2.3); | ||
} | ||
@@ -53,3 +66,3 @@ var r = row.slice(0, mirrorWidth); | ||
function createCanvas(imageData, color, scale, bgcolor) { | ||
function createCanvas(imageData, color, scale, bgcolor, spotcolor) { | ||
var c = document.createElement('canvas'); | ||
@@ -67,3 +80,6 @@ var width = Math.sqrt(imageData.length); | ||
var col = i % width; | ||
// if data is 2, choose spot color, if 1 choose foreground | ||
cc.fillStyle = (imageData[i] == 1) ? color : spotcolor; | ||
// if data is 0, leave the background | ||
if(imageData[i]) { | ||
@@ -79,6 +95,5 @@ cc.fillRect(col * scale, row * scale, scale, scale); | ||
opts = opts || {}; | ||
var size = opts.size || 10; | ||
var scale = opts.scale || 5; | ||
var seed = opts.seed || Math.random().toString(36).substr(2); | ||
var bgcolor = opts.bgcolor || '#fff'; | ||
var size = opts.size || 8; | ||
var scale = opts.scale || 4; | ||
var seed = opts.seed || Math.floor((Math.random()*Math.pow(10,16))).toString(16); | ||
@@ -88,4 +103,6 @@ seedrand(seed); | ||
var color = opts.color || createColor(); | ||
var bgcolor = opts.bgcolor || createColor(); | ||
var spotcolor = opts.spotcolor || createColor(); | ||
var imageData = createImageData(size); | ||
var canvas = createCanvas(imageData, color, scale, bgcolor); | ||
var canvas = createCanvas(imageData, color, scale, bgcolor, spotcolor); | ||
@@ -96,2 +113,2 @@ return canvas; | ||
return createIcon; | ||
})(); | ||
})(); |
{ | ||
"name": "blockies-npm", | ||
"version": "0.0.0", | ||
"description": "npm package for blockies https://github.com/download13/blockies", | ||
"main": "index.js", | ||
"version": "0.0.1", | ||
"description": "npm package for blockies https://github.com/alexvandesande/blockies", | ||
"main": "build/index.js", | ||
"scripts": { | ||
@@ -17,3 +17,3 @@ "test": "echo \"Error: no test specified\" && exit 1" | ||
], | ||
"author": "goldylucks <adamgoldman7@gmail.com>", | ||
"author": "goldylucks", | ||
"license": "GPL-3.0", | ||
@@ -20,0 +20,0 @@ "bugs": { |
# blockies-npm | ||
npm package for blockies https://github.com/download13/blockies | ||
npm package for blockies https://github.com/alexvandesande/blockies |
40143
5.29%86
16.22%