mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 09:57:02 +01:00
Merge pull request #2615 from jpclark/NewUI-flat
Provide blockie identicon option
This commit is contained in:
commit
2b1f2557c7
@ -2,4 +2,5 @@ app/scripts/lib/extension-instance.js
|
|||||||
test/integration/bundle.js
|
test/integration/bundle.js
|
||||||
test/integration/jquery-3.1.0.min.js
|
test/integration/jquery-3.1.0.min.js
|
||||||
test/integration/helpers.js
|
test/integration/helpers.js
|
||||||
test/integration/lib/first-time.js
|
test/integration/lib/first-time.js
|
||||||
|
ui/lib/blockies.js
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -6,6 +6,8 @@ app/bower_components
|
|||||||
test/bower_components
|
test/bower_components
|
||||||
package
|
package
|
||||||
|
|
||||||
|
.idea
|
||||||
|
|
||||||
temp
|
temp
|
||||||
.tmp
|
.tmp
|
||||||
.sass-cache
|
.sass-cache
|
||||||
|
@ -9,11 +9,20 @@ class PreferencesController {
|
|||||||
frequentRpcList: [],
|
frequentRpcList: [],
|
||||||
currentAccountTab: 'history',
|
currentAccountTab: 'history',
|
||||||
tokens: [],
|
tokens: [],
|
||||||
|
useBlockie: false,
|
||||||
}, opts.initState)
|
}, opts.initState)
|
||||||
this.store = new ObservableStore(initState)
|
this.store = new ObservableStore(initState)
|
||||||
}
|
}
|
||||||
// PUBLIC METHODS
|
// PUBLIC METHODS
|
||||||
|
|
||||||
|
setUseBlockie (val) {
|
||||||
|
this.store.updateState({ useBlockie: val })
|
||||||
|
}
|
||||||
|
|
||||||
|
getUseBlockie () {
|
||||||
|
return this.store.getState().useBlockie
|
||||||
|
}
|
||||||
|
|
||||||
setSelectedAddress (_address) {
|
setSelectedAddress (_address) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const address = normalizeAddress(_address)
|
const address = normalizeAddress(_address)
|
||||||
|
@ -315,6 +315,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
// etc
|
// etc
|
||||||
getState: (cb) => cb(null, this.getState()),
|
getState: (cb) => cb(null, this.getState()),
|
||||||
setCurrentCurrency: this.setCurrentCurrency.bind(this),
|
setCurrentCurrency: this.setCurrentCurrency.bind(this),
|
||||||
|
setUseBlockie: this.setUseBlockie.bind(this),
|
||||||
markAccountsFound: this.markAccountsFound.bind(this),
|
markAccountsFound: this.markAccountsFound.bind(this),
|
||||||
|
|
||||||
// coinbase
|
// coinbase
|
||||||
@ -774,4 +775,13 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
return rpcTarget
|
return rpcTarget
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setUseBlockie (val, cb) {
|
||||||
|
try {
|
||||||
|
this.preferencesController.setUseBlockie(val)
|
||||||
|
cb(null)
|
||||||
|
} catch (err) {
|
||||||
|
cb(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
17
package.json
17
package.json
@ -52,11 +52,11 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"abi-decoder": "^1.0.8",
|
"abi-decoder": "^1.0.9",
|
||||||
"async": "^2.5.0",
|
"async": "^2.5.0",
|
||||||
"await-semaphore": "^0.1.1",
|
"await-semaphore": "^0.1.1",
|
||||||
"babel-runtime": "^6.23.0",
|
"babel-runtime": "^6.23.0",
|
||||||
"bignumber.js": "^4.0.4",
|
"bignumber.js": "^4.1.0",
|
||||||
"bip39": "^2.2.0",
|
"bip39": "^2.2.0",
|
||||||
"bluebird": "^3.5.0",
|
"bluebird": "^3.5.0",
|
||||||
"bn.js": "^4.11.7",
|
"bn.js": "^4.11.7",
|
||||||
@ -77,8 +77,8 @@
|
|||||||
"eslint-plugin-react": "^7.4.0",
|
"eslint-plugin-react": "^7.4.0",
|
||||||
"eth-bin-to-ops": "^1.0.1",
|
"eth-bin-to-ops": "^1.0.1",
|
||||||
"eth-block-tracker": "^2.2.0",
|
"eth-block-tracker": "^2.2.0",
|
||||||
"eth-hd-keyring": "^1.2.1",
|
|
||||||
"eth-contract-metadata": "^1.1.5",
|
"eth-contract-metadata": "^1.1.5",
|
||||||
|
"eth-hd-keyring": "^1.2.1",
|
||||||
"eth-json-rpc-filters": "^1.2.4",
|
"eth-json-rpc-filters": "^1.2.4",
|
||||||
"eth-keyring-controller": "^2.1.2",
|
"eth-keyring-controller": "^2.1.2",
|
||||||
"eth-phishing-detect": "^1.1.4",
|
"eth-phishing-detect": "^1.1.4",
|
||||||
@ -100,9 +100,9 @@
|
|||||||
"extensionizer": "^1.0.0",
|
"extensionizer": "^1.0.0",
|
||||||
"fast-json-patch": "^2.0.4",
|
"fast-json-patch": "^2.0.4",
|
||||||
"fast-levenshtein": "^2.0.6",
|
"fast-levenshtein": "^2.0.6",
|
||||||
"fuse.js": "^3.1.0",
|
"fuse.js": "^3.2.0",
|
||||||
"gulp": "github:gulpjs/gulp#4.0",
|
|
||||||
"gulp-autoprefixer": "^4.0.0",
|
"gulp-autoprefixer": "^4.0.0",
|
||||||
|
"gulp": "github:gulpjs/gulp#4.0",
|
||||||
"gulp-eslint": "^4.0.0",
|
"gulp-eslint": "^4.0.0",
|
||||||
"gulp-sass": "^3.1.0",
|
"gulp-sass": "^3.1.0",
|
||||||
"hat": "0.0.3",
|
"hat": "0.0.3",
|
||||||
@ -144,12 +144,13 @@
|
|||||||
"react-hyperscript": "^3.0.0",
|
"react-hyperscript": "^3.0.0",
|
||||||
"react-markdown": "^2.3.0",
|
"react-markdown": "^2.3.0",
|
||||||
"react-redux": "^5.0.5",
|
"react-redux": "^5.0.5",
|
||||||
"react-select": "^1.0.0-rc.2",
|
"react-select": "^1.0.0",
|
||||||
"react-simple-file-input": "^2.0.0",
|
"react-simple-file-input": "^2.0.0",
|
||||||
|
"react-toggle-button": "^2.2.0",
|
||||||
"react-tooltip-component": "^0.3.0",
|
"react-tooltip-component": "^0.3.0",
|
||||||
"react-transition-group": "^2.2.0",
|
"react-transition-group": "^2.2.1",
|
||||||
"reactify": "^1.1.1",
|
|
||||||
"react-trigger-change": "^1.0.2",
|
"react-trigger-change": "^1.0.2",
|
||||||
|
"reactify": "^1.1.1",
|
||||||
"readable-stream": "^2.3.3",
|
"readable-stream": "^2.3.3",
|
||||||
"recompose": "^0.25.0",
|
"recompose": "^0.25.0",
|
||||||
"redux": "^3.0.5",
|
"redux": "^3.0.5",
|
||||||
|
@ -234,6 +234,9 @@ var actions = {
|
|||||||
toggleAccountMenu,
|
toggleAccountMenu,
|
||||||
|
|
||||||
useEtherscanProvider,
|
useEtherscanProvider,
|
||||||
|
|
||||||
|
SET_USE_BLOCKIE: 'SET_USE_BLOCKIE',
|
||||||
|
setUseBlockie,
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = actions
|
module.exports = actions
|
||||||
@ -1550,3 +1553,20 @@ function toggleAccountMenu () {
|
|||||||
type: actions.TOGGLE_ACCOUNT_MENU,
|
type: actions.TOGGLE_ACCOUNT_MENU,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setUseBlockie (val) {
|
||||||
|
return (dispatch) => {
|
||||||
|
dispatch(actions.showLoadingIndication())
|
||||||
|
log.debug(`background.setUseBlockie`)
|
||||||
|
background.setUseBlockie(val, (err) => {
|
||||||
|
dispatch(actions.hideLoadingIndication())
|
||||||
|
if (err) {
|
||||||
|
return dispatch(actions.displayWarning(err.message))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
dispatch({
|
||||||
|
type: actions.SET_USE_BLOCKIE,
|
||||||
|
value: val,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
const Component = require('react').Component
|
const Component = require('react').Component
|
||||||
const h = require('react-hyperscript')
|
const h = require('react-hyperscript')
|
||||||
const inherits = require('util').inherits
|
const inherits = require('util').inherits
|
||||||
|
const connect = require('react-redux').connect
|
||||||
const isNode = require('detect-node')
|
const isNode = require('detect-node')
|
||||||
const findDOMNode = require('react-dom').findDOMNode
|
const findDOMNode = require('react-dom').findDOMNode
|
||||||
const jazzicon = require('jazzicon')
|
const jazzicon = require('jazzicon')
|
||||||
const iconFactoryGen = require('../../lib/icon-factory')
|
const iconFactoryGen = require('../../lib/icon-factory')
|
||||||
const iconFactory = iconFactoryGen(jazzicon)
|
const iconFactory = iconFactoryGen(jazzicon)
|
||||||
|
const { toDataUrl } = require('../../lib/blockies')
|
||||||
|
|
||||||
module.exports = IdenticonComponent
|
module.exports = connect(mapStateToProps)(IdenticonComponent)
|
||||||
|
|
||||||
inherits(IdenticonComponent, Component)
|
inherits(IdenticonComponent, Component)
|
||||||
function IdenticonComponent () {
|
function IdenticonComponent () {
|
||||||
@ -16,6 +18,12 @@ function IdenticonComponent () {
|
|||||||
this.defaultDiameter = 46
|
this.defaultDiameter = 46
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function mapStateToProps (state) {
|
||||||
|
return {
|
||||||
|
useBlockie: state.metamask.useBlockie,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
IdenticonComponent.prototype.render = function () {
|
IdenticonComponent.prototype.render = function () {
|
||||||
var props = this.props
|
var props = this.props
|
||||||
const { className = '', address } = props
|
const { className = '', address } = props
|
||||||
@ -51,38 +59,59 @@ IdenticonComponent.prototype.render = function () {
|
|||||||
|
|
||||||
IdenticonComponent.prototype.componentDidMount = function () {
|
IdenticonComponent.prototype.componentDidMount = function () {
|
||||||
var props = this.props
|
var props = this.props
|
||||||
const { address } = props
|
const { address, useBlockie } = props
|
||||||
|
|
||||||
if (!address) return
|
if (!address) return
|
||||||
|
|
||||||
// eslint-disable-next-line react/no-find-dom-node
|
|
||||||
var container = findDOMNode(this)
|
|
||||||
|
|
||||||
var diameter = props.diameter || this.defaultDiameter
|
|
||||||
if (!isNode) {
|
if (!isNode) {
|
||||||
var img = iconFactory.iconForAddress(address, diameter)
|
// eslint-disable-next-line react/no-find-dom-node
|
||||||
container.appendChild(img)
|
var container = findDOMNode(this)
|
||||||
|
|
||||||
|
const diameter = props.diameter || this.defaultDiameter
|
||||||
|
|
||||||
|
if (useBlockie) {
|
||||||
|
_generateBlockie(container, address, diameter)
|
||||||
|
} else {
|
||||||
|
_generateJazzicon(container, address, diameter)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
IdenticonComponent.prototype.componentDidUpdate = function () {
|
IdenticonComponent.prototype.componentDidUpdate = function () {
|
||||||
var props = this.props
|
var props = this.props
|
||||||
const { address } = props
|
const { address, useBlockie } = props
|
||||||
|
|
||||||
if (!address) return
|
if (!address) return
|
||||||
|
|
||||||
// eslint-disable-next-line react/no-find-dom-node
|
|
||||||
var container = findDOMNode(this)
|
|
||||||
|
|
||||||
var children = container.children
|
|
||||||
for (var i = 0; i < children.length; i++) {
|
|
||||||
container.removeChild(children[i])
|
|
||||||
}
|
|
||||||
|
|
||||||
var diameter = props.diameter || this.defaultDiameter
|
|
||||||
if (!isNode) {
|
if (!isNode) {
|
||||||
var img = iconFactory.iconForAddress(address, diameter)
|
// eslint-disable-next-line react/no-find-dom-node
|
||||||
container.appendChild(img)
|
var container = findDOMNode(this)
|
||||||
|
|
||||||
|
var children = container.children
|
||||||
|
for (var i = 0; i < children.length; i++) {
|
||||||
|
container.removeChild(children[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
const diameter = props.diameter || this.defaultDiameter
|
||||||
|
|
||||||
|
if (useBlockie) {
|
||||||
|
_generateBlockie(container, address, diameter)
|
||||||
|
} else {
|
||||||
|
_generateJazzicon(container, address, diameter)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function _generateBlockie (container, address, diameter) {
|
||||||
|
const img = new Image()
|
||||||
|
img.src = toDataUrl(address)
|
||||||
|
const dia = !diameter || diameter < 50 ? 50 : diameter
|
||||||
|
img.height = dia * 1.25
|
||||||
|
img.width = dia * 1.25
|
||||||
|
container.appendChild(img)
|
||||||
|
}
|
||||||
|
|
||||||
|
function _generateJazzicon (container, address, diameter) {
|
||||||
|
const img = iconFactory.iconForAddress(address, diameter)
|
||||||
|
container.appendChild(img)
|
||||||
|
}
|
||||||
|
@ -36,6 +36,7 @@ function reduceMetamask (state, action) {
|
|||||||
editingTransactionId: null,
|
editingTransactionId: null,
|
||||||
},
|
},
|
||||||
coinOptions: {},
|
coinOptions: {},
|
||||||
|
useBlockie: false,
|
||||||
}, state.metamask)
|
}, state.metamask)
|
||||||
|
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
@ -314,6 +315,11 @@ function reduceMetamask (state, action) {
|
|||||||
coinOptions,
|
coinOptions,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
case actions.SET_USE_BLOCKIE:
|
||||||
|
return extend(metamaskState, {
|
||||||
|
useBlockie: action.value,
|
||||||
|
})
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return metamaskState
|
return metamaskState
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ const validUrl = require('valid-url')
|
|||||||
const { exportAsFile } = require('./util')
|
const { exportAsFile } = require('./util')
|
||||||
const TabBar = require('./components/tab-bar')
|
const TabBar = require('./components/tab-bar')
|
||||||
const SimpleDropdown = require('./components/dropdowns/simple-dropdown')
|
const SimpleDropdown = require('./components/dropdowns/simple-dropdown')
|
||||||
|
const ToggleButton = require('react-toggle-button')
|
||||||
|
|
||||||
const getInfuraCurrencyOptions = () => {
|
const getInfuraCurrencyOptions = () => {
|
||||||
const sortedCurrencies = infuraCurrencies.objects.sort((a, b) => {
|
const sortedCurrencies = infuraCurrencies.objects.sort((a, b) => {
|
||||||
@ -51,6 +52,26 @@ class Settings extends Component {
|
|||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
renderBlockieOptIn () {
|
||||||
|
const { metamask: { useBlockie }, setUseBlockie } = this.props
|
||||||
|
|
||||||
|
return h('div.settings__content-row', [
|
||||||
|
h('div.settings__content-item', [
|
||||||
|
h('span', 'Use Blockies Identicon'),
|
||||||
|
]),
|
||||||
|
h('div.settings__content-item', [
|
||||||
|
h('div.settings__content-item-col', [
|
||||||
|
h(ToggleButton, {
|
||||||
|
value: useBlockie,
|
||||||
|
onToggle: (value) => setUseBlockie(!value),
|
||||||
|
activeLabel: '',
|
||||||
|
inactiveLabel: '',
|
||||||
|
}),
|
||||||
|
]),
|
||||||
|
]),
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
renderCurrentConversion () {
|
renderCurrentConversion () {
|
||||||
const { metamask: { currentCurrency, conversionDate }, setCurrentCurrency } = this.props
|
const { metamask: { currentCurrency, conversionDate }, setCurrentCurrency } = this.props
|
||||||
|
|
||||||
@ -214,6 +235,7 @@ class Settings extends Component {
|
|||||||
return (
|
return (
|
||||||
h('div.settings__content', [
|
h('div.settings__content', [
|
||||||
warning && h('div.settings__error', warning),
|
warning && h('div.settings__error', warning),
|
||||||
|
this.renderBlockieOptIn(),
|
||||||
this.renderCurrentConversion(),
|
this.renderCurrentConversion(),
|
||||||
// this.renderCurrentProvider(),
|
// this.renderCurrentProvider(),
|
||||||
this.renderNewRpcUrl(),
|
this.renderNewRpcUrl(),
|
||||||
@ -335,6 +357,7 @@ class Settings extends Component {
|
|||||||
Settings.propTypes = {
|
Settings.propTypes = {
|
||||||
tab: PropTypes.string,
|
tab: PropTypes.string,
|
||||||
metamask: PropTypes.object,
|
metamask: PropTypes.object,
|
||||||
|
setUseBlockie: PropTypes.func,
|
||||||
setCurrentCurrency: PropTypes.func,
|
setCurrentCurrency: PropTypes.func,
|
||||||
setRpcTarget: PropTypes.func,
|
setRpcTarget: PropTypes.func,
|
||||||
displayWarning: PropTypes.func,
|
displayWarning: PropTypes.func,
|
||||||
@ -357,6 +380,7 @@ const mapDispatchToProps = dispatch => {
|
|||||||
setRpcTarget: newRpc => dispatch(actions.setRpcTarget(newRpc)),
|
setRpcTarget: newRpc => dispatch(actions.setRpcTarget(newRpc)),
|
||||||
displayWarning: warning => dispatch(actions.displayWarning(warning)),
|
displayWarning: warning => dispatch(actions.displayWarning(warning)),
|
||||||
revealSeedConfirmation: () => dispatch(actions.revealSeedConfirmation()),
|
revealSeedConfirmation: () => dispatch(actions.revealSeedConfirmation()),
|
||||||
|
setUseBlockie: value => dispatch(actions.setUseBlockie(value)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
364
ui/lib/blockies.js
Normal file
364
ui/lib/blockies.js
Normal file
@ -0,0 +1,364 @@
|
|||||||
|
(function (global, factory) {
|
||||||
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
||||||
|
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
||||||
|
(factory((global.blockies = {})));
|
||||||
|
}(this, (function (exports) { 'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A handy class to calculate color values.
|
||||||
|
*
|
||||||
|
* @version 1.0
|
||||||
|
* @author Robert Eisele <robert@xarg.org>
|
||||||
|
* @copyright Copyright (c) 2010, Robert Eisele
|
||||||
|
* @link http://www.xarg.org/2010/03/generate-client-side-png-files-using-javascript/
|
||||||
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
// helper functions for that ctx
|
||||||
|
function write(buffer, offs) {
|
||||||
|
for (var i = 2; i < arguments.length; i++) {
|
||||||
|
for (var j = 0; j < arguments[i].length; j++) {
|
||||||
|
buffer[offs++] = arguments[i].charAt(j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function byte2(w) {
|
||||||
|
return String.fromCharCode((w >> 8) & 255, w & 255);
|
||||||
|
}
|
||||||
|
|
||||||
|
function byte4(w) {
|
||||||
|
return String.fromCharCode((w >> 24) & 255, (w >> 16) & 255, (w >> 8) & 255, w & 255);
|
||||||
|
}
|
||||||
|
|
||||||
|
function byte2lsb(w) {
|
||||||
|
return String.fromCharCode(w & 255, (w >> 8) & 255);
|
||||||
|
}
|
||||||
|
|
||||||
|
var PNG = function(width,height,depth) {
|
||||||
|
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
this.depth = depth;
|
||||||
|
|
||||||
|
// pixel data and row filter identifier size
|
||||||
|
this.pix_size = height * (width + 1);
|
||||||
|
|
||||||
|
// deflate header, pix_size, block headers, adler32 checksum
|
||||||
|
this.data_size = 2 + this.pix_size + 5 * Math.floor((0xfffe + this.pix_size) / 0xffff) + 4;
|
||||||
|
|
||||||
|
// offsets and sizes of Png chunks
|
||||||
|
this.ihdr_offs = 0; // IHDR offset and size
|
||||||
|
this.ihdr_size = 4 + 4 + 13 + 4;
|
||||||
|
this.plte_offs = this.ihdr_offs + this.ihdr_size; // PLTE offset and size
|
||||||
|
this.plte_size = 4 + 4 + 3 * depth + 4;
|
||||||
|
this.trns_offs = this.plte_offs + this.plte_size; // tRNS offset and size
|
||||||
|
this.trns_size = 4 + 4 + depth + 4;
|
||||||
|
this.idat_offs = this.trns_offs + this.trns_size; // IDAT offset and size
|
||||||
|
this.idat_size = 4 + 4 + this.data_size + 4;
|
||||||
|
this.iend_offs = this.idat_offs + this.idat_size; // IEND offset and size
|
||||||
|
this.iend_size = 4 + 4 + 4;
|
||||||
|
this.buffer_size = this.iend_offs + this.iend_size; // total PNG size
|
||||||
|
|
||||||
|
this.buffer = new Array();
|
||||||
|
this.palette = new Object();
|
||||||
|
this.pindex = 0;
|
||||||
|
|
||||||
|
var _crc32 = new Array();
|
||||||
|
|
||||||
|
// initialize buffer with zero bytes
|
||||||
|
for (var i = 0; i < this.buffer_size; i++) {
|
||||||
|
this.buffer[i] = "\x00";
|
||||||
|
}
|
||||||
|
|
||||||
|
// initialize non-zero elements
|
||||||
|
write(this.buffer, this.ihdr_offs, byte4(this.ihdr_size - 12), 'IHDR', byte4(width), byte4(height), "\x08\x03");
|
||||||
|
write(this.buffer, this.plte_offs, byte4(this.plte_size - 12), 'PLTE');
|
||||||
|
write(this.buffer, this.trns_offs, byte4(this.trns_size - 12), 'tRNS');
|
||||||
|
write(this.buffer, this.idat_offs, byte4(this.idat_size - 12), 'IDAT');
|
||||||
|
write(this.buffer, this.iend_offs, byte4(this.iend_size - 12), 'IEND');
|
||||||
|
|
||||||
|
// initialize deflate header
|
||||||
|
var header = ((8 + (7 << 4)) << 8) | (3 << 6);
|
||||||
|
header+= 31 - (header % 31);
|
||||||
|
|
||||||
|
write(this.buffer, this.idat_offs + 8, byte2(header));
|
||||||
|
|
||||||
|
// initialize deflate block headers
|
||||||
|
for (var i = 0; (i << 16) - 1 < this.pix_size; i++) {
|
||||||
|
var size, bits;
|
||||||
|
if (i + 0xffff < this.pix_size) {
|
||||||
|
size = 0xffff;
|
||||||
|
bits = "\x00";
|
||||||
|
} else {
|
||||||
|
size = this.pix_size - (i << 16) - i;
|
||||||
|
bits = "\x01";
|
||||||
|
}
|
||||||
|
write(this.buffer, this.idat_offs + 8 + 2 + (i << 16) + (i << 2), bits, byte2lsb(size), byte2lsb(~size));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Create crc32 lookup table */
|
||||||
|
for (var i = 0; i < 256; i++) {
|
||||||
|
var c = i;
|
||||||
|
for (var j = 0; j < 8; j++) {
|
||||||
|
if (c & 1) {
|
||||||
|
c = -306674912 ^ ((c >> 1) & 0x7fffffff);
|
||||||
|
} else {
|
||||||
|
c = (c >> 1) & 0x7fffffff;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_crc32[i] = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
// compute the index into a png for a given pixel
|
||||||
|
this.index = function(x,y) {
|
||||||
|
var i = y * (this.width + 1) + x + 1;
|
||||||
|
var j = this.idat_offs + 8 + 2 + 5 * Math.floor((i / 0xffff) + 1) + i;
|
||||||
|
return j;
|
||||||
|
};
|
||||||
|
|
||||||
|
// convert a color and build up the palette
|
||||||
|
this.color = function(red, green, blue, alpha) {
|
||||||
|
|
||||||
|
alpha = alpha >= 0 ? alpha : 255;
|
||||||
|
var color = (((((alpha << 8) | red) << 8) | green) << 8) | blue;
|
||||||
|
|
||||||
|
if (typeof this.palette[color] == "undefined") {
|
||||||
|
if (this.pindex == this.depth) return "\x00";
|
||||||
|
|
||||||
|
var ndx = this.plte_offs + 8 + 3 * this.pindex;
|
||||||
|
|
||||||
|
this.buffer[ndx + 0] = String.fromCharCode(red);
|
||||||
|
this.buffer[ndx + 1] = String.fromCharCode(green);
|
||||||
|
this.buffer[ndx + 2] = String.fromCharCode(blue);
|
||||||
|
this.buffer[this.trns_offs+8+this.pindex] = String.fromCharCode(alpha);
|
||||||
|
|
||||||
|
this.palette[color] = String.fromCharCode(this.pindex++);
|
||||||
|
}
|
||||||
|
return this.palette[color];
|
||||||
|
};
|
||||||
|
|
||||||
|
// output a PNG string, Base64 encoded
|
||||||
|
this.getBase64 = function() {
|
||||||
|
|
||||||
|
var s = this.getDump();
|
||||||
|
|
||||||
|
var ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
||||||
|
var c1, c2, c3, e1, e2, e3, e4;
|
||||||
|
var l = s.length;
|
||||||
|
var i = 0;
|
||||||
|
var r = "";
|
||||||
|
|
||||||
|
do {
|
||||||
|
c1 = s.charCodeAt(i);
|
||||||
|
e1 = c1 >> 2;
|
||||||
|
c2 = s.charCodeAt(i+1);
|
||||||
|
e2 = ((c1 & 3) << 4) | (c2 >> 4);
|
||||||
|
c3 = s.charCodeAt(i+2);
|
||||||
|
if (l < i+2) { e3 = 64; } else { e3 = ((c2 & 0xf) << 2) | (c3 >> 6); }
|
||||||
|
if (l < i+3) { e4 = 64; } else { e4 = c3 & 0x3f; }
|
||||||
|
r+= ch.charAt(e1) + ch.charAt(e2) + ch.charAt(e3) + ch.charAt(e4);
|
||||||
|
} while ((i+= 3) < l);
|
||||||
|
return r;
|
||||||
|
};
|
||||||
|
|
||||||
|
// output a PNG string
|
||||||
|
this.getDump = function() {
|
||||||
|
|
||||||
|
// compute adler32 of output pixels + row filter bytes
|
||||||
|
var BASE = 65521; /* largest prime smaller than 65536 */
|
||||||
|
var NMAX = 5552; /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
|
||||||
|
var s1 = 1;
|
||||||
|
var s2 = 0;
|
||||||
|
var n = NMAX;
|
||||||
|
|
||||||
|
for (var y = 0; y < this.height; y++) {
|
||||||
|
for (var x = -1; x < this.width; x++) {
|
||||||
|
s1+= this.buffer[this.index(x, y)].charCodeAt(0);
|
||||||
|
s2+= s1;
|
||||||
|
if ((n-= 1) == 0) {
|
||||||
|
s1%= BASE;
|
||||||
|
s2%= BASE;
|
||||||
|
n = NMAX;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s1%= BASE;
|
||||||
|
s2%= BASE;
|
||||||
|
write(this.buffer, this.idat_offs + this.idat_size - 8, byte4((s2 << 16) | s1));
|
||||||
|
|
||||||
|
// compute crc32 of the PNG chunks
|
||||||
|
function crc32(png, offs, size) {
|
||||||
|
var crc = -1;
|
||||||
|
for (var i = 4; i < size-4; i += 1) {
|
||||||
|
crc = _crc32[(crc ^ png[offs+i].charCodeAt(0)) & 0xff] ^ ((crc >> 8) & 0x00ffffff);
|
||||||
|
}
|
||||||
|
write(png, offs+size-4, byte4(crc ^ -1));
|
||||||
|
}
|
||||||
|
|
||||||
|
crc32(this.buffer, this.ihdr_offs, this.ihdr_size);
|
||||||
|
crc32(this.buffer, this.plte_offs, this.plte_size);
|
||||||
|
crc32(this.buffer, this.trns_offs, this.trns_size);
|
||||||
|
crc32(this.buffer, this.idat_offs, this.idat_size);
|
||||||
|
crc32(this.buffer, this.iend_offs, this.iend_size);
|
||||||
|
|
||||||
|
// convert PNG to string
|
||||||
|
return "\x89PNG\r\n\x1A\n"+this.buffer.join('');
|
||||||
|
};
|
||||||
|
|
||||||
|
this.fillRect = function (x, y, w, h, color) {
|
||||||
|
for(var i = 0; i < w; i++) {
|
||||||
|
for (var j = 0; j < h; j++) {
|
||||||
|
this.buffer[this.index(x+i, y+j)] = color;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
// https://stackoverflow.com/questions/2353211/hsl-to-rgb-color-conversion
|
||||||
|
/**
|
||||||
|
* Converts an HSL color value to RGB. Conversion formula
|
||||||
|
* adapted from http://en.wikipedia.org/wiki/HSL_color_space.
|
||||||
|
* Assumes h, s, and l are contained in the set [0, 1] and
|
||||||
|
* returns r, g, and b in the set [0, 255].
|
||||||
|
*
|
||||||
|
* @param {number} h The hue
|
||||||
|
* @param {number} s The saturation
|
||||||
|
* @param {number} l The lightness
|
||||||
|
* @return {Array} The RGB representation
|
||||||
|
*/
|
||||||
|
|
||||||
|
function hue2rgb(p, q, t) {
|
||||||
|
if(t < 0) t += 1;
|
||||||
|
if(t > 1) t -= 1;
|
||||||
|
if(t < 1/6) return p + (q - p) * 6 * t;
|
||||||
|
if(t < 1/2) return q;
|
||||||
|
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
function hsl2rgb(h, s, l){
|
||||||
|
var r, g, b;
|
||||||
|
|
||||||
|
if(s == 0){
|
||||||
|
r = g = b = l; // achromatic
|
||||||
|
}else{
|
||||||
|
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
||||||
|
var p = 2 * l - q;
|
||||||
|
r = hue2rgb(p, q, h + 1/3);
|
||||||
|
g = hue2rgb(p, q, h);
|
||||||
|
b = hue2rgb(p, q, h - 1/3);
|
||||||
|
}
|
||||||
|
|
||||||
|
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255), 255];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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() {
|
||||||
|
// 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);
|
||||||
|
//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;
|
||||||
|
|
||||||
|
return [h / 360,s / 100,l / 100];
|
||||||
|
}
|
||||||
|
|
||||||
|
function createImageData(size) {
|
||||||
|
var width = size; // Only support square icons for now
|
||||||
|
var height = size;
|
||||||
|
|
||||||
|
var dataWidth = Math.ceil(width / 2);
|
||||||
|
var mirrorWidth = width - dataWidth;
|
||||||
|
|
||||||
|
var data = [];
|
||||||
|
for (var y = 0; y < height; y++) {
|
||||||
|
var row = [];
|
||||||
|
for (var x = 0; x < dataWidth; x++) {
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
var r = row.slice(0, mirrorWidth);
|
||||||
|
r.reverse();
|
||||||
|
row = row.concat(r);
|
||||||
|
|
||||||
|
for (var i = 0; i < row.length; i++) {
|
||||||
|
data.push(row[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildOpts(opts) {
|
||||||
|
if (!opts.seed) {
|
||||||
|
throw 'No seed provided'
|
||||||
|
}
|
||||||
|
|
||||||
|
seedrand(opts.seed);
|
||||||
|
|
||||||
|
return Object.assign({
|
||||||
|
size: 8,
|
||||||
|
scale: 16,
|
||||||
|
color: createColor(),
|
||||||
|
bgcolor: createColor(),
|
||||||
|
spotcolor: createColor(),
|
||||||
|
}, opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
function toDataUrl(address) {
|
||||||
|
const opts = buildOpts({seed: address.toLowerCase()});
|
||||||
|
|
||||||
|
const imageData = createImageData(opts.size);
|
||||||
|
const width = Math.sqrt(imageData.length);
|
||||||
|
|
||||||
|
const p = new PNG(opts.size*opts.scale, opts.size*opts.scale, 3);
|
||||||
|
const bgcolor = p.color(...hsl2rgb(...opts.bgcolor));
|
||||||
|
const color = p.color(...hsl2rgb(...opts.color));
|
||||||
|
const spotcolor = p.color(...hsl2rgb(...opts.spotcolor));
|
||||||
|
|
||||||
|
for (var i = 0; i < imageData.length; i++) {
|
||||||
|
var row = Math.floor(i / width);
|
||||||
|
var col = i % width;
|
||||||
|
// if data is 0, leave the background
|
||||||
|
if (imageData[i]) {
|
||||||
|
// if data is 2, choose spot color, if 1 choose foreground
|
||||||
|
const pngColor = imageData[i] == 1 ? color : spotcolor;
|
||||||
|
p.fillRect(col * opts.scale, row * opts.scale, opts.scale, opts.scale, pngColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return `data:image/png;base64,${p.getBase64()}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.toDataUrl = toDataUrl;
|
||||||
|
|
||||||
|
Object.defineProperty(exports, '__esModule', { value: true });
|
||||||
|
|
||||||
|
})));
|
Loading…
Reference in New Issue
Block a user