UNPKG

csx

Version:

Utility functions for TypeStyle

60 lines (59 loc) 7.61 kB
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((t=t||self).csx={})}(this,function(t){"use strict" function a(t){return t||0===t?t.toString():""}var h=/[\s]*([a-z-]+)[\s]*\([\s]*([^\)]+)[\s]*\)[\s]*/i,r=/^(\-?\d+\.?\d{0,5})/,n=function(r){return function(t){return t+r}},v=parseFloat function g(t){return"number"==typeof t?t:.01*v(t)}function c(t){return s(100*t)+"%"}function s(t){return r.exec(t.toString())[1]}function p(t,r){return t+"("+Array.prototype.join.call(r,", ")+")"}function e(t){return function(){return p(t,arguments)}}var o=n("%"),i=n("deg"),u=n("em"),f=n("ex"),l=n("px"),d=n("rad"),y=n("rem"),b=n("vh"),w=n("vw"),m=n("turn"),x=function(n){return function(){return(t=arguments,r=function(t){return t||0===t},Array.prototype.filter.call(t,r)).map(function(t){return"number"==typeof t?l(t):t.toString()}).join(n) var t,r}},S=x(" "),k=x(",") var H,X=S,Y=S,j=S,G=Math.round function A(t,r){return G(t*r)/r}var B="rgb",L="hsl",M=((H={})[B+L]=function(t,r,n,e,o){var a,i,u=t/255,f=r/255,c=n/255,s=Math.min(u,f,c),p=Math.max(u,f,c),l=(s+p)/2,h=p-s a=p===s?0:u===p?(f-c)/h:f===p?2+(c-u)/h:c===p?4+(u-f)/h:0;(a=Math.min(60*a,360))<0&&(a+=360) i=p===s?0:.5<l?h/(2-p-s):h/(p+s) return new I(L,a,i,l,e,o)},H[L+B]=function(t,r,n,e,o){var a=t/360,i=r,u=n if(0===i){var f=255*u return new I(B,f,f,f,e,o)}for(var c=u<.5?u*(1+i):u+i-u*i,s=2*u-c,p=0,l=0,h=0,v=0;v<3;v++){var g=a+1/3*-(v-1) g<0&&g++,1<g&&g-- var f=void 0 f=6*g<1?s+6*(c-s)*g:2*g<1?c:3*g<2?s+(c-s)*(2/3-g)*6:s,f*=255,0===v?p=f:1===v?l=f:h=f}return new I(B,p,l,h,e,o)},H),Z={r:255,g:255,b:255,h:360,s:1,l:1,a:1} function R(t){return function(t){var r=t.match(/#(([a-f0-9]{6})|([a-f0-9]{3}))$/i) if(!r)return var n=r[1],e=parseInt(3===n.length?n[0]+n[0]+n[1]+n[1]+n[2]+n[2]:n,16) return new I(B,e>>16&255,e>>8&255,255&e,1,!1)}(t)||function(t){var r=function(t){var r=h.exec(t) if(r&&r.length)return[r[1]].concat(r[2].split(","))}(t) if(!r||4!==r.length&&5!==r.length)return var n,e=r[0],o="rgba"===e,a="hsla"===e,i=e===B,u=e===L,f=a||o if(i||o)n=B else{if(!u&&!a)throw new Error("unsupported color string") n=L}var c=v(r[1]),s=i||o?v(r[2]):g(r[2]),p=i||o?v(r[3]):g(r[3]),l=f?v(r[4]):1 return new I(n,c,s,p,l,f)}(t)||z(255,0,0)}function z(t,r,n,e){return new I(B,t,r,n,void 0===e?1:g(e),void 0!==e)}function C(t,r,n){var e=r.f,o=r.r,a=r.g,i=r.b,u=r.a,f=void 0===n?r.o:n return e!==t?M[e+t](o,a,i,u,f):void 0===n?r:new I(e,o,a,i,u,f)}var I=function(){function l(t,r,n,e,o,a){var i=this i.f=t,i.o=a var u=t===L i.r=W(u?"h":"r",r),i.g=W(u?"s":"g",n),i.b=W(u?"l":"b",e),i.a=W("a",o)}return l.prototype.toString=function(){var t,r,n=this,e=n.o,o=n.f,a=n.r,i=n.g,u=n.b,f=n.a if(o===B)t=e?"rgba":B,r=[G(a),G(i),G(u)] else{if(o!==L)throw new Error("Invalid color format") t=e?"hsla":L,r=[G(a),c(A(i,100)),c(A(u,100))]}return e&&r.push(s(A(f,1e5))),p(t,r)},l.prototype.toHexString=function(){var t=C(B,this) return"#"+(E(t.r)+E(t.g)+E(t.b)).toUpperCase()},l.prototype.toHSL=function(){return C(L,this,!1)},l.prototype.toHSLA=function(){return C(L,this,!0)},l.prototype.toRGB=function(){return C(B,this,!1)},l.prototype.toRGBA=function(){return C(B,this,!0)},l.prototype.red=function(){var _=this return(_.f===B?_:_.toRGB()).r},l.prototype.green=function(){var _=this return(_.f===B?_:_.toRGB()).g},l.prototype.blue=function(){var _=this return(_.f===B?_:_.toRGB()).b},l.prototype.hue=function(){var _=this return(_.f===L?_:_.toHSL()).r},l.prototype.saturation=function(){var _=this return(_.f===L?_:_.toHSL()).g},l.prototype.lightness=function(){var _=this return(_.f===L?_:_.toHSL()).b},l.prototype.alpha=function(){return this.a},l.prototype.opacity=function(){return this.a},l.prototype.invert=function(){var _=this,t=C(B,_) return C(_.f,new l(B,255-t.r,255-t.g,255-t.b,_.a,_.o))},l.prototype.lighten=function(t,r){var _=this,n=C(L,_),e=Z.l,o=n.b+(r?e-n.b:e)*g(t) return C(_.f,new l(L,n.r,n.g,o,_.a,_.o))},l.prototype.darken=function(t,r){var _=this,n=C(L,_),e=n.b-(r?n.b:Z.l)*g(t) return C(_.f,new l(L,n.r,n.g,e,_.a,_.o))},l.prototype.saturate=function(t,r){var _=this,n=C(L,_),e=Z.s,o=n.g+(r?e-n.g:e)*g(t) return C(_.f,new l(L,n.r,o,n.b,_.a,_.o))},l.prototype.desaturate=function(t,r){var _=this,n=C(L,_),e=Z.s,o=n.g-(r?n.g:e)*g(t) return C(_.f,new l(L,n.r,o,n.b,_.a,_.o))},l.prototype.grayscale=function(){return this.desaturate(1)},l.prototype.fade=function(t){var _=this,r=W("a",g(t)) return C(_.f,new l(_.f,_.r,_.g,_.b,r,!0))},l.prototype.fadeOut=function(t,r){var _=this,n=W("a",_.a-(r?_.a:1)*g(t)) return C(_.f,new l(_.f,_.r,_.g,_.b,n,!0))},l.prototype.fadeIn=function(t,r){var _=this,n=W("a",_.a+(r?_.a:1)*g(t)) return C(_.f,new l(_.f,_.r,_.g,_.b,n,!0))},l.prototype.mix=function(t,r){var n,e=(n=t)instanceof I?n:R(n),o=C(B,this),a=C(B,e),i=void 0===r?.5:r,u=2*i-1,f=Math.abs(o.a-a.a),c=((u*f==-1?u:(u+f)/(1+u*f))+1)/2,s=1-c,p=new l(B,G(o.r*c+a.r*s),G(o.g*c+a.g*s),G(o.b*c+a.b*s),o.a*i+a.a*(1-i),this.o||e.o) return C(this.f,p)},l.prototype.tint=function(t){return z(255,255,255).mix(this,t)},l.prototype.shade=function(t){return z(0,0,0).mix(this,t)},l.prototype.spin=function(t){var _=this,r=C(L,_) return C(_.f,new l(L,O(r.r+t),r.g,r.b,_.a,_.o))},l}() function E(t){var r=G(t) return(r<16?"0":"")+r.toString(16)}function O(t){return((t<0?360:0)+t%360)%360}function W(t,r){var n=0,e=Z[t] return r<n?n:e<r?e:r}function q(t){return Array.isArray(t)?t.map(function(t){return t.toString()}).join(" "):t.toString()}var F=S,P=S var U=e("matrix"),$=e("matrix3d"),D=e("perspective"),J=e("rotate"),K=e("rotate3d"),N=e("rotateX"),Q=e("rotateY"),T=e("rotateZ"),V=e("scale"),tt=e("scale3d"),rt=e("scaleX"),nt=e("scaleY"),et=e("scaleZ"),ot=e("skew"),at=e("skewX"),it=e("skewY"),ut=e("translate"),ft=e("translate3d"),ct=e("translateX"),st=e("translateY"),pt=e("translateZ") t.ColorHelper=I,t.background=function(){for(var t="",r=0;r<arguments.length;r++){var n=arguments[r],e=n.size?"/"+n.size:"",o=[a(n.image),a(n.position)+e,a(n.repeat),a(n.origin),a(n.clip),a(n.attachment),a(n.color)].filter(Boolean).join(" ") t+=(t.length&&o?", ":"")+o}return t},t.border=function(t){return S(t.color,t.style,function(t){if(null!=t){var r=+t return r==r?t+"px":t}}(t.width))},t.borderColor=X,t.borderStyle=Y,t.borderWidth=j,t.calc=function(t){return"calc("+t+")"},t.coalesce=a,t.color=R,t.deg=i,t.em=u,t.ex=f,t.hsl=function(t,r,n,e){return new I(L,O(t),g(r),g(n),void 0===e?1:g(e),void 0!==e)},t.hsla=function(t,r,n,e){return new I(L,O(t),g(r),g(n),g(e),!0)},t.important=function(t){return t||0===t?t.toString()+" !important":""},t.linearGradient=function(t){for(var r=[],n=1;n<arguments.length;n++)r[n-1]=arguments[n] return p("linear-gradient",[t].concat(r.map(q)))},t.list=k,t.margin=F,t.matrix=U,t.matrix3d=$,t.padding=P,t.params=S,t.percent=o,t.perspective=D,t.px=l,t.quote=function(t){return"'"+(t||0===t?t.toString():"").replace(/\'/g,"\\'")+"'"},t.rad=d,t.rem=y,t.repeatingLinearGradient=function(t){for(var r=[],n=1;n<arguments.length;n++)r[n-1]=arguments[n] return p("repeating-linear-gradient",[t].concat(r.map(q)))},t.rgb=z,t.rgba=function(t,r,n,e){return new I(B,t,r,n,g(e),!0)},t.rotate=J,t.rotate3d=K,t.rotateX=N,t.rotateY=Q,t.rotateZ=T,t.scale=V,t.scale3d=tt,t.scaleX=rt,t.scaleY=nt,t.scaleZ=et,t.skew=ot,t.skewX=at,t.skewY=it,t.transform=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r] return t.length?t.join(" "):"none"},t.translate=ut,t.translate3d=ft,t.translateX=ct,t.translateY=st,t.translateZ=pt,t.turn=m,t.url=function(t){return"url("+(t||"")+")"},t.viewHeight=b,t.viewWidth=w,Object.defineProperty(t,"__esModule",{value:!0})})