-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmfobj-parser.min.js
More file actions
1 lines (1 loc) · 4.47 KB
/
mfobj-parser.min.js
File metadata and controls
1 lines (1 loc) · 4.47 KB
1
var mfobj={};mfobj.settings={color:"white",width:16,height:12},mfobj.repo="https://github.com/bribes/mobile-obj-parser",mfobj.author={name:"Faav",github:"bribes"},mfobj.isMobile=function(){try{return document.createEvent("TouchEvent"),!0}catch(t){return!1}};class Color{constructor(t,s,$){this.set(t,s,$)}toString(){return`rgb(${Math.round(this.r)}, ${Math.round(this.g)}, ${Math.round(this.b)})`}set(t,s,$){this.r=this.clamp(t),this.g=this.clamp(s),this.b=this.clamp($)}hueRotate(t=0){t=t/180*Math.PI;let s=Math.sin(t),$=Math.cos(t);this.multiply([.213+.787*$-.213*s,.715-.715*$-.715*s,.072-.072*$+.928*s,.213-.213*$+.143*s,.715+.285*$+.14*s,.072-.072*$-.283*s,.213-.213*$-.787*s,.715-.715*$+.715*s,.072+.928*$+.072*s,])}grayscale(t=1){this.multiply([.2126+.7874*(1-t),.7152-.7152*(1-t),.0722-.0722*(1-t),.2126-.2126*(1-t),.7152+.2848*(1-t),.0722-.0722*(1-t),.2126-.2126*(1-t),.7152-.7152*(1-t),.0722+.9278*(1-t),])}sepia(t=1){this.multiply([.393+.607*(1-t),.769-.769*(1-t),.189-.189*(1-t),.349-.349*(1-t),.686+.314*(1-t),.168-.168*(1-t),.272-.272*(1-t),.534-.534*(1-t),.131+.869*(1-t),])}saturate(t=1){this.multiply([.213+.787*t,.715-.715*t,.072-.072*t,.213-.213*t,.715+.285*t,.072-.072*t,.213-.213*t,.715-.715*t,.072+.928*t,])}multiply(t){let s=this.clamp(this.r*t[0]+this.g*t[1]+this.b*t[2]),$=this.clamp(this.r*t[3]+this.g*t[4]+this.b*t[5]),e=this.clamp(this.r*t[6]+this.g*t[7]+this.b*t[8]);this.r=s,this.g=$,this.b=e}brightness(t=1){this.linear(t)}contrast(t=1){this.linear(t,-(.5*t)+.5)}linear(t=1,s=0){this.r=this.clamp(this.r*t+255*s),this.g=this.clamp(this.g*t+255*s),this.b=this.clamp(this.b*t+255*s)}invert(t=1){this.r=this.clamp((t+this.r/255*(1-2*t))*255),this.g=this.clamp((t+this.g/255*(1-2*t))*255),this.b=this.clamp((t+this.b/255*(1-2*t))*255)}hsl(){let t=this.r/255,s=this.g/255,$=this.b/255,e=Math.max(t,s,$),i=Math.min(t,s,$),r,l,h=(e+i)/2;if(e===i)r=l=0;else{let _=e-i;switch(l=h>.5?_/(2-e-i):_/(e+i),e){case t:r=(s-$)/_+(s<$?6:0);break;case s:r=($-t)/_+2;break;case $:r=(t-s)/_+4}r/=6}return{h:100*r,s:100*l,l:100*h}}clamp(t){return t>255?t=255:t<0&&(t=0),t}}class Solver{constructor(t,s){this.target=t,this.targetHSL=t.hsl(),this.reusedColor=new Color(0,0,0)}solve(){let t=this.solveNarrow(this.solveWide());return{values:t.values,loss:t.loss,filter:this.css(t.values)}}solveWide(){let t=[60,180,18e3,600,1.2,1.2],s={loss:1/0};for(let $=0;s.loss>25&&$<3;$++){let e=[50,20,3750,50,100,100],i=this.spsa(5,t,15,e,1e3);i.loss<s.loss&&(s=i)}return s}solveNarrow(t){let s=t.loss,$=s+1;return this.spsa(s,[.25*$,.25*$,$,.25*$,.2*$,.2*$],2,t.values,500)}spsa(t,s,$,e,i){let r=null,l=1/0,h=Array(6),_=Array(6),o=Array(6);for(let a=0;a<i;a++){let n=$/Math.pow(a+1,.16666666666666666);for(let c=0;c<6;c++)h[c]=Math.random()>.5?1:-1,_[c]=e[c]+n*h[c],o[c]=e[c]-n*h[c];let u=this.loss(_)-this.loss(o);for(let b=0;b<6;b++){let g=u/(2*n)*h[b],m=s[b]/Math.pow(t+a+1,1);e[b]=d(e[b]-m*g,b)}let p=this.loss(e);p<l&&(r=e.slice(0),l=p)}return{values:r,loss:l};function d(t,s){let $=100;return 2===s?$=7500:(4===s||5===s)&&($=200),3===s?t>$?t%=$:t<0&&(t=$+t%$):t<0?t=0:t>$&&(t=$),t}}loss(t){let s=this.reusedColor;s.set(0,0,0),s.invert(t[0]/100),s.sepia(t[1]/100),s.saturate(t[2]/100),s.hueRotate(3.6*t[3]),s.brightness(t[4]/100),s.contrast(t[5]/100);let $=s.hsl();return Math.abs(s.r-this.target.r)+Math.abs(s.g-this.target.g)+Math.abs(s.b-this.target.b)+Math.abs($.h-this.targetHSL.h)+Math.abs($.s-this.targetHSL.s)+Math.abs($.l-this.targetHSL.l)}css(t){function s(s,$=1){return Math.round(t[s]*$)}return`filter: invert(${s(0)}%) sepia(${s(1)}%) saturate(${s(2)}%) hue-rotate(${s(3,3.6)}deg) brightness(${s(4)}%) contrast(${s(5)}%);`}}const hexToRgb=t=>{let s=/^#?([a-f\d])([a-f\d])([a-f\d])$/i;t=t.replace(s,(t,s,$,e)=>s+s+$+$+e+e);let $=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(t);return $?[parseInt($[1],16),parseInt($[2],16),parseInt($[3],16),]:null},standardize_color=t=>{if(t.startsWith("#"))return t;var s=document.createElement("canvas").getContext("2d");return s.fillStyle=t,s.fillStyle},hexCodeToFilter=t=>{let s=hexToRgb(standardize_color(t));if(void 0===s||null==s||3!==s.length)return"";let $=new Color(s[0],s[1],s[2]),e=new Solver($),i=e.solve();return i.filter};mfobj.parse=function(t,s={}){if(!0!=mfobj.isMobile())return t;var $=mfobj.settings;return s!=={}&&(s.color&&($.color=s.color),s.width&&($.width=s.width),s.height&&($.height=s.height)),t.split("").join(`<img src="https://cdn.jsdelivr.net/gh/bribes/mobile-obj-parser/images/obj_black.png" style="${hexCodeToFilter($.color)}" width="${$.width}" height="${$.height}" class="mobile-obj">`)};