From 0cbb580e79ff3c539a45250285358d2e8acfe1e7 Mon Sep 17 00:00:00 2001 From: Laurent Payot Date: Tue, 8 Feb 2022 18:21:09 +0100 Subject: [PATCH] identicon() simplifications --- minidenticons.js | 5 ++--- minidenticons.min.js | 2 +- minidenticons.min.js.map | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/minidenticons.js b/minidenticons.js index 43e4de4..d8858da 100644 --- a/minidenticons.js +++ b/minidenticons.js @@ -18,13 +18,12 @@ function pseudoFNV1a(str) { export function identicon(username, saturation=50, lightness=50) { const hash = pseudoFNV1a(username) // dividing hash by FNV_PRIME to get last XOR result for better color randomness (will be an integer except for empty string hash) - const hue = ((hash / FNV_PRIME) % COLORS_NB) * (360 / COLORS_NB) - const rects = username ? [...Array(25).keys()] + const hue = hash / FNV_PRIME % COLORS_NB * 360 / COLORS_NB + const rects = [...Array(username ? 25 : 0).keys()] // 2 + ((3 * 5 - 1) - modulo) to concentrate squares at the center .map(i => hash % (16 - i % 15) < SQUARE_DENSITY ? `` : '') .join('') - : [] return `${rects}` } diff --git a/minidenticons.min.js b/minidenticons.min.js index 297cccd..c765f00 100644 --- a/minidenticons.min.js +++ b/minidenticons.min.js @@ -1 +1 @@ -export function identicon(t,e=50,i=50){const n=t.split("").reduce(((t,e)=>16777619*((t^e.charCodeAt(0))>>>0)),2166136261);return`${t?[...Array(25).keys()].map((t=>n%(16-t%15)<4?``:"")).join(""):[]}`}export const identiconSvg=globalThis.customElements?.define("identicon-svg",class extends HTMLElement{constructor(){super()}connectedCallback(){this.identiconSvg()}attributeChangedCallback(){this.identiconSvg()}static get observedAttributes(){return["username","saturation","lightness"]}identiconSvg(){this.innerHTML=identicon(this.getAttribute("username"),this.getAttribute("saturation"),this.getAttribute("lightness"))}}); \ No newline at end of file +export function identicon(t,e=50,i=50){const n=t.split("").reduce(((t,e)=>16777619*((t^e.charCodeAt(0))>>>0)),2166136261);return`${[...Array(t?25:0).keys()].map((t=>n%(16-t%15)<4?``:"")).join("")}`}export const identiconSvg=globalThis.customElements?.define("identicon-svg",class extends HTMLElement{constructor(){super()}connectedCallback(){this.identiconSvg()}attributeChangedCallback(){this.identiconSvg()}static get observedAttributes(){return["username","saturation","lightness"]}identiconSvg(){this.innerHTML=identicon(this.getAttribute("username"),this.getAttribute("saturation"),this.getAttribute("lightness"))}}); \ No newline at end of file diff --git a/minidenticons.min.js.map b/minidenticons.min.js.map index 5e1aa04..897267a 100644 --- a/minidenticons.min.js.map +++ b/minidenticons.min.js.map @@ -1 +1 @@ -{"version":3,"sources":["minidenticons.js"],"names":["identicon","username","saturation","lightness","hash","split","reduce","char","charCodeAt","Array","keys","map","i","join","identiconSvg","globalThis","customElements","define","HTMLElement","constructor","super","connectedCallback","this","attributeChangedCallback","observedAttributes","innerHTML","getAttribute"],"mappings":"OAiBO,SAASA,UAAUC,EAAUC,EAAW,GAAIC,EAAU,IACzD,MAAMC,EAAmBH,EANdI,MAAM,IAEZC,QAAO,CAACF,EAAMG,IARL,WAQgBH,EAAOG,EAAKC,WAAW,MAAQ,IAP5C,YAoBjB,MAAO,6EAPOJ,EAdA,SAHA,GAiBiC,MAO4CF,MAAeC,QAN5FF,EAAW,IAAIQ,MAAM,IAAIC,QAElCC,KAAIC,GAAKR,GAAQ,GAAKQ,EAAI,IAtBZ,EAuBX,YAAYA,EAAI,GAAK,KAAOA,EAAE,MAAQA,EAAE,UAAUA,EAAI,4BAA8B,KACvFC,KAAK,IACJ,kBAIH,MAAMC,aACTC,WAAWC,gBAAgBC,OAAO,gBAC9B,cAAcC,YACVC,cAAgBC,QAChBC,oBAAsBC,KAAKR,eAC3BS,2BAA6BD,KAAKR,eACvBU,gCAAuB,MAAO,CAAC,WAAY,aAAc,aACpEV,eACIQ,KAAKG,UAAYzB,UACbsB,KAAKI,aAAa,YAClBJ,KAAKI,aAAa,cAClBJ,KAAKI,aAAa"} \ No newline at end of file +{"version":3,"sources":["minidenticons.js"],"names":["identicon","username","saturation","lightness","hash","split","reduce","char","charCodeAt","Array","keys","map","i","join","identiconSvg","globalThis","customElements","define","HTMLElement","constructor","super","connectedCallback","this","attributeChangedCallback","observedAttributes","innerHTML","getAttribute"],"mappings":"OAiBO,SAASA,UAAUC,EAAUC,EAAW,GAAIC,EAAU,IACzD,MAAMC,EAAmBH,EANdI,MAAM,IAEZC,QAAO,CAACF,EAAMG,IARL,WAQgBH,EAAOG,EAAKC,WAAW,MAAQ,IAP5C,YAmBjB,MAAO,6EANKJ,EAdE,SAHA,GAiB6B,IAjB7B,MAuB6EF,MAAeC,QAL5F,IAAIM,MAAMR,EAAW,GAAK,GAAGS,QAEtCC,KAAIC,GAAKR,GAAQ,GAAKQ,EAAI,IAtBZ,EAuBX,YAAYA,EAAI,GAAK,KAAOA,EAAE,MAAQA,EAAE,UAAUA,EAAI,4BAA8B,KACvFC,KAAK,mBAIP,MAAMC,aACTC,WAAWC,gBAAgBC,OAAO,gBAC9B,cAAcC,YACVC,cAAgBC,QAChBC,oBAAsBC,KAAKR,eAC3BS,2BAA6BD,KAAKR,eACvBU,gCAAuB,MAAO,CAAC,WAAY,aAAc,aACpEV,eACIQ,KAAKG,UAAYzB,UACbsB,KAAKI,aAAa,YAClBJ,KAAKI,aAAa,cAClBJ,KAAKI,aAAa"} \ No newline at end of file