-
-
Save qodesmith/beba97d13a9ab81360ed39598c9fc37c to your computer and use it in GitHub Desktop.
Revisions
-
qodesmith revised this gist
Jul 26, 2016 . 1 changed file with 11 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ function avgColor(imageUrl) { var canvas = document.createElement('canvas'); var context = canvas.getContext && canvas.getContext('2d'); var rgb = {r: 102, g: 102, b: 102}; // Set a base colour as a fallback for non-compliant browsers var pixelInterval = 5; // Rather than inspect every single pixel in the image inspect every 5th pixel var count = 0; var i = -4; @@ -10,7 +10,7 @@ function avgColor (imageUrl) { var length; var width; var height; img.setAttribute('src', imageUrl); // return the base colour for non-compliant browsers @@ -24,7 +24,7 @@ function avgColor (imageUrl) { try { data = context.getImageData(0, 0, width, height); } catch (e) { // catch errors - usually due to cross domain security issues alert(e); return rgb; @@ -35,14 +35,14 @@ function avgColor (imageUrl) { while ((i += pixelInterval * 4) < length) { count++; rgb.r += data[i]; rgb.g += data[i + 1]; rgb.b += data[i + 2]; } // floor the average values to give correct rgb values (ie: round number values) rgb.r = Math.floor(rgb.r / count); rgb.g = Math.floor(rgb.g / count); rgb.b = Math.floor(rgb.b / count); return rgb; } -
qodesmith revised this gist
Jul 26, 2016 . 1 changed file with 6 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,22 +1,24 @@ function avgColor (imageUrl) { var canvas = document.createElement('canvas'); var context = canvas.getContext && canvas.getContext('2d'); var rgb = {r:102, g:102, b:102}; // Set a base colour as a fallback for non-compliant browsers var pixelInterval = 5; // Rather than inspect every single pixel in the image inspect every 5th pixel var count = 0; var i = -4; var img = document.createElement('img'); var data; var length; var width; var height; img.setAttribute('src', imageUrl); // return the base colour for non-compliant browsers if (!context) return rgb; // set the height and width of the canvas element to that of the image height = canvas.height = img.naturalHeight || img.offsetHeight || img.height; width = canvas.width = img.naturalWidth || img.offsetWidth || img.width; context.drawImage(img, 0, 0); -
qodesmith renamed this gist
Jul 26, 2016 . 1 changed file with 12 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,15 @@ function avgColor (imageUrl) { var canvas = document.createElement('canvas'); var context = canvas.getContext && canvas.getContext('2d'); var rgb = {r:102,g:102,b:102}; // Set a base colour as a fallback for non-compliant browsers var pixelInterval = 5; // Rather than inspect every single pixel in the image inspect every 5th pixel var count = 0; var i = -4; var img = document.createElement('img'); var data; var length; img.setAttribute('src', imageUrl); // return the base colour for non-compliant browsers if (!context) { return rgb; } -
olvado revised this gist
Jun 27, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ function getAverageColourAsRGB (img) { data, length; // return the base colour for non-compliant browsers if (!context) { return rgb; } // set the height and width of the canvas element to that of the image var height = canvas.height = img.naturalHeight || img.offsetHeight || img.height, -
olvado revised this gist
Jun 27, 2011 . 1 changed file with 7 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,14 +1,16 @@ function getAverageColourAsRGB (img) { var canvas = document.createElement('canvas'), context = canvas.getContext && canvas.getContext('2d'), rgb = {r:102,g:102,b:102}, // Set a base colour as a fallback for non-compliant browsers pixelInterval = 5, // Rather than inspect every single pixel in the image inspect every 5th pixel count = 0, i = -4, data, length; // return the base colour for non-compliant browsers if (!context) { alert('Your browser does not support CANVAS'); return rgb; } // set the height and width of the canvas element to that of the image var height = canvas.height = img.naturalHeight || img.offsetHeight || img.height, width = canvas.width = img.naturalWidth || img.offsetWidth || img.width; @@ -17,6 +19,7 @@ function getAverageColourAsRGB (img) { try { data = context.getImageData(0, 0, width, height); } catch(e) { // catch errors - usually due to cross domain security issues alert(e); return rgb; } @@ -29,6 +32,8 @@ function getAverageColourAsRGB (img) { rgb.g += data[i+1]; rgb.b += data[i+2]; } // floor the average values to give correct rgb values (ie: round number values) rgb.r = Math.floor(rgb.r/count); rgb.g = Math.floor(rgb.g/count); rgb.b = Math.floor(rgb.b/count); -
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ function getAverageColourAsRGB (img) { var canvas = document.createElement('canvas'), context = canvas.getContext && canvas.getContext('2d'), rgb = {r:102,g:102,b:102}, pixelInterval = 5, count = 0, i = -4, data, length; if (!context) { alert('Your browser does not support CANVAS'); return rgb; } var height = canvas.height = img.naturalHeight || img.offsetHeight || img.height, width = canvas.width = img.naturalWidth || img.offsetWidth || img.width; context.drawImage(img, 0, 0); try { data = context.getImageData(0, 0, width, height); } catch(e) { alert(e); return rgb; } data = data.data; length = data.length; while ((i += pixelInterval * 4) < length) { count++; rgb.r += data[i]; rgb.g += data[i+1]; rgb.b += data[i+2]; } rgb.r = Math.floor(rgb.r/count); rgb.g = Math.floor(rgb.g/count); rgb.b = Math.floor(rgb.b/count); return rgb; }