@aboveyou00/identicon 中文文档教程
node-identicon-github
https://github.com/sehrgut/node-retricon 的作者已经很长时间没有维护模块了(从 2013 年开始)。 一些依赖已经更新,我们需要使用它。
创建类似于 Github 和 Gravatar(复古) 头像
Example
var identicon = require('identicon-github');
var fmt = require('util').format;
console.log(fmt("<img alt='kibo' src='%s' />", identicon('kibo', { pixelSize: 16 }).toDataURL()));
Output
<img alt='kibo' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAABQCAYAAACOEfKtAAAABmJLR0QA/wD/AP+gvaeTAAAA7UlEQVR4nO3asQ3CQBAAwX9kiVqogP5DV0AtRFDEBCfDTm6/vbrodfv5un/WoPPx3vL89PffJg//BQVEBUQFRAVEBUQFRAVEBUQFRAVEBUQFRAVEBUR7+j7t6ppAVEBUQFRAVEBUQFRAVEBUQFRAVEBUQFRAVEBUQES7eWv5ft70fqCe3wSiAqICogKiAqICogKiAqICogKiAqICogKiAqICouPf9wP1/5tAVEBUQFRAVEBUQFRAVEBUQFRAVEBUQFRAVEBUQHToC66+39d94LACogKiAqICogKiAqICogKiAqICogKiAqICogKiL2SGHPJ+jTmVAAAAAElFTkSuQmCC' />
API
identicon(str, pixelSize, border)
str
: string - username, email, or other string to hashopts
: objectpixelSize
: int (default: 10) - width and height in pixels to render each tilebgColor
: mixed (default: null) - color to fill backgroundnull
for transparent- 0 or 1 for first (brighter) or second (darker) random color
- CSS hex colorspec (e.g. '#F0F0F0')
pixelPadding
: int (default: 0) - background pixels within the border of each rendered tile. Use negative values for overlap, as in Github- style identiconsimagePadding
: int (default: 0) - padding around outside of imagetiles
: int (default: 5) - number of tiles wide and high to renderminFill
: float (default: 0.3) - proportion of tiles which must be filled. Hash chaining is used to satisfy fill criteria.maxFill
: float (default: 0.9) - maximum proportion of tiles which may be filled.pixelColor
: mixed (default: 0) - color to fill foreground tiles. AllbgColor
values are valid forpixelColor
.
返回包含渲染图像的 Canvas
对象。
Styles
所有样式选项都可以更改,但 identicon 带有几个预卷 样式:
default
透明背景下的彩色瓷砖。
mini
带有填充图块的微型 3x3 标识。
gravatar
不是 Gravatar 的“复古”风格的完美复制品,但很近。
mono
灰色背景上的黑色方块。
mosaic
填充图块,用于马赛克效果。
window
彩色背景下的透明填充图块。
github
Github identicon 风格的忠实复制品。
custom example
荒谬的设置,例如: { pixelSize: 8, pixelPadding: -2, tiles: 30, pixelColor: 0, bgColor: 1 }
仍然可以产生非常有用的结果。
Algorithm
输入字符串转换为UTF-的缓冲区8 个字节。 单字节迭代器 附加到此,初始化为零,用于哈希链接。 缓冲区是 通过 SHA-512 传递,并且输出缓冲区被迭代异或以产生一个 最终缓冲区只有所需字节数(目前为 18 个)。 这 前六个字节作为颜色的 RGB 值。 剩余字节 在 127 的阈值处被量化为布尔值。如果最小比例 的布尔值是真的,但不超过最大比例,散列是 公认。 否则,迭代器递增,并重复该过程。
布尔数组被认为是覆盖左半部分的行优先数组 的最终图像。 它反映在中心垂直轴(或列, 对于奇数顺序数组),然后将其呈现为最终的视觉散列。
Known Limitations
- 30x30 tile images are the maximum which can be generated from SHA-512.
node-identicon-github
The author of https://github.com/sehrgut/node-retricon has not maintain module for a long time (since 2013). Some dependencies has been updated and we need to use it.
Create indenticon-like image hashes styled like Github and Gravatar (retro) avatars
Example
var identicon = require('identicon-github');
var fmt = require('util').format;
console.log(fmt("<img alt='kibo' src='%s' />", identicon('kibo', { pixelSize: 16 }).toDataURL()));
Output
<img alt='kibo' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAABQCAYAAACOEfKtAAAABmJLR0QA/wD/AP+gvaeTAAAA7UlEQVR4nO3asQ3CQBAAwX9kiVqogP5DV0AtRFDEBCfDTm6/vbrodfv5un/WoPPx3vL89PffJg//BQVEBUQFRAVEBUQFRAVEBUQFRAVEBUQFRAVEBUR7+j7t6ppAVEBUQFRAVEBUQFRAVEBUQFRAVEBUQFRAVEBUQES7eWv5ft70fqCe3wSiAqICogKiAqICogKiAqICogKiAqICogKiAqICouPf9wP1/5tAVEBUQFRAVEBUQFRAVEBUQFRAVEBUQFRAVEBUQHToC66+39d94LACogKiAqICogKiAqICogKiAqICogKiAqICogKiL2SGHPJ+jTmVAAAAAElFTkSuQmCC' />
API
identicon(str, pixelSize, border)
str
: string - username, email, or other string to hashopts
: objectpixelSize
: int (default: 10) - width and height in pixels to render each tilebgColor
: mixed (default: null) - color to fill backgroundnull
for transparent- 0 or 1 for first (brighter) or second (darker) random color
- CSS hex colorspec (e.g. '#F0F0F0')
pixelPadding
: int (default: 0) - background pixels within the border of each rendered tile. Use negative values for overlap, as in Github- style identiconsimagePadding
: int (default: 0) - padding around outside of imagetiles
: int (default: 5) - number of tiles wide and high to renderminFill
: float (default: 0.3) - proportion of tiles which must be filled. Hash chaining is used to satisfy fill criteria.maxFill
: float (default: 0.9) - maximum proportion of tiles which may be filled.pixelColor
: mixed (default: 0) - color to fill foreground tiles. AllbgColor
values are valid forpixelColor
.
Returns a Canvas
object containing the rendered image.
Styles
All style options can be changed, but identicon comes with several prerolled styles:
default
Coloured tiles against a transparent background.
mini
Tiny 3x3 identicons with padded tiles.
gravatar
Not a perfect replica of Gravatar's "retro" style, but close.
mono
Black tiles on a grey background.
mosaic
Padded tiles, for a mosaic effect.
window
Transparent, padded tiles against coloured background.
github
Faithful replica of Github's identicon style.
custom example
Absurd settings, such as: { pixelSize: 8, pixelPadding: -2, tiles: 30, pixelColor: 0, bgColor: 1 }
can still yield quite serviceable results.
Algorithm
The input string is converted to a buffer of UTF-8 bytes. A one-byte iterator is appended to this, initialized at zero, for hash chaining. The buffer is passed through SHA-512, and the output buffer is iteratively XORed to produce a final buffer of only the number of required bytes (currently eighteen). The first six bytes are taken as RGB values for the colours. The remaining bytes are quantized to boolean values at a threshold of 127. If a minimum proportion of the booleans are true, but not more than the maximum proportion, the hash is accepted. Otherwise, the iterator is incremented, and the process repeated.
The array of booleans is taken to be a row-major array covering the left half of the final image. It is reflected about the central vertical axis (or column, for odd-order arrays) which is then rendered as the final visual hash.
Known Limitations
- 30x30 tile images are the maximum which can be generated from SHA-512.