Skip to content

Commit 3d4d499

Browse files
committed
replace colorthief with rgbaster
1 parent 251fe79 commit 3d4d499

File tree

3 files changed

+14
-27
lines changed

3 files changed

+14
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ $('img.my-image').on('ab-color-found', function(payload){
6262

6363
Caveats
6464
--------------
65-
This plugin utlizes [Color Thief](http://lokeshdhakar.com/projects/color-thief/) which in turn uses the Canvas. The script will silently fail if one tries to extract the colors from an image not hosted in the current domain.
65+
This plugin utlizes the `<canvas>` element and the `ImageData` object, and due to cross-site security limitations, the script will silently fail if one tries to extract the colors from an image not hosted on the current domain. This plugin is built on top of a script called [RGBaster](https://github.com/briangonzalez/rgbaster.js).
6666

6767
Author
6868
-------

dist/jquery.adaptive-background.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/jquery.adaptive-background.js

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
;(function($){
55

6-
/* Constants. */
6+
/* Constants & defaults. */
77
var DATA_COLOR = 'data-ab-color';
88
var DATA_PARENT = 'data-ab-parent';
99
var EVENT_CF = 'ab-color-found';
@@ -13,20 +13,18 @@
1313
parent: null,
1414
};
1515

16-
/* Create a ColorThief object, which will
17-
do the heavy-lifting.
18-
https://github.com/lokesh/color-thief
19-
*/
20-
var ColorThief = includeColorThief();
21-
var ct = new ColorThief();
22-
23-
/* Helper function for comparring */
16+
/* Helper function for detecting remote images. */
2417
var isRemoteImageURL = function(url){
2518
var parser = document.createElement('a');
2619
parser.href = url;
2720
return !(parser.hostname === location.hostname && parser.protocol === location.protocol && parser.port === location.port);
2821
};
2922

23+
// Include RGBaster - https://github.com/briangonzalez/rgbaster.js
24+
/* jshint ignore:start */
25+
(function(n,t){var a=function(){return document.createElement("canvas").getContext("2d")};var e=function(n){var t=new Image;t.src=n.src;var e=a();e.drawImage(n,0,0);var r=e.getImageData(0,0,n.width,n.height);return r.data};var r=function(n){return["rgb(",n,")"].join("")};var o=function(n){return n.map(function(n){return r(n.name)})};var i=5;var u=10;var c={};c.colors=function(n){var t=e(n),a=t.length,c=0,m=-(i-1);var l={},v="",f=[],d={dominant:{name:"",count:0},palette:Array.apply(null,Array(u)).map(Boolean).map(function(n){return{name:"0,0,0",count:0}})};while((m+=i*4)<a){++c;f[0]=t[m];f[1]=t[m+1];f[2]=t[m+2];v=f.join(",");if(v in l){l[v]=l[v]+1}else{l[v]=1}if(v!=="0,0,0"){var p=l[v];if(p>d.dominant.count){d.dominant.name=v;d.dominant.count=p}else{d.palette.some(function(n){if(p>n.count){n.name=v;n.count=p;return true}})}}}return{dominant:r(d.dominant.name),palette:o(d.palette)}};n.RGBaster=n.RGBaster||c})(window);
26+
/* jshint ignore:end */
27+
3028
/* Our main function declaration. */
3129
$.adaptiveBackground = {
3230
run: function( options ){
@@ -46,13 +44,10 @@
4644

4745
/* Small helper function which applies colors, attrs, and triggers events. */
4846
var handleColors = function(){
49-
var arrayToRGB = function(a){ return "rgb(" + a.join(',') + ")"; };
50-
var color = arrayToRGB( ct.getColor( $this[0] ) );
51-
var palette = ct.getPalette( $this[0] )
52-
.map(function(a){ return arrayToRGB(a); });
53-
54-
$this.attr(DATA_COLOR, color);
55-
$this.trigger(EVENT_CF, { color: color, palette: palette });
47+
var colors = RGBaster.colors($this[0]);
48+
49+
$this.attr(DATA_COLOR, colors.dominant);
50+
$this.trigger(EVENT_CF, { color: colors.dominant, palette: colors.palette });
5651
};
5752

5853
/* Subscribe to our color-found event. */
@@ -78,12 +73,4 @@
7873
},
7974
};
8075

81-
/* This little helper includes ColorThief for us. */
82-
/* jshint ignore:start */
83-
function includeColorThief(){
84-
var CanvasImage=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)};CanvasImage.prototype.clear=function(){this.context.clearRect(0,0,this.width,this.height)},CanvasImage.prototype.update=function(t){this.context.putImageData(t,0,0)},CanvasImage.prototype.getPixelCount=function(){return this.width*this.height},CanvasImage.prototype.getImageData=function(){return this.context.getImageData(0,0,this.width,this.height)},CanvasImage.prototype.removeCanvas=function(){this.canvas.parentNode.removeChild(this.canvas)};var ColorThief=function(){};if(ColorThief.prototype.getColor=function(t,r){var n=this.getPalette(t,5,r),o=n[0];return o},ColorThief.prototype.getPalette=function(t,r,n){"undefined"==typeof r&&(r=10),"undefined"==typeof n&&(n=10);for(var o,e,a,i,u,c=new CanvasImage(t),f=c.getImageData(),s=f.data,v=c.getPixelCount(),h=[],p=0;v>p;p+=n)o=4*p,e=s[o+0],a=s[o+1],i=s[o+2],u=s[o+3],u>=125&&(e>250&&a>250&&i>250||h.push([e,a,i]));var g=MMCQ.quantize(h,r),l=g.palette();return c.removeCanvas(),l},!pv)var pv={map:function(t,r){var n={};return r?t.map(function(t,o){return n.index=o,r.call(n,t)}):t.slice()},naturalOrder:function(t,r){return r>t?-1:t>r?1:0},sum:function(t,r){var n={};return t.reduce(r?function(t,o,e){return n.index=e,t+r.call(n,o)}:function(t,r){return t+r},0)},max:function(t,r){return Math.max.apply(null,r?pv.map(t,r):t)}};var MMCQ=function(){function t(t,r,n){return(t<<2*c)+(r<<c)+n}function r(t){function r(){n.sort(t),o=!0}var n=[],o=!1;return{push:function(t){n.push(t),o=!1},peek:function(t){return o||r(),void 0===t&&(t=n.length-1),n[t]},pop:function(){return o||r(),n.pop()},size:function(){return n.length},map:function(t){return n.map(t)},debug:function(){return o||r(),n}}}function n(t,r,n,o,e,a,i){var u=this;u.r1=t,u.r2=r,u.g1=n,u.g2=o,u.b1=e,u.b2=a,u.histo=i}function o(){this.vboxes=new r(function(t,r){return pv.naturalOrder(t.vbox.count()*t.vbox.volume(),r.vbox.count()*r.vbox.volume())})}function e(r){var n,o,e,a,i=1<<3*c,u=new Array(i);return r.forEach(function(r){o=r[0]>>f,e=r[1]>>f,a=r[2]>>f,n=t(o,e,a),u[n]=(u[n]||0)+1}),u}function a(t,r){var o,e,a,i=1e6,u=0,c=1e6,s=0,v=1e6,h=0;return t.forEach(function(t){o=t[0]>>f,e=t[1]>>f,a=t[2]>>f,i>o?i=o:o>u&&(u=o),c>e?c=e:e>s&&(s=e),v>a?v=a:a>h&&(h=a)}),new n(i,u,c,s,v,h,r)}function i(r,n){function o(t){var r,o,e,a,i,u=t+"1",f=t+"2",s=0;for(c=n[u];c<=n[f];c++)if(g[c]>p/2){for(e=n.copy(),a=n.copy(),r=c-n[u],o=n[f]-c,i=o>=r?Math.min(n[f]-1,~~(c+o/2)):Math.max(n[u],~~(c-1-r/2));!g[i];)i++;for(s=l[i];!s&&g[i-1];)s=l[--i];return e[f]=i,a[u]=e[f]+1,[e,a]}}if(n.count()){var e=n.r2-n.r1+1,a=n.g2-n.g1+1,i=n.b2-n.b1+1,u=pv.max([e,a,i]);if(1==n.count())return[n.copy()];var c,f,s,v,h,p=0,g=[],l=[];if(u==e)for(c=n.r1;c<=n.r2;c++){for(v=0,f=n.g1;f<=n.g2;f++)for(s=n.b1;s<=n.b2;s++)h=t(c,f,s),v+=r[h]||0;p+=v,g[c]=p}else if(u==a)for(c=n.g1;c<=n.g2;c++){for(v=0,f=n.r1;f<=n.r2;f++)for(s=n.b1;s<=n.b2;s++)h=t(f,c,s),v+=r[h]||0;p+=v,g[c]=p}else for(c=n.b1;c<=n.b2;c++){for(v=0,f=n.r1;f<=n.r2;f++)for(s=n.g1;s<=n.g2;s++)h=t(f,s,c),v+=r[h]||0;p+=v,g[c]=p}return g.forEach(function(t,r){l[r]=p-t}),u==e?o("r"):u==a?o("g"):o("b")}}function u(t,n){function u(t,r){for(var n,o=1,e=0;s>e;)if(n=t.pop(),n.count()){var a=i(c,n),u=a[0],f=a[1];if(!u)return;if(t.push(u),f&&(t.push(f),o++),o>=r)return;if(e++>s)return}else t.push(n),e++}if(!t.length||2>n||n>256)return!1;var c=e(t),f=0;c.forEach(function(){f++});var h=a(t,c),p=new r(function(t,r){return pv.naturalOrder(t.count(),r.count())});p.push(h),u(p,v*n);for(var g=new r(function(t,r){return pv.naturalOrder(t.count()*t.volume(),r.count()*r.volume())});p.size();)g.push(p.pop());u(g,n-g.size());for(var l=new o;g.size();)l.push(g.pop());return l}var c=5,f=8-c,s=1e3,v=.75;return n.prototype={volume:function(t){var r=this;return(!r._volume||t)&&(r._volume=(r.r2-r.r1+1)*(r.g2-r.g1+1)*(r.b2-r.b1+1)),r._volume},count:function(r){var n=this,o=n.histo;if(!n._count_set||r){var e,a,i,u=0;for(e=n.r1;e<=n.r2;e++)for(a=n.g1;a<=n.g2;a++)for(i=n.b1;i<=n.b2;i++)index=t(e,a,i),u+=o[index]||0;n._count=u,n._count_set=!0}return n._count},copy:function(){var t=this;return new n(t.r1,t.r2,t.g1,t.g2,t.b1,t.b2,t.histo)},avg:function(r){var n=this,o=n.histo;if(!n._avg||r){var e,a,i,u,f,s=0,v=1<<8-c,h=0,p=0,g=0;for(a=n.r1;a<=n.r2;a++)for(i=n.g1;i<=n.g2;i++)for(u=n.b1;u<=n.b2;u++)f=t(a,i,u),e=o[f]||0,s+=e,h+=e*(a+.5)*v,p+=e*(i+.5)*v,g+=e*(u+.5)*v;n._avg=s?[~~(h/s),~~(p/s),~~(g/s)]:[~~(v*(n.r1+n.r2+1)/2),~~(v*(n.g1+n.g2+1)/2),~~(v*(n.b1+n.b2+1)/2)]}return n._avg},contains:function(t){var r=this,n=t[0]>>f;return gval=t[1]>>f,bval=t[2]>>f,n>=r.r1&&n<=r.r2&&gval>=r.g1&&gval<=r.g2&&bval>=r.b1&&bval<=r.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 r=this.vboxes,n=0;n<r.size();n++)if(r.peek(n).vbox.contains(t))return r.peek(n).color;return this.nearest(t)},nearest:function(t){for(var r,n,o,e=this.vboxes,a=0;a<e.size();a++)n=Math.sqrt(Math.pow(t[0]-e.peek(a).color[0],2)+Math.pow(t[1]-e.peek(a).color[1],2)+Math.pow(t[2]-e.peek(a).color[2],2)),(r>n||void 0===r)&&(r=n,o=e.peek(a).color);return o},forcebw:function(){var t=this.vboxes;t.sort(function(t,r){return pv.naturalOrder(pv.sum(t.color),pv.sum(r.color))});var r=t[0].color;r[0]<5&&r[1]<5&&r[2]<5&&(t[0].color=[0,0,0]);var n=t.length-1,o=t[n].color;o[0]>251&&o[1]>251&&o[2]>251&&(t[n].color=[255,255,255])}},{quantize:u}}();
85-
return ColorThief;
86-
};
87-
/* jshint ignore:end */
88-
8976
})(jQuery);

0 commit comments

Comments
 (0)