amos-tool
Version:
amos ui tool
81 lines (78 loc) • 4 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"), _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")), PNGlib = require("./pnglib"), Svg = function(t, i, e) {
this.size = t, this.foreground = this.color.apply(this, i), this.background = this.color.apply(this, e),
this.rectangles = [];
};
function Identicon(t, i) {
if ("string" != typeof t || t.length < 15) throw "A hash of at least 15 characters is required.";
this.defaults = {
background: [ 240, 240, 240, 255 ],
margin: .08,
size: 64,
saturation: .7,
brightness: .5,
format: "png"
}, this.options = "object" === (0, _typeof2.default)(i) ? i : this.defaults, "number" == typeof arguments[1] && (this.options.size = arguments[1]),
arguments[2] && (this.options.margin = arguments[2]), this.hash = t, this.background = this.options.background || this.defaults.background,
this.size = this.options.size || this.defaults.size, this.format = this.options.format || this.defaults.format,
this.margin = void 0 !== this.options.margin ? this.options.margin : this.defaults.margin;
var e = parseInt(this.hash.substr(-7), 16) / 268435455, s = this.options.saturation || this.defaults.saturation, r = this.options.brightness || this.defaults.brightness;
this.foreground = this.options.foreground || this.hsl2rgb(e, s, r);
}
Svg.prototype = {
size: null,
foreground: null,
background: null,
rectangles: null,
color: function(t, i, e, s) {
var r = [ t, i, e ].map(Math.round);
return r.push(s >= 0 && s <= 255 ? s / 255 : 1), "rgba(" + r.join(",") + ")";
},
getDump: function() {
var t, i, e, s = this.foreground, r = this.background, n = .005 * this.size;
for (i = "<svg xmlns='http://www.w3.org/2000/svg' width='" + this.size + "' height='" + this.size + "' style='background-color:" + r + ";'><g style='fill:" + s + "; stroke:" + s + "; stroke-width:" + n + ";'>",
t = 0; t < this.rectangles.length; t++) (e = this.rectangles[t]).color != r && (i += "<rect x='" + e.x + "' y='" + e.y + "' width='" + e.w + "' height='" + e.h + "'/>");
return i += "</g></svg>";
},
getBase64: function() {
if ("function" == typeof btoa) return btoa(this.getDump());
if (Buffer) return new Buffer(this.getDump(), "binary").toString("base64");
throw "Cannot generate base64 output";
}
}, Identicon.prototype = {
background: null,
foreground: null,
hash: null,
margin: null,
size: null,
format: null,
image: function() {
return this.isSvg() ? new Svg(this.size, this.foreground, this.background) : new PNGlib(this.size, this.size, 256);
},
render: function() {
var t, i, e = this.image(), s = this.size, r = Math.floor(s * this.margin), n = Math.floor((s - 2 * r) / 5), o = Math.floor((s - 5 * n) / 2), h = e.color.apply(e, this.background), a = e.color.apply(e, this.foreground);
for (t = 0; t < 15; t++) i = parseInt(this.hash.charAt(t), 16) % 2 ? h : a, t < 5 ? this.rectangle(2 * n + o, t * n + o, n, n, i, e) : t < 10 ? (this.rectangle(1 * n + o, (t - 5) * n + o, n, n, i, e),
this.rectangle(3 * n + o, (t - 5) * n + o, n, n, i, e)) : t < 15 && (this.rectangle(0 * n + o, (t - 10) * n + o, n, n, i, e),
this.rectangle(4 * n + o, (t - 10) * n + o, n, n, i, e));
return e;
},
rectangle: function(t, i, e, s, r, n) {
var o, h;
if (this.isSvg()) n.rectangles.push({
x: t,
y: i,
w: e,
h: s,
color: r
}); else for (o = t; o < t + e; o++) for (h = i; h < i + s; h++) n.buffer[n.index(o, h)] = r;
},
hsl2rgb: function(t, i, e) {
return [ 255 * (i = [ e += i *= e < .5 ? e : 1 - e, e - (t *= 6) % 1 * i * 2, e -= i *= 2, e, e + t % 1 * i, e + i ])[~~t % 6], 255 * i[(16 | t) % 6], 255 * i[(8 | t) % 6] ];
},
toString: function(t) {
return t ? this.render().getDump() : this.render().getBase64();
},
isSvg: function() {
return this.format.match(/svg/i);
}
}, module.exports = Identicon;