-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.min.js
16 lines (16 loc) · 5.7 KB
/
app.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* Created by alejandrorioscalera
* On 20/10/17
*
* -- SOCIAL NETWORKS --
*
* GitHub: https://github.com/clonalejandro or @clonalejandro
* Website: https://clonalejandro.me/
* Twitter: https://twitter.com/clonalejandro11/ or @clonalejandro11
* Keybase: https://keybase.io/clonalejandro/
*
* -- LICENSE --
*
* All rights reserved for clonalejandro ©malextrap 2017 / 2018
*/
function Enum(a){let i=Object.keys(a).reduce((o,k)=>(o[a[k]]=k,o),{});return Object.freeze(Object.keys(a).reduce((o,k)=>(o[k]=a[k],o),v=>i[v]))}class CMath{constructor(){}static Miliseconds(v){return 1e3*v}}const labels=$("label[for]"),iField=$("#f1"),typeF=$("#f2"),bubble=$(".bubble"),oField=$("#f3"),reset=$("#reset");var err=0,notify_en=!1;const options=()=>{"HEX"===typeF.val()&&labels.each(pos=>{switch($(labels[pos]).attr("for")){default:break;case"f1":$(labels[pos]).text("RGB color");break;case"f3":$(labels[pos]).text("HEX color")}}),"RGB"===typeF.val()&&labels.each(pos=>{switch($(labels[pos]).attr("for")){default:break;case"f1":$(labels[pos]).text("HEX color");break;case"f3":$(labels[pos]).text("RGB color")}}),"RGBA"===typeF.val()&&labels.each(pos=>{switch($(labels[pos]).attr("for")){default:break;case"f1":$(labels[pos]).text("HEX color");break;case"f3":$(labels[pos]).text("RGBA color")}})},reflectValues=()=>{const v=iField.val(),type=typeF.val();if("RGB"===type)if(0==(err="#"!==v.toString().charAt(0)?1:0)){const result=new Converter(v).hexToRGB();oField.val(result),bubble.attr("data-clipboard-text",result.toString())}else{const type=n_Types.orange,size=n_Sizes.normal,delay=CMath.Miliseconds(10.5);notify_en||new UtilNotify(type,"Syntax error","You need to put #hexcode example #ffffff",{x:"right",y:"top"},size,!0,delay),notify_en=!0}if("HEX"===type)if(0==(err="rgb"!==v.toString().substring(0,3)&&"("!==v.toString().charAt(4)&&")"!==v.toString().charAt(v.toString().length+1)?1:0)){let pres=v.toString();for(;pres.indexOf(",")>-1;)pres=pres.replace(",","");pres=(pres=pres.substring(4,pres.length+1)).substring(0,pres.length-1);const result=new Converter(pres).rgbToHex();oField.val(result),bubble.attr("data-clipboard-text",result.toString())}else{const type=n_Types.orange,size=n_Sizes.normal,delay=CMath.Miliseconds(10.5);notify_en||new UtilNotify(type,"Syntax error","You need to put rgb-code example rgb(RED, GREEN, BLUE)",{x:"right",y:"top"},size,!0,delay),notify_en=!0}},copyBubble=()=>{const data=bubble.attr("data-clipboard-text");if(null!==data||void 0!==data||""!==data)try{const type=n_Types.green,size=n_Sizes.normal,delay=CMath.Miliseconds(10.5);notify_en||new UtilNotify(type,"Success","Has been copy to clipboard your result",{x:"right",y:"top"},size,!0,delay),notify_en=!0,new Clipboard(".bubble")}catch(err){console.log(err.message);const type=n_Types.red,message=err.message,size=n_Sizes.normal,delay=CMath.Miliseconds(10.5);throw notify_en||new UtilNotify(type,"Several error",message,{x:"right",y:"top"},size,!0,delay),notify_en=!0,err}},resetHandler=()=>new TaskReset;class TaskHidder{constructor(time){const task=setInterval(()=>{0!==time?time--:(notify_en=!1,this.kill(task))},CMath.Miliseconds(1))}kill(e){return clearInterval(e)}}class TaskReset{constructor(){let time="2";const task=setInterval(()=>{0!==time?time--:this.kill(task),options()},time)}kill(e){return clearInterval(e)}}class UtilNotify{constructor(type,title,message,location,size,hidden,delay){return notify({type,title,message,position:location,icon:'<i class="fa fa-check"></i>',size,overlay:!1,closeBtn:hidden,overflowHide:!1,spacing:20,theme:"default",autoHide:!0,delay,onShow:()=>{new TaskHidder(parseInt(String(String(delay/1e3).charAt(0)+(null===String(delay/1e3).charAt(1)?"":String(delay).charAt(1)))))},onClick:null,onHide:null,template:'<div class="notify"><div class="notify-text"></div></div>'})}}const n_Types=Enum({default:"alert",green:"success",red:"error",blue:"info",orange:"warning"}),n_Sizes=Enum({small:"small",normal:"normal",big:"full"});var code="";class Converter{constructor(code){this.code=code}hexToRGB(){return Hex.toRGB(this.code)}rgbToHex(){const arr=[];let bool=!1,flip=1,istwo=!1,istree=!1;for(let i=0;i<this.code.length;i++)" "!==this.code.charAt(i)&&(0!==arr.length&&1!==arr.length||(bool?arr[0].length<3&&(arr[0]+=this.code.charAt(i)):(arr[0]=this.code.charAt(i),bool=!0)),(1===arr.length&&arr[0].length>=3||2===arr.length&&arr[0].length>=3)&&(istwo?(1===flip&&(flip=0,bool=!1),bool?arr[1].length<3&&(arr[1]+=this.code.charAt(i)):(arr[1]=this.code.charAt(i),bool=!0)):istwo=!0),(2===arr.length&&arr[1].length>=3||3===arr.length&&arr[1].length>=3)&&(istree?(istwo?arr[2]=this.code.charAt(i):arr[2]+=this.code.charAt(i),istwo=!1):istree=!0));try{return"#"+RGB.toHex(arr[0],arr[1],arr[2])}catch(err){const type=n_Types.red,message=err.toString(),size=n_Sizes.normal,delay=CMath.Miliseconds(10.5);return notify_en||new UtilNotify(type,"Several error",message,{x:"right",y:"top"},size,!0,delay),notify_en=!0,null}}}class Hex{constructor(){}static toRGB(hex){const h=new Hex;return"rgb("+String(h.hexToR(hex))+", "+String(h.hexToG(hex))+", "+String(h.hexToB(hex))+")"}hexToR(h){return parseInt(this.cutHex(h).substring(0,2),16)}hexToG(h){return parseInt(this.cutHex(h).substring(2,4),16)}hexToB(h){return parseInt(this.cutHex(h).substring(4,6),16)}cutHex(h){return"#"===h.charAt(0)?h.substring(1,7):h}}class RGB{constructor(){}static toHex(R,G,B){const rgb=new RGB;return rgb.rgbToHex(R)+rgb.rgbToHex(G)+rgb.rgbToHex(B)}rgbToHex(n){return n=parseInt(n,10),isNaN(n)?"00":(n=Math.max(0,Math.min(n,255)),"0123456789ABCDEF".charAt((n-n%16)/16)+"0123456789ABCDEF".charAt(n%16))}}$(document).ready(()=>{options()}),typeF.on("change",()=>{options()}),reset.on("click",()=>{resetHandler()}),iField.on("autocomplete keyup blur keypress change",()=>{reflectValues()}),bubble.on("click",()=>{copyBubble()});