Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Identicons #50

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
26b1a66
[imad-console] Updates ui/index.html
Arogyaraj Oct 9, 2016
d4c440a
[imad-console] Updates ui/index.html
Arogyaraj Oct 10, 2016
739d52f
[imad-console] Updates ui/index.html
Arogyaraj Oct 10, 2016
c4deb38
[imad-console] Updates server.js
Arogyaraj Oct 31, 2016
773bea2
Create aro.html
Arogyaraj Oct 31, 2016
48e50fd
[imad-console] Updates ui/aro.html
Arogyaraj Oct 31, 2016
c637e35
[imad-console] Updates ui/style.css
Arogyaraj Oct 31, 2016
627db4a
[imad-console] Updates ui/aro.html
Arogyaraj Oct 31, 2016
08014c4
[imad-console] Updates ui/aro.html
Arogyaraj Oct 31, 2016
6cde456
[imad-console] Updates ui/aro.html
Arogyaraj Oct 31, 2016
e9cdf07
[imad-console] Updates ui/aro.html
Arogyaraj Oct 31, 2016
2a5080a
[imad-console] Updates ui/aro.html
Arogyaraj Oct 31, 2016
297b2cd
[imad-console] Updates ui/aro.html
Arogyaraj Oct 31, 2016
2721884
[imad-console] Updates ui/aro.html
Arogyaraj Oct 31, 2016
e779940
[imad-console] Updates ui/aro.html
Arogyaraj Oct 31, 2016
b4671f2
[imad-console] Updates server.js
Arogyaraj Oct 31, 2016
960a4c8
[imad-console] Updates ui/aro.html
Arogyaraj Oct 31, 2016
bba9753
[imad-console] Updates ui/index.html
Arogyaraj Oct 31, 2016
397b34a
[imad-console] Updates ui/index.html
Arogyaraj Oct 31, 2016
4303ca3
[imad-console] Updates ui/index.html
Arogyaraj Oct 31, 2016
05d4376
[imad-console] Updates ui/index.html
Arogyaraj Oct 31, 2016
39c068c
[imad-console] Updates ui/index.html
Arogyaraj Oct 31, 2016
fbda5fc
[imad-console] Updates ui/index.html
Arogyaraj Oct 31, 2016
f4c4479
[imad-console] Updates ui/style.css
Arogyaraj Oct 31, 2016
a895564
[imad-console] Updates ui/style.css
Arogyaraj Oct 31, 2016
f0d2e36
[imad-console] Updates ui/style.css
Arogyaraj Oct 31, 2016
7f58771
[imad-console] Updates ui/style.css
Arogyaraj Oct 31, 2016
87ba403
[imad-console] Updates ui/style.css
Arogyaraj Oct 31, 2016
2c0cf13
[imad-console] Updates ui/style.css
Arogyaraj Oct 31, 2016
c69fcd5
[imad-console] Updates ui/index.html
Arogyaraj Oct 31, 2016
549580f
[imad-console] Updates ui/aro.html
Arogyaraj Oct 31, 2016
0605b88
[imad-console] Updates ui/aro.html
Arogyaraj Oct 31, 2016
6e9bf83
[imad-console] Updates ui/aro.html
Arogyaraj Oct 31, 2016
7a86008
[imad-console] Updates ui/index.html
Arogyaraj Oct 31, 2016
183d05a
[imad-console] Updates ui/index.html
Arogyaraj Oct 31, 2016
7d3c68e
Add files via upload
Arogyaraj Oct 31, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
.idea/
8 changes: 8 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Copyright (c) 2013, Stewart Lord
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
identicon.js
============

GitHub-style identicons in JS with no server-side processing.

This little library will produce the same shape and (roughly) the same color as GitHub when given the same hash value. Note that GitHub uses an internal database identifier for the hash, so you can't simply md5 the username and get the same result. The hard work is done by [Robert Eisele's](http://twitter.com/roberteisele) [PNGlib](http://www.xarg.org/2010/03/generate-client-side-png-files-using-javascript/). The creative visual design is borrowed from [Jason Long](http://twitter.com/jasonlong) of Git and GitHub fame.

Demo
----
[View Demo](https://rawgithub.com/stewartlord/identicon.js/master/demo.html)

Options
----
* **hash** - [Optional] A unicode string that will be used to generate the image. Defaults to a random hash based on the current time.
* **options** - [Optional] An options object used to customize the generated image.
* **foreground** - The foreground color is automatically derived from the hash value. Use this option to override that behavior and provide a rgba value array instead (e.g. [255,0,0,255] for red).
* **background** - The background color expressed as an rgba value array to use for the image background. For example, use [255,0,0,255] for red. Defaults to an opaque light gray [240,240,240,255].
* **margin** - The decimal fraction of the size to use for margin. For example, use 0.2 for a 20% margin. Defaults to 0.08 for an 8% margin.
* **size** - The size in pixels of the height and width of the generated (square) image. Defaults to 64 pixels.

Usage
-----

##### Simple
Generate the Identicon by supplying a hash string and size.
```js

// create a base64 encoded PNG
var data = new Identicon('hash', 420).toString();

// write to a data URI
document.write('<img width=420 height=420 src="data:image/png;base64,' + data + '">');
```

##### Advanced
To customize additional properties, generate the Identicon by supplying a hash string and an options object.
```js
// set up options
var hash = "myUnicodeUsername!"; // Any unicode string
var options = {
foreground: [0, 0, 0, 255], // rgba black
background: [255, 255, 255, 255], // rgba white
margin: 0.2, // 20% margin
size: 420 // 420px square
};

// create a base64 encoded PNG
var data = new Identicon(hash, options).toString();

// write to a data URI
document.write('<img width=420 height=420 src="data:image/png;base64,' + data + '">');
```


[Requires PNGLib](http://www.xarg.org/download/pnglib.js)

Copyright 2013, [Stewart Lord](http://twitter.com/stewartlord)
Released under the [BSD license](http://www.opensource.org/licenses/bsd-license.php)
20 changes: 20 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "identicon.js",
"main": [
"pnglib.js",
"identicon.js"
],
"version": "1.3.0",
"homepage": "https://github.com/stewartlord/identicon.js",
"authors": [
"stewardlord"
],
"description": "GitHub-style identicons in JS with no server-side processing.",
"moduleType": [
"globals"
],
"keywords": [
"identicon"
],
"license": "BSD"
}
90 changes: 90 additions & 0 deletions demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<html>
<head>
<script src="pnglib.js"></script>
<script src="identicon.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
</head>

<body>
<div class="container-fluid">
<h1>Identicon.js</h1>
<div class="row">
<div class="col-md-12">
<h4>Basic Usage</h4>
<p>Specify just a hash, use default background color, margins, and size.</p>
<pre>
var hash = 'd6fe8c82fb0abac17a702fd2a94eff37';
var data = new Identicon(hash).toString();
document.write('&lt;img src="data:image/png;base64,' + data + '"&gt;');</pre>
<script type="text/javascript">
hashes = [
{id: 'stewartlord', hash: 'd6fe8c82fb0abac17a702fd2a94eff37'},
{id: 'jasonlong', hash: '46384036044a604b6b3316fc167fc15f'},
{id: 'infusion', hash: '373ff2375be91ebc97501e67d24ccd4d'},
{id: 'ppuleo', hash: '001000101000000000000000099fff86'}
];

var i,
data;
for (i = 0; i < hashes.length; i++) {
data = new Identicon(hashes[i].hash).toString();
document.write(
hashes[i].id + '<br><img src="data:image/png;base64,' + data + '"><br><br>'
);
}
</script>
</div>
</div>

<div class="row">
<div class="col-md-12">
<h4>Options Usage</h4>
<p>Specify a hash and an options object to customize background color, margins, and size.</p>
<pre>
var hash = 'd6fe8c82fb0abac17a702fd2a94eff37';
var options = {
foreground: [255, 255, 255, 255],
background: [0, 0, 0, 255],
margin: 0.2,
size: 128
};
var data = new Identicon(hash, options).toString();
document.write('&lt;img src="data:image/png;base64,' + data + '"&gt;');</pre>
<script type="text/javascript">

var data,
hash = 'd6fe8c82fb0abac17a702fd2a94eff37',
options = {
foreground: [255, 255, 255, 255],
background: [0, 0, 0, 255],
margin: 0.2,
size: 128
};

data = new Identicon(hash, options).toString();
document.write(
'<br><img src="data:image/png;base64,' + data + '"><br><br>'
);
</script>
</div>
</div>

<div class="row">
<div class="col-md-12">
<h4>Automatic Usage</h4>
<p>Don't specify a hash to have one automatically generated based on the current time.</p>
<pre>
var data = new Identicon().toString();
document.write('&lt;img src="data:image/png;base64,' + data + '"&gt;');</pre>
<script type="text/javascript">

var data = new Identicon().toString();
document.write(
'<br><img src="data:image/png;base64,' + data + '"><br><br>'
);
</script>
</div>
</div>
</div>
</body>
</html>
46 changes: 46 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//------------ user for publishing
gulp.task('bump-patch-version', function () {
return gulp.src(['./bower.json', './package.json'])
.pipe(bump({type: "patch"}).on('error', gutil.log))
.pipe(gulp.dest('./'));
});

gulp.task('commit-changes', function () {
return gulp.src('.')
.pipe(git.commit('Bumped version number'));
});

gulp.task('push-changes', function (cb) {
git.push('origin', 'master', cb);
});

gulp.task('create-new-tag', function (cb) {
var version = getPackageJsonVersion();
git.tag(version, 'Created Tag for version: ' + version, function (error) {
if (error) {
return cb(error);
}
git.push('origin', 'master', {args: '--tags'}, cb);
});

function getPackageJsonVersion () {
//We parse the json file instead of using require because require caches multiple calls so the version number won't be updated
return JSON.parse(fs.readFileSync('./package.json', 'utf8')).version;
}
});

gulp.task('release', function (callback) {
runSequence(
'bump-patch-version',
'commit-changes',
'push-changes',
'create-new-tag',
function (error) {
if (error) {
console.log(error.message);
} else {
console.log('RELEASE FINISHED SUCCESSFULLY');
}
callback(error);
});
});
144 changes: 144 additions & 0 deletions identicon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/**
* Identicon.js v1.3.0
* http://github.com/stewartlord/identicon.js
*
* Requires PNGLib
* http://www.xarg.org/download/pnglib.js
*
* Copyright 2013, Stewart Lord
* Released under the BSD license
* http://www.opensource.org/licenses/bsd-license.php
*/

(function() {
var PNGlib;
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
PNGlib = require('./pnglib');
} else {
PNGlib = window.PNGlib;
}

var Identicon = function(hash, options){
this.defaults = {
background: [240, 240, 240, 255],
hash: this.createHashFromString((new Date()).toISOString()),
margin: 0.08,
size: 64
};

this.options = typeof(options) === 'object' ? options : this.defaults;

// backward compatibility with old constructor (hash, size, margin)
if (arguments[1] && typeof(arguments[1]) === 'number') { this.options.size = arguments[1]; }
if (arguments[2]) { this.options.margin = arguments[2]; }

this.hash = hash || this.defaults.hash;
this.background = this.options.background || this.defaults.background;
this.foreground = this.options.foreground;
this.margin = this.options.margin || this.defaults.margin;
this.size = this.options.size || this.defaults.size;
};

Identicon.prototype = {
background: null,
foreground: null,
hash: null,
margin: null,
size: null,

render: function(){
var hash = this.hash,
size = this.size,
baseMargin = Math.floor(size * this.margin),
cell = Math.floor((size - (baseMargin * 2)) / 5),
margin = Math.floor((size - cell * 5) / 2),
image = new PNGlib(size, size, 256);

// light-grey background
var bg = image.color(this.background[0], this.background[1], this.background[2], this.background[3]),
fg;

if (this.foreground) {
fg = image.color(this.foreground[0], this.foreground[1], this.foreground[2]);
} else {
// foreground is last 7 chars as hue at 50% saturation, 70% brightness
var rgb = this.hsl2rgb(parseInt(hash.substr(-7), 16) / 0xfffffff, 0.5, 0.7);
fg = image.color(rgb[0] * 255, rgb[1] * 255, rgb[2] * 255);
}

// the first 15 characters of the hash control the pixels (even/odd)
// they are drawn down the middle first, then mirrored outwards
var i, color;
for (i = 0; i < 15; i++) {
color = parseInt(hash.charAt(i), 16) % 2 ? bg : fg;
if (i < 5) {
this.rectangle(2 * cell + margin, i * cell + margin, cell, cell, color, image);
} else if (i < 10) {
this.rectangle(1 * cell + margin, (i - 5) * cell + margin, cell, cell, color, image);
this.rectangle(3 * cell + margin, (i - 5) * cell + margin, cell, cell, color, image);
} else if (i < 15) {
this.rectangle(0 * cell + margin, (i - 10) * cell + margin, cell, cell, color, image);
this.rectangle(4 * cell + margin, (i - 10) * cell + margin, cell, cell, color, image);
}
}

return image;
},

rectangle: function(x, y, w, h, color, image) {
var i, j;
for (i = x; i < x + w; i++) {
for (j = y; j < y + h; j++) {
image.buffer[image.index(i, j)] = color;
}
}
},

// adapted from: https://gist.github.com/aemkei/1325937
hsl2rgb: function(h, s, b){
h *= 6;
s = [
b += s *= b < .5 ? b : 1 - b,
b - h % 1 * s * 2,
b -= s *= 2,
b,
b + h % 1 * s,
b + s
];

return[
s[ ~~h % 6 ], // red
s[ (h|16) % 6 ], // green
s[ (h|8) % 6 ] // blue
];
},

toString: function(){
return this.render().getBase64();
},

// Creates a consistent-length hash from a string
createHashFromString: function(str) {
var hash = '0', salt = 'identicon', i, chr, len;

if (!str) {
return hash;
}

str += salt + str; // Better randomization for short inputs.

for (i = 0, len = str.length; i < len; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash.toString();
}
};

if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = Identicon;
} else {
window.Identicon = Identicon;
}
})();
Loading