snappymail/plugins/avatars/jdenticon.min.js
2022-11-29 15:56:21 +01:00

1 line
3.9 KiB
JavaScript

(()=>{class t{constructor(t,i,s){this.h=t,this.s=i,this.l=s}toString(){const t=this.h,s=this.s,h=this.l,r=t=>(t=i(0,255,255*t).toString(16)).slice(0,2).padStart(2,"0");let n=h,a=h,e=h;if(0!=s){let i=h<.5?h*(s+1):h+s-h*s,r=2*h-i,o=t=>6*(t+=t<0?1:t>1?-1:0)<1?r+(i-r)*t*6:2*t<1?i:3*t<2?r+(i-r)*(2/3-t)*6:r;n=o(t+1/3),a=o(t),e=o(t-1/3)}return"#"+r(n)+r(a)+r(e)}}const i=(t,i,s)=>Math.max(t,Math.min(i,s)),s=(t,i,s)=>parseInt(t.substr(i,s),16),h=(i,s,h)=>{var r=[.55,.5,.5,.46,.6,.55,.55][6*i+.5|0];return new t(i,s,h=h<.5?h*r*2:r+(h-.5)*(1-r)*2).toString()};class r{constructor(t,i){this.x=t,this.y=i}}class n{constructor(t,i,s,h){this.u=t,this.v=i,this.K=s,this.Z=h}L(t,i,s,h){var n=this.u+this.K,a=this.v+this.K,e=this.Z;return 1===e?new r(n-i-(h||0),this.v+t):2===e?new r(n-t-(s||0),a-i-(h||0)):3===e?new r(this.u+i,a-t-(s||0)):new r(this.u+t,this.v+i)}}var a=new n(0,0,0,0);class e{constructor(t){this.M=t,this.A=a}g(t,i){for(var s=i?-2:2,h=[],r=i?t.length-2:0;r<t.length&&r>=0;r+=s)h.push(this.A.L(t[r],t[r+1]));this.M.g(h)}h(t,i,s,h){var r=this.A.L(t,i,s,s);this.M.h(r,s,h)}i(t,i,s,h,r){this.g([t,i,t+s,i,t+s,i+h,t,i+h],r)}j(t,i,s,h,r,n){var a=[t+s,i,t+s,i+h,t,i+h,t,i];a.splice((r||0)%4*2,2),this.g(a,n)}N(t,i,s,h,r){this.g([t+s/2,i,t+s,i+h/2,t+s/2,i+h,t,i+h/2],r)}}function o(t,i,s,h){var r,n,a,e,o,c;(t%=14)?1==t?(a=0|.5*s,e=0|.8*s,i.j(s-a,0,a,e,2)):2==t?(a=0|s/3,i.i(a,a,s-a,s-a)):3==t?(c=s<6?1:s<8?2:0|.25*s,o=(o=.1*s)>1?0|o:o>.5?1:o,i.i(c,c,s-o-c,s-o-c)):4==t?(n=0|.15*s,a=0|.5*s,i.h(s-a-n,s-a-n,a)):5==t?((c=4*(o=.1*s))>3&&(c|=0),i.i(0,0,s,s),i.g([c,c,s-o,c,c+(s-c-o)/2,s-o],!0)):6==t?i.g([0,0,s,0,s,.7*s,.4*s,.4*s,.7*s,s,0,s]):7==t?i.j(s/2,s/2,s/2,s/2,3):8==t?(i.i(0,0,s,s/2),i.i(0,s/2,s/2,s/2),i.j(s/2,s/2,s/2,s/2,1)):9==t?(o=.14*s,c=s<4?1:s<6?2:0|.35*s,o=s<8?o:0|o,i.i(0,0,s,s),i.i(c,c,s-c-o,s-c-o,!0)):10==t?(c=3*(o=.12*s),i.i(0,0,s,s),i.h(c,c,s-o-c,!0)):11==t?i.j(s/2,s/2,s/2,s/2,3):12==t?(n=.25*s,i.i(0,0,s,s),i.N(n,n,s-n,s-n,!0)):!h&&(n=.4*s,a=1.2*s,i.h(n,n,a)):(r=.42*s,i.g([0,0,s,0,s,s-2*r,s-r,s,0,s]))}function c(t,i,s){var h;(t%=4)?1==t?i.j(0,s/2,s,s/2,0):2==t?i.N(0,0,s,s):(h=s/6,i.h(h,h,s-2*h)):i.j(0,0,s,s,0)}function g(t){return(10*t+.5|0)/10}class l{constructor(){this.B=""}g(t){for(var i="",s=0;s<t.length;s++)i+=(s?"L":"M")+g(t[s].x)+" "+g(t[s].y);this.B+=i+"Z"}h(t,i,s){var h=s?0:1,r=g(i/2),n=g(i),a="a"+r+","+r+" 0 1,"+h+" ";this.B+="M"+g(t.x)+" "+g(t.y+i/2)+a+n+",0"+a+-n+",0"}}class u{constructor(t){this.C,this.D={},this.R=t,this.k=t.k}m(t){var i=/^(#......)(..)?/.exec(t),h=i[2]?s(i[2],0)/255:1;this.R.m(i[1],h)}O(t){this.C=this.D[t]||(this.D[t]=new l)}P(){}g(t){this.C.g(t)}h(t,i,s){this.C.h(t,i,s)}finish(){var t=this.D;for(var i in t)t.hasOwnProperty(i)&&this.R.S(i,t[i].B)}}class v{constructor(t){this.k=t,this.F='<svg xmlns="http://www.w3.org/2000/svg" width="'+t+'" height="'+t+'" viewBox="0 0 '+t+" "+t+'">'}m(t,i){i&&(this.F+='<rect width="100%" height="100%" fill="'+t+'" opacity="'+i.toFixed(2)+'"/>')}S(t,i){this.F+='<path fill="'+t+'" d="'+i+'"/>'}toString(){return this.F+"</svg>"}}window.identiconSvg=async t=>{const r=new v(50),a=new u(r),g=/^[0-9a-f]{11,}$/i.test(t)&&t||await async function(t){return Array.from(new Uint8Array(await crypto.subtle.digest("SHA-1",(new TextEncoder).encode(t)))).map((t=>t.toString(16).padStart(2,"0"))).join("")}(null==t?"":""+t),l=.5,w=0,f=t=>i(0,1,.4+.4*t),d=t=>i(0,1,.3+t*(.9-.3));var p,x=a.k,S=.5+.08*x|0;x-=2*S;const m=new e(a),y=0|x/4,j=0|S+x/2-2*y,A=s(g,-7)/268435455,M=[h(A,w,d(0)),h(A,l,f(.5)),h(A,w,d(1)),h(A,l,f(1)),h(A,l,f(0))],B=[],F=t=>{if(t.indexOf(p)>=0)for(var i=0;i<t.length;i++)if(B.indexOf(t[i])>=0)return!0},O=(t,i,h,r,e)=>{var o=s(g,h,1),c=r?s(g,r,1):0;a.O(M[B[t]]);for(var l=0;l<e.length;l++)m.A=new n(j+e[l][0]*y,j+e[l][1]*y,y,c++%4),i(o,m,y,l);a.P()};for(var b=0;b<3;b++)p=s(g,8+b,1)%M.length,(F([0,4])||F([2,3]))&&(p=1),B.push(p);return O(0,c,2,3,[[1,0],[2,0],[2,3],[1,3],[0,1],[3,1],[3,2],[0,2]]),O(1,c,4,5,[[0,0],[3,0],[3,3],[0,3]]),O(2,o,1,null,[[1,1],[2,1],[2,2],[1,2]]),a.finish(),"data:image/svg+xml;base64,"+btoa(r)}})();