UNPKG

vue-jdenticon

Version:

A simple wrapper around jdenticon library for Vue3

2 lines (1 loc) 8.28 kB
(function(_,m){typeof exports=="object"&&typeof module<"u"?m(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],m):(_=typeof globalThis<"u"?globalThis:_||self,m(_["vue-jdenticon"]={},_.Vue))})(this,function(_,m){"use strict";function b(o,n,t){return parseInt(o.substr(n,t),16)}function y(o){return o|=0,o<0?"00":o<16?"0"+o.toString(16):o<256?o.toString(16):"ff"}function j(o,n,t){return t=t<0?t+6:t>6?t-6:t,y(255*(t<1?o+(n-o)*t:t<3?n:t<4?o+(n-o)*(4-t):o))}function V(o){if(/^#[0-9a-f]{3,8}$/i.test(o)){let n;const t=o.length;if(t<6){const i=o[1],e=o[2],s=o[3],r=o[4]||"";n="#"+i+i+e+e+s+s+r+r}return(t==7||t>8)&&(n=o),n}}function M(o){const n=b(o,7,2);let t;if(isNaN(n))t=o;else{const i=b(o,1,2),e=b(o,3,2),s=b(o,5,2);t="rgba("+i+","+e+","+s+","+(n/255).toFixed(2)+")"}return t}function W(o,n,t){let i;if(n==0){const e=y(t*255);i=e+e+e}else{const e=t<=.5?t*(n+1):t+n-t*n,s=t*2-e;i=j(s,e,o*6+2)+j(s,e,o*6)+j(s,e,o*6-2)}return"#"+i}function T(o,n,t){const i=[.55,.5,.5,.46,.6,.55,.55],e=i[o*6+.5|0];return t=t<.5?t*e*2:e+(t-.5)*(1-e)*2,W(o,n,t)}const $=typeof window<"u"?window:typeof self<"u"?self:typeof global<"u"?global:{},H={V:"jdenticon_config",n:"config"};var U={};function z(o,n){const t=U[H.n]||$[H.V]||{},i=t.lightness||{},e=t.saturation||{},s="color"in e?e.color:e,r=e.grayscale,f=t.backColor,u=t.padding;function a(p,c){let d=i[p];return d&&d.length>1||(d=c),function(h){return h=d[0]+h*(d[1]-d[0]),h<0?0:h>1?1:h}}function g(p){const c=t.hues;let d;return c&&c.length>0&&(d=c[0|.999*p*c.length]),typeof d=="number"?(d/360%1+1)%1:p}return{W:g,o:typeof s=="number"?s:.5,D:typeof r=="number"?r:0,p:a("color",[.4,.8]),F:a("grayscale",[.3,.9]),G:V(f),X:typeof u=="number"?u:n}}class O{constructor(n,t){this.x=n,this.y=t}}class P{constructor(n,t,i,e){this.q=n,this.t=t,this.H=i,this.Y=e}I(n,t,i,e){const s=this.q+this.H,r=this.t+this.H,f=this.Y;return f===1?new O(s-t-(e||0),this.t+n):f===2?new O(s-n-(i||0),r-t-(e||0)):f===3?new O(this.q+t,r-n-(i||0)):new O(this.q+n,this.t+t)}}const X=new P(0,0,0,0);class Q{constructor(n){this.J=n,this.u=X}g(n,t){const i=t?-2:2,e=[];for(let s=t?n.length-2:0;s<n.length&&s>=0;s+=i)e.push(this.u.I(n[s],n[s+1]));this.J.g(e)}h(n,t,i,e){const s=this.u.I(n,t,i,i);this.J.h(s,i,e)}i(n,t,i,e,s){this.g([n,t,n+i,t,n+i,t+e,n,t+e],s)}j(n,t,i,e,s,r){const f=[n+i,t,n+i,t+e,n,t+e,n,t];f.splice((s||0)%4*2,2),this.g(f,r)}K(n,t,i,e,s){this.g([n+i/2,t,n+i,t+e/2,n+i/2,t+e,n,t+e/2],s)}}function x(o,n,t,i){o=o%14;let e,s,r,f,u,a;o?o==1?(r=0|t*.5,f=0|t*.8,n.j(t-r,0,r,f,2)):o==2?(r=0|t/3,n.i(r,r,t-r,t-r)):o==3?(u=t*.1,a=t<6?1:t<8?2:0|t*.25,u=u>1?0|u:u>.5?1:u,n.i(a,a,t-u-a,t-u-a)):o==4?(s=0|t*.15,r=0|t*.5,n.h(t-r-s,t-r-s,r)):o==5?(u=t*.1,a=u*4,a>3&&(a=0|a),n.i(0,0,t,t),n.g([a,a,t-u,a,a+(t-a-u)/2,t-u],!0)):o==6?n.g([0,0,t,0,t,t*.7,t*.4,t*.4,t*.7,t,0,t]):o==7?n.j(t/2,t/2,t/2,t/2,3):o==8?(n.i(0,0,t,t/2),n.i(0,t/2,t/2,t/2),n.j(t/2,t/2,t/2,t/2,1)):o==9?(u=t*.14,a=t<4?1:t<6?2:0|t*.35,u=t<8?u:0|u,n.i(0,0,t,t),n.i(a,a,t-a-u,t-a-u,!0)):o==10?(u=t*.12,a=u*3,n.i(0,0,t,t),n.h(a,a,t-u-a,!0)):o==11?n.j(t/2,t/2,t/2,t/2,3):o==12?(s=t*.25,n.i(0,0,t,t),n.K(s,s,t-s,t-s,!0)):!i&&(s=t*.4,r=t*1.2,n.h(s,s,r)):(e=t*.42,n.g([0,0,t,0,t,t-e*2,t-e,t,0,t]))}function k(o,n,t){o=o%4;let i;o?o==1?n.j(0,t/2,t,t/2,0):o==2?n.K(0,0,t,t):(i=t/6,n.h(i,i,t-2*i)):n.j(0,0,t,t,0)}function l(o,n){return o=n.W(o),[T(o,n.D,n.F(0)),T(o,n.o,n.p(.5)),T(o,n.D,n.F(1)),T(o,n.o,n.p(1)),T(o,n.o,n.p(0))]}function tt(o,n,t){const i=z(t,.08);i.G&&o.m(i.G);let e=o.k;const s=.5+e*i.X|0;e-=s*2;const r=new Q(o),f=0|e/4,u=0|s+e/2-f*2,a=0|s+e/2-f*2;function g(S,w,I,v,N){const dt=b(n,I,1);let gt=v?b(n,v,1):0;o.L(c[d[S]]);for(let E=0;E<N.length;E++)r.u=new P(u+N[E][0]*f,a+N[E][1]*f,f,gt++%4),w(dt,r,f,E);o.M()}const p=b(n,-7)/268435455,c=l(p,i),d=[];let h;function A(S){if(S.indexOf(h)>=0){for(let w=0;w<S.length;w++)if(d.indexOf(S[w])>=0)return!0}}for(let S=0;S<3;S++)h=b(n,8+S,1)%c.length,(A([0,4])||A([2,3]))&&(h=1),d.push(h);g(0,k,2,3,[[1,0],[2,0],[2,3],[1,3],[0,1],[3,1],[3,2],[0,2]]),g(1,k,4,5,[[0,0],[3,0],[3,3],[0,3]]),g(2,x,1,null,[[1,1],[2,1],[2,2],[1,2]]),o.finish()}function nt(o){var i=0,e=0,s=encodeURI(o)+"%80",r=[],f,u=[],a=1732584193,g=4023233417,p=~a,c=~g,d=3285377520,h=[a,g,p,c,d],A=0,S="";function w(I,v){return I<<v|I>>>32-v}for(;i<s.length;e++)r[e>>2]=r[e>>2]|(s[i]=="%"?parseInt(s.substring(i+1,i+=3),16):s.charCodeAt(i++))<<(3-(e&3))*8;for(f=((e+7>>6)+1)*16,r[f-1]=e*8-8;A<f;A+=16){for(i=0;i<80;i++)e=w(a,5)+d+(i<20?(g&p^~g&c)+1518500249:i<40?(g^p^c)+1859775393:i<60?(g&p^g&c^p&c)+2400959708:(g^p^c)+3395469782)+(u[i]=i<16?r[A+i]|0:w(u[i-3]^u[i-8]^u[i-14]^u[i-16],1)),d=c,c=p,p=w(g,30),g=a,a=e;h[0]=a=h[0]+a|0,h[1]=g=h[1]+g|0,h[2]=p=h[2]+p|0,h[3]=c=h[3]+c|0,h[4]=d=h[4]+d|0}for(i=0;i<40;i++)S+=(h[i>>3]>>>(7-(i&7))*4&15).toString(16);return S}function L(o){return/^[0-9a-f]{11,}$/i.test(o)&&o}function D(o){return nt(o==null?"":""+o)}class ot{constructor(n,t){const i=n.canvas,e=i.width,s=i.height;n.save(),t||(t=Math.min(e,s),n.translate((e-t)/2|0,(s-t)/2|0)),this.l=n,this.k=t,n.clearRect(0,0,t,t)}m(n){const t=this.l,i=this.k;t.fillStyle=M(n),t.fillRect(0,0,i,i)}L(n){const t=this.l;t.fillStyle=M(n),t.beginPath()}M(){this.l.fill()}g(n){const t=this.l;t.moveTo(n[0].x,n[0].y);for(let i=1;i<n.length;i++)t.lineTo(n[i].x,n[i].y);t.closePath()}h(n,t,i){const e=this.l,s=t/2;e.moveTo(n.x+s,n.y+s),e.arc(n.x+s,n.y+s,s,0,Math.PI*2,i),e.closePath()}finish(){this.l.restore()}}const F=1,it=2,B={Z:"data-jdenticon-hash",N:"data-jdenticon-value"},et="jdenticonRendered",G=typeof document<"u"&&document.querySelectorAll.bind(document);function st(o){if(o){const n=o.tagName;if(/^svg$/i.test(n))return F;if(/^canvas$/i.test(n)&&"getContext"in o)return it}}function C(o){return(o*10+.5|0)/10}class rt{constructor(){this.v=""}g(n){let t="";for(let i=0;i<n.length;i++)t+=(i?"L":"M")+C(n[i].x)+" "+C(n[i].y);this.v+=t+"Z"}h(n,t,i){const e=i?0:1,s=C(t/2),r=C(t),f="a"+s+","+s+" 0 1,"+e+" ";this.v+="M"+C(n.x)+" "+C(n.y+t/2)+f+r+",0"+f+-r+",0"}}class at{constructor(n){this.A,this.B={},this.O=n,this.k=n.k}m(n){const t=/^(#......)(..)?/.exec(n),i=t[2]?b(t[2],0)/255:1;this.O.m(t[1],i)}L(n){this.A=this.B[n]||(this.B[n]=new rt)}M(){}g(n){this.A.g(n)}h(n,t,i){this.A.h(n,t,i)}finish(){const n=this.B;for(let t in n)n.hasOwnProperty(t)&&this.O.P(t,n[t].v)}}const R={R:"http://www.w3.org/2000/svg",S:"width",T:"height"};function J(o,n,...t){const i=document.createElementNS(R.R,n);for(let e=0;e+1<t.length;e+=2)i.setAttribute(t[e],t[e+1]);o.appendChild(i)}class ut{constructor(n){const t=this.k=Math.min(Number(n.getAttribute(R.S))||100,Number(n.getAttribute(R.T))||100);for(this.U=n;n.firstChild;)n.removeChild(n.firstChild);n.setAttribute("viewBox","0 0 "+t+" "+t),n.setAttribute("preserveAspectRatio","xMidYMid meet")}m(n,t){t&&J(this.U,"rect",R.S,"100%",R.T,"100%","fill",n,"opacity",t)}P(n,t){J(this.U,"path","fill",n,"d",t)}}function Z(o,n,t){Y(o,n,t,function(i,e){if(e)return e==F?new at(new ut(i)):new ot(i.getContext("2d"))})}function Y(o,n,t,i){if(typeof o=="string"){if(G){const r=G(o);for(let f=0;f<r.length;f++)Y(r[f],n,t,i)}return}const e=L(n)||n!=null&&D(n)||L(o.getAttribute(B.Z))||o.hasAttribute(B.N)&&D(o.getAttribute(B.N));if(!e)return;const s=i(o,st(o));s&&(tt(s,e,t),o[et]=!0)}const ft=["data-jdenticon-value","width","height"],K=m.defineComponent({__name:"Jdenticon",props:m.mergeModels({size:{default:100}},{modelValue:{},modelModifiers:{}}),emits:["update:modelValue"],setup(o){const n=m.useTemplateRef("icon"),t=m.useModel(o,"modelValue");return m.watch([n,t],()=>{n.value&&Z(n.value,t.value??"")}),(i,e)=>(m.openBlock(),m.createElementBlock("svg",{ref_key:"icon",ref:n,"data-jdenticon-value":t.value,width:i.size,height:i.size},null,8,ft))}}),ht=["data-jdenticon-value","width","height"],q=m.defineComponent({__name:"StaticJdenticon",props:{size:{default:100},value:{}},setup(o){const n=m.useTemplateRef("icon");return m.watch([n],()=>{n.value&&Z(n.value,o.value??"")}),(t,i)=>(m.openBlock(),m.createElementBlock("svg",{ref_key:"icon",ref:n,"data-jdenticon-value":t.value,width:t.size,height:t.size},null,8,ht))}}),ct={install:o=>{o.component("Jdenticon",K),o.component("StaticJdenticon",q)}};_.Jdenticon=K,_.StaticJdenticon=q,_.default=ct,Object.defineProperties(_,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});