
Untitled
By: a guest on
May 2nd, 2012 | syntax:
JavaScript | size: 1.12 KB | hits: 14 | expires: Never
// ==UserScript==
// @id 2
// @name 2
// @version 1.0
// @namespace
// @author
// @description
// @include *.jpg
// @include *.png
// @run-at window-load
var img=document.getElementsByTagName('img')[0];
var can=document.createElement('canvas');
var w=img.width;
var h=img.height;
can.width=img.width;
can.height=img.height;
img.removeAttribute('width');
img.removeAttribute('height');
var ctx=can.getContext('2d');
ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, w, h)
var data=ctx.getImageData(0,0,can.width,can.height);
img.width=w;
img.height=h;
var sum=0;
var num_transparent=0;
for(var i=0;i<data.data.length;i+=4)
{
if(data.data[i+3]>0)
{
sum+=(data.data[i]+data.data[i+1]+data.data[i+2])*data.data[i+3]/255;
}
else
{
num_transparent++;
}
}
sum/=(data.data.length/4-num_transparent);
//alert(sum);
if(sum<128)
document.body.setAttribute('style', "background: #fff;");
else
document.body.setAttribute('style', "background: #000;");
//document.body.appendChild(can);