mirror of
https://github.com/geode-sdk/geode.git
synced 2024-11-27 01:45:35 -05:00
6 lines
No EOL
14 KiB
JavaScript
6 lines
No EOL
14 KiB
JavaScript
/*!
|
|
* palettify v1.0.3
|
|
* (c) 2018 Dobromir Hristov
|
|
* Released under the MIT License.
|
|
*/
|
|
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.palettify=e()}(this,function(){"use strict";function t(t){switch(typeof t){case"string":case"number":case"boolean":return t;case"object":try{return null===t?"":JSON.stringify(t)}catch(t){return"{...}"}default:return""}}function e(t,n,r){if(void 0===r&&(r=0),"object"!=typeof t||null===t||void 0===t)return"";var o=n[r],a=t[o];return r===n.length-1?a:e(a,n,++r)}function n(t,n){return e(n,t.split("."))}function r(e,r,o){return void 0===r&&(r={}),void 0===o&&(o=n),"string"!=typeof e?e:e.replace(g,function(e,a){try{return t(o(a,r))}catch(e){return t(n(a,r))}})}function o(t){return!!t&&"object"==typeof t}function a(t){var e=Object.prototype.toString.call(t);return"[object RegExp]"===e||"[object Date]"===e||i(t)}function i(t){return t.$$typeof===m}function s(t){return Array.isArray(t)?[]:{}}function c(t,e){return e&&!0===e.clone&&d(t)?f(s(t),t,e):t}function u(t,e,n){var r=t.slice();return e.forEach(function(e,o){void 0===r[o]?r[o]=c(e,n):d(e)?r[o]=f(t[o],e,n):-1===t.indexOf(e)&&r.push(c(e,n))}),r}function l(t,e,n){var r={};return d(t)&&Object.keys(t).forEach(function(e){r[e]=c(t[e],n)}),Object.keys(e).forEach(function(o){d(e[o])&&t[o]?r[o]=f(t[o],e[o],n):r[o]=c(e[o],n)}),r}function f(t,e,n){var r=Array.isArray(e),o=Array.isArray(t),a=n||{arrayMerge:u};if(r===o)return r?(a.arrayMerge||u)(t,e,n):l(t,e,n);return c(e,n)}function p(){function t(t,e){var n=p(t);return new Promise(function(t){f(n)?t((new v).getPalette(n,e)):n.onload=function(){t((new v).getPalette(n,e))}})}function e(t,e){return t.map(function(t,r){var o=1;return Array.isArray(e)&&e[r]&&(o=e[r]),n(t,o)})}function n(t,e){return"rgba("+t[0]+", "+t[1]+", "+t[2]+", "+e+")"}function r(t){var e=t.target,n=t.styles,r=t.palette,o=t.staticCallback;void 0===o&&(o=null);var a=t.obj;for(var i in n)n.hasOwnProperty(i)&&(e.style[i]=y(n[i],r));o&&o.call(e,a,m.options)}function o(t,e,n,r){for(var o in e)e.hasOwnProperty(o)&&!n.hasOwnProperty(o)?t.style[o]="":n.hasOwnProperty(o)&&(t.style[o]=y(n[o],r))}function a(t){m.options=E(b,t,{clone:!0,arrayMerge:g}),Object.keys(m.options).forEach(function(t){if(m.options[t]instanceof Error)throw m.options[t]})}function i(t){return(299*t[0]+587*t[1]+114*t[2])/1e3}function s(t){return i(t)<170}function c(t,e){return t.map(function(t){return s(t)?e.light:e.dark})}function u(t){var e=document.createElement("a");return e.href=t,e}function l(t){return document.location.host!==u(t).host}function f(t){return!!t.complete&&(void 0===t.naturalWidth||0!==t.naturalWidth)}function p(t){var e=t,n="IMG"!==e.tagName,r=n?t.style.backgroundImage.replace("url(","").replace(")","").replace(/"/gi,""):t.src,o=l(r);if(!e)throw Error("Target is not an element",e);if(n||o&&!e.crossOrigin){if(n&&!e.style.backgroundImage)throw Error("Tag provided is not an image and does not have a background-image style attached to it.");e=new Image(t.naturalWidth,t.naturalHeight),o&&(e.crossOrigin="anonymous"),e.src=r}return e}function g(t,e,n){return e}var h=!1,d=null,b={selector:Error("Please provide \u0430 selector in your options."),eventTarget:Error("Please provide an eventTarget as a parent for your image in the options."),image:Error("Please provide an image to sample."),styleTarget:null,contrastColors:{light:"#fff",dark:"#000"},activeClass:"palettify--active",readyClass:"palettify--ready",colorsToExtract:3,enterEvent:"mouseenter",leaveEvent:"mouseleave",styles:{opacities:[.5,.5,.5],static:{},dynamic:{}},staticCallback:null,beforeEnterCallback:null,afterEnterCallback:null,beforeLeaveCallback:null,afterLeaveCallback:null,onReadyCallback:null},m={options:{},data:[],collectElements:function(){var t="";if(!(d="string"==typeof m.options.selector?document.querySelector(m.options.selector):m.options.selector))throw new Error("Selector "+m.options.selector+" does not exist");t=d.querySelectorAll(m.options.eventTarget),[].slice.call(t,0).forEach(function(t){var e=t.querySelector(m.options.image),n=!0===m.options.styleTarget?t:t.querySelector(m.options.styleTarget||m.options.image),r={eventTarget:t,styleTarget:n,image:e,palette:{original:[],rgb:[],rgba:[]}};m.data.push(r)})},extractColorsAndAttachStyles:function(n){void 0===n&&(n=!1);var o=[];m.data.forEach(function(n,a){o[a]=t(n.image,m.options.colorsToExtract).then(function(t){n.palette.original=t,n.palette.rgb=e(n.palette.original,[]),n.palette.rgba=e(n.palette.original,m.options.styles.opacities),n.palette.contrastColors=c(n.palette.original,m.options.contrastColors),r({target:n.styleTarget,styles:m.options.styles.static,palette:n.palette,staticCallback:m.options.staticCallback,obj:n})})}),n||Promise.all(o).then(function(t){d.classList.add(m.options.readyClass),"function"==typeof m.options.onReadyCallback&&m.options.onReadyCallback.call(m,m)})},generateEnterHandler:function(t){return function(e){t.styleTarget&&("function"==typeof m.options.beforeEnterCallback&&m.options.beforeEnterCallback.call(t.styleTarget,t,m.options,e),t.styleTarget.classList.add(m.options.activeClass),r({target:t.styleTarget,styles:m.options.styles.dynamic,palette:t.palette,obj:t}),"function"==typeof m.options.afterEnterCallback&&m.options.afterEnterCallback.call(t.styleTarget,t,m.options,e))}},generateLeaveHandler:function(t){return function(e){var n=t.styleTarget;n&&("function"==typeof m.options.beforeLeaveCallback&&m.options.beforeLeaveCallback.call(t.styleTarget,t,m.options,e),n.classList.remove(m.options.activeClass),o(t.styleTarget,m.options.styles.dynamic,m.options.styles.static,t.palette),"function"==typeof m.options.afterLeaveCallback&&m.options.afterLeaveCallback.call(t.styleTarget,t,m.options,e))}},attachEventListeners:function(){var t=m.options,e=t.enterEvent,n=t.leaveEvent;e=Array.isArray(e)?e:[e],n=Array.isArray(n)?n:[n],m.data.forEach(function(t){t.enterHandler=m.generateEnterHandler(t),t.leaveHandler=m.generateLeaveHandler(t),e.forEach(function(e){t.eventTarget.addEventListener(e,t.enterHandler,!1)}),n.forEach(function(e){t.eventTarget.addEventListener(e,t.leaveHandler,!1)})})},detachEventListeners:function(){var t=m.options,e=t.enterEvent,n=t.leaveEvent;e=Array.isArray(e)?e:[e],n=Array.isArray(n)?n:[n],m.data.forEach(function(t){e.forEach(function(e){t.eventTarget.removeEventListener(e,t.enterHandler,!1)}),n.forEach(function(e){t.eventTarget.removeEventListener(e,t.leaveHandler,!1)})})},init:function(t){if(h)throw new Error("Palettify is already initialized");return a(t),m.collectElements(),m.extractColorsAndAttachStyles(),m.attachEventListeners(),h=!0,m},destroy:function(t){void 0===t&&(t=!0),m.detachEventListeners(),t&&m.cleanUp(),m.data=[],h=!1},reInit:function(){m.destroy(),m.init(m.options)},cleanUp:function(){m.data.forEach(function(t){for(var e in m.options.styles.static)m.options.styles.static.hasOwnProperty(e)&&(t.styleTarget.style[e]="")})},setOptions:function(t,e){void 0===e&&(e=!0),m.options=Object.assign({},m.options,t),e&&m.reInit()},isInitialized:function(){return h}};return m}var v=function(){var t=function(){},e=function(t){this.canvas=document.createElement("canvas"),this.context=this.canvas.getContext("2d"),document.body.appendChild(this.canvas),this.width=this.canvas.width=t.width,this.height=this.canvas.height=t.height,this.context.drawImage(t,0,0,this.width,this.height)};e.prototype.clear=function(){this.context.clearRect(0,0,this.width,this.height)},e.prototype.update=function(t){this.context.putImageData(t,0,0)},e.prototype.getPixelCount=function(){return this.width*this.height},e.prototype.getImageData=function(){return this.context.getImageData(0,0,this.width,this.height)},e.prototype.removeCanvas=function(){this.canvas.parentNode.removeChild(this.canvas)};var n={map:function(t,e){var n={};return e?t.map(function(t,r){return n.index=r,e.call(n,t)}):t.slice()},naturalOrder:function(t,e){return t<e?-1:t>e?1:0},sum:function(t,e){var n={};return t.reduce(e?function(t,r,o){return n.index=o,t+e.call(n,r)}:function(t,e){return t+e},0)},max:function(t,e){return Math.max.apply(null,e?n.map(t,e):t)}},r=function(){function t(t,e,n){return(t<<2*u)+(e<<u)+n}function e(t){function e(){n.sort(t),r=!0}var n=[],r=!1;return{push:function(t){n.push(t),r=!1},peek:function(t){return r||e(),void 0===t&&(t=n.length-1),n[t]},pop:function(){return r||e(),n.pop()},size:function(){return n.length},map:function(t){return n.map(t)},debug:function(){return r||e(),n}}}function r(t,e,n,r,o,a,i){var s=this;s.r1=t,s.r2=e,s.g1=n,s.g2=r,s.b1=o,s.b2=a,s.histo=i}function o(){this.vboxes=new e(function(t,e){return n.naturalOrder(t.vbox.count()*t.vbox.volume(),e.vbox.count()*e.vbox.volume())})}function a(e){var n,r,o,a,i=1<<3*u,s=new Array(i);return e.forEach(function(e){r=e[0]>>l,o=e[1]>>l,a=e[2]>>l,n=t(r,o,a),s[n]=(s[n]||0)+1}),s}function i(t,e){var n,o,a,i=1e6,s=0,c=1e6,u=0,f=1e6,p=0;return t.forEach(function(t){n=t[0]>>l,o=t[1]>>l,a=t[2]>>l,n<i?i=n:n>s&&(s=n),o<c?c=o:o>u&&(u=o),a<f?f=a:a>p&&(p=a)}),new r(i,s,c,u,f,p,e)}function s(e,r){if(r.count()){var o=r.r2-r.r1+1,a=r.g2-r.g1+1,i=r.b2-r.b1+1,s=n.max([o,a,i]);if(1==r.count())return[r.copy()];var c,u,l,f,p,v=0,g=[],h=[];if(s==o)for(c=r.r1;c<=r.r2;c++){for(f=0,u=r.g1;u<=r.g2;u++)for(l=r.b1;l<=r.b2;l++)p=t(c,u,l),f+=e[p]||0;v+=f,g[c]=v}else if(s==a)for(c=r.g1;c<=r.g2;c++){for(f=0,u=r.r1;u<=r.r2;u++)for(l=r.b1;l<=r.b2;l++)p=t(u,c,l),f+=e[p]||0;v+=f,g[c]=v}else for(c=r.b1;c<=r.b2;c++){for(f=0,u=r.r1;u<=r.r2;u++)for(l=r.g1;l<=r.g2;l++)p=t(u,l,c),f+=e[p]||0;v+=f,g[c]=v}return g.forEach(function(t,e){h[e]=v-t}),function(t){var e,n,o,a,i,s=t+"1",u=t+"2",l=0;for(c=r[s];c<=r[u];c++)if(g[c]>v/2){for(o=r.copy(),a=r.copy(),e=c-r[s],n=r[u]-c,i=e<=n?Math.min(r[u]-1,~~(c+n/2)):Math.max(r[s],~~(c-1-e/2));!g[i];)i++;for(l=h[i];!l&&g[i-1];)l=h[--i];return o[u]=i,a[s]=o[u]+1,[o,a]}}(s==o?"r":s==a?"g":"b")}}function c(t,r){function c(t,e){for(var n,r=1,o=0;o<f;)if(n=t.pop(),n.count()){var a=s(u,n),i=a[0],c=a[1];if(!i)return;if(t.push(i),c&&(t.push(c),r++),r>=e)return;if(o++>f)return}else t.push(n),o++}if(!t.length||r<2||r>256)return!1;var u=a(t),l=0;u.forEach(function(){l++});var v=i(t,u),g=new e(function(t,e){return n.naturalOrder(t.count(),e.count())});g.push(v),c(g,p*r);for(var h=new e(function(t,e){return n.naturalOrder(t.count()*t.volume(),e.count()*e.volume())});g.size();)h.push(g.pop());c(h,r-h.size());for(var y=new o;h.size();)y.push(h.pop());return y}var u=5,l=8-u,f=1e3,p=.75;return r.prototype={volume:function(t){var e=this;return e._volume&&!t||(e._volume=(e.r2-e.r1+1)*(e.g2-e.g1+1)*(e.b2-e.b1+1)),e._volume},count:function(e){var n=this,r=n.histo;if(!n._count_set||e){var o,a,i,s,c=0;for(a=n.r1;a<=n.r2;a++)for(i=n.g1;i<=n.g2;i++)for(s=n.b1;s<=n.b2;s++)o=t(a,i,s),c+=r[o]||0;n._count=c,n._count_set=!0}return n._count},copy:function(){var t=this;return new r(t.r1,t.r2,t.g1,t.g2,t.b1,t.b2,t.histo)},avg:function(e){var n=this,r=n.histo;if(!n._avg||e){var o,a,i,s,c,l=0,f=1<<8-u,p=0,v=0,g=0;for(a=n.r1;a<=n.r2;a++)for(i=n.g1;i<=n.g2;i++)for(s=n.b1;s<=n.b2;s++)c=t(a,i,s),o=r[c]||0,l+=o,p+=o*(a+.5)*f,v+=o*(i+.5)*f,g+=o*(s+.5)*f;n._avg=l?[~~(p/l),~~(v/l),~~(g/l)]:[~~(f*(n.r1+n.r2+1)/2),~~(f*(n.g1+n.g2+1)/2),~~(f*(n.b1+n.b2+1)/2)]}return n._avg},contains:function(t){var e=this,n=t[0]>>l,r=t[1]>>l,o=t[2]>>l;return n>=e.r1&&n<=e.r2&&r>=e.g1&&r<=e.g2&&o>=e.b1&&o<=e.b2}},o.prototype={push:function(t){this.vboxes.push({vbox:t,color:t.avg()})},palette:function(){return this.vboxes.map(function(t){return t.color})},size:function(){return this.vboxes.size()},map:function(t){for(var e=this.vboxes,n=0;n<e.size();n++)if(e.peek(n).vbox.contains(t))return e.peek(n).color;return this.nearest(t)},nearest:function(t){for(var e,n,r,o=this.vboxes,a=0;a<o.size();a++)((n=Math.sqrt(Math.pow(t[0]-o.peek(a).color[0],2)+Math.pow(t[1]-o.peek(a).color[1],2)+Math.pow(t[2]-o.peek(a).color[2],2)))<e||void 0===e)&&(e=n,r=o.peek(a).color);return r},forcebw:function(){var t=this.vboxes;t.sort(function(t,e){return n.naturalOrder(n.sum(t.color),n.sum(e.color))});var e=t[0].color;e[0]<5&&e[1]<5&&e[2]<5&&(t[0].color=[0,0,0]);var r=t.length-1,o=t[r].color;o[0]>251&&o[1]>251&&o[2]>251&&(t[r].color=[255,255,255])}},{quantize:c}}();return t.prototype.getColor=function(t,e){return this.getPalette(t,5,e)[0]},t.prototype.getPalette=function(t,n,o){(void 0===n||n<2||n>256)&&(n=10),(void 0===o||o<1)&&(o=10);for(var a,i,s,c,u=new e(t),l=u.getImageData(),f=l.data,p=u.getPixelCount(),v=[],g=0;g<p;g+=o)a=4*g,i=f[a+0],s=f[a+1],c=f[a+2],f[a+3]>=125&&(i>250&&s>250&&c>250||v.push([i,s,c]));var h=r.quantize(v,n),y=h?h.palette():null;return u.removeCanvas(),y},t.prototype.getColorFromUrl=function(t,e,n){sourceImage=document.createElement("img");var r=this;sourceImage.addEventListener("load",function(){e(r.getPalette(sourceImage,5,n)[0],t)}),sourceImage.src=t},t.prototype.getImageData=function(t,e){xhr=new XMLHttpRequest,xhr.open("GET",t,!0),xhr.responseType="arraybuffer",xhr.onload=function(t){if(200==this.status){uInt8Array=new Uint8Array(this.response),n=uInt8Array.length,binaryString=new Array(n);for(var n=0;n<uInt8Array.length;n++)binaryString[n]=String.fromCharCode(uInt8Array[n]);data=binaryString.join(""),base64=window.btoa(data),e("data:image/png;base64,"+base64)}},xhr.send()},t.prototype.getColorAsync=function(t,e,n){var r=this;this.getImageData(t,function(t){sourceImage=document.createElement("img"),sourceImage.addEventListener("load",function(){e(r.getPalette(sourceImage,5,n)[0],this)}),sourceImage.src=t})},t}(),g=/\{\{\s*(.*?)\s*\}\}/g,h=r,y=h,d=function(t){return o(t)&&!a(t)},b="function"==typeof Symbol&&Symbol.for,m=b?Symbol.for("react.element"):60103;f.all=function(t,e){if(!Array.isArray(t)||t.length<2)throw new Error("first argument should be an array with at least two elements");return t.reduce(function(t,n){return f(t,n,e)})};var E=f;return p}); |