Skip to content

Instantly share code, notes, and snippets.

@SilencerWeb
Created December 4, 2017 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SilencerWeb/5e194117d5ccd0a14a13ce5de5b03fd4 to your computer and use it in GitHub Desktop.
Save SilencerWeb/5e194117d5ccd0a14a13ce5de5b03fd4 to your computer and use it in GitHub Desktop.
var img = new Image();
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
img.src = imageUrl;
img.onload = function () {
canvas.width = img.width;
canvas.height = 100;
ctx.drawImage(img, 0, 0);
img.style.display = 'none';
var result = 0,
mediumValue = 0,
bright,
pixelCount = 0;
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var data = imageData.data;
for (var i = 0; i < data.length; i += 4 * 10) {
mediumValue = (data[ i ] + data[ i + 1 ] + data[ i + 2 ]) / 3;
result += mediumValue;
pixelCount++;
}
bright = result / pixelCount;
if (bright > 200) {
$('header').addClass('header_gray');
} else {
if ($('header').hasClass('header_gray')) {
$('header').removeClass('header_gray');
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment