User:Inductiveload/ImageGreyscaler.js

// ImageGreyscaler.js // User:Inductiveload // April 2012

// Greyscaler function: // Copyright (c) 2011 Daniel Westendorf // // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

(function( $ ){

$.fn.greyscaleImage = function(options) { var settings = { 'fadeOnHover': true, 'fadeInSpeed': 250, 'fadeOutSpeed': 250 };   if ( options ) { $.extend( settings, options ) };   function makeGreyScale(src) { var canvas = document.createElement('canvas'); var ctx = canvas.getContext('2d'); var imgObj = new Image; imgObj.src = src;

canvas.width = imgObj.width; canvas.height = imgObj.height; ctx.drawImage(imgObj, 0, 0); var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);

for(var y = 0; y < imgPixels.height; y++){ for(var x = 0; x < imgPixels.width; x++){ var i = (y * 4) * imgPixels.width + x * 4; var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3; imgPixels.data[i] = avg; imgPixels.data[i + 1] = avg; imgPixels.data[i + 2] = avg; }       }        ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height); return canvas.toDataURL; }   this.each(function {        var el = $(this);        el.css({"position":"absolute"}).wrap("").clone.addClass('img_greyscale').css({"position":"absolute","z-index":"2","opacity":"1"}).insertBefore(el).queue(function{ var el = $(this); el.parent.css({"width":this.width,"height":this.height}); el.dequeue; });       this.src = makeGreyScale(this.src);        el.parent.find('.img_greyscale').animate({'opacity': 0}, settings['fadeOutSpeed']);        if (settings['fadeOnHover']){             el.parent.find('.img_greyscale').mouseenter(function{ $(this).parent.find('img:first').stop.animate({opacity:1}, settings['fadeInSpeed']); }).mouseleave(function{ $(this).stop.animate({opacity:0}, settings['fadeOutSpeed']); });       };    });  } })( jQuery );

ImageGreyscaler = function{ this.addToolbarButton; }

ImageGreyscaler.prototype.addToolbarButton = function{ var myself = this; var img = $(' ').attr({                'width':22,                 'height':22,                 'border':0,                 'className':"wpGreyscaleImage",                'comment': "Convert image to greyscale",                'alt':"Convert to greyscale",                'src': "//upload.wikimedia.org/wikipedia/commons/thumb/f/f9/Button_greyscale_page.svg/23px-Button_greyscale_page.svg.png"}) .css('cursor', 'pointer') .click( function{myself.PerformGreyscaleImage}); $('#toolbar').append(img); return; }

ImageGreyscaler.prototype.PerformGreyscaleImage = function{ $('#ProofReadImage').greyscaleImage({}); }

$(document).ready( function{   if (wgNamespaceNumber == 104){		var ImageGreyscalerInstance = new ImageGreyscaler;    } })