├── LICENCE ├── README.rst ├── example.html ├── greyScale.js ├── greyScale.min.js └── image.jpg /LICENCE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011 Andrew Pryde 2 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 3 | software and associated documentation files (the "Software"), to deal in the Software 4 | without restriction, including without limitation the rights to use, copy, modify, merge, 5 | publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons 6 | to whom the Software is furnished to do so, subject to the following conditions: 7 | 8 | The above copyright notice and this permission notice shall be included in all copies or 9 | substantial portions of the Software. 10 | 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 12 | BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 13 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 14 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 15 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 16 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | DEPRECIATED 2 | ----------- 3 | This plugin is no longer under development the following CSS (in modern browsers) fulfils the same purpose with far greater efficiency. 4 | 5 | .. code:: css 6 | 7 | img { 8 | filter: grayscale(100%); 9 | } 10 | ================ 11 | jQuery GreyScale 12 | ================ 13 | A jQuery plugin for greyscaleing images on the fly and then colouring them on hover. 14 | 15 | See example.html for a usage. 16 | 17 | Tested in IE6-9, Safari, Chrome and FireFox. 18 | 19 | Please send bug reports and feature requests to andrew@pryde-design.co.uk or log them in the issue queue on github (https://github.com/Prydie/jQuery-GreyScale-Plugin/issues). 20 | 21 | Pull requests are welcome. 22 | 23 | Rewrite 24 | ======= 25 | The rewrite branch is a full rewrite of the plugin which aims to solve isues with the changes to jQuery since release as well as exposing the internals of the plugin to enable use with your own event handlers. 26 | -------------------------------------------------------------------------------- /example.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | jQuery GreyScale Plugin 6 | 7 | 8 | 9 | 15 | 16 | 17 | Image 18 | 19 | 20 | Image 21 | 22 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /greyScale.js: -------------------------------------------------------------------------------- 1 | /* 2 | * jQuery $.greyScale Plugin v0.2 3 | * Written by Andrew Pryde (www.pryde-design.co.uk) 4 | * Date: Mon 1 Aug 2011 5 | * Licence: MIT Licence 6 | * 7 | * Copyright (c) 2011 Andrew Pryde 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this 9 | * software and associated documentation files (the "Software"), to deal in the Software 10 | * without restriction, including without limitation the rights to use, copy, modify, merge, 11 | * publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons 12 | * to whom the Software is furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in all copies or 15 | * substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 18 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 20 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | */ 23 | 24 | (function($){ 25 | 26 | $.fn.greyScale = function(args) { 27 | $options = $.extend({ 28 | fadeTime: $.fx.speeds._default, 29 | reverse: false 30 | }, args); 31 | function greyScale(image, width, height) { 32 | can = $('') 33 | .css({ 34 | 'display' : 'none', 35 | 'left' : '0', 36 | 'position' : 'absolute', 37 | 'top' : '0' 38 | }) 39 | .attr({ 40 | 'width': width, 41 | 'height': height 42 | }) 43 | .addClass('gsCanvas'); 44 | ctx = can[0].getContext('2d'); 45 | ctx.drawImage(image, 0, 0, width, height); 46 | 47 | imageData = ctx.getImageData(0, 0, width, height); 48 | px = imageData.data; 49 | for (i = 0; i < px.length; i+= 4) { 50 | grey = px[i] * .3 + px[i+1] * .59 + px[i+2] * .11; 51 | px[i] = px[i+1] = px[i+2] = grey; 52 | } 53 | ctx.putImageData(imageData, 0, 0); 54 | return can; 55 | } 56 | if (navigator.userAgent.match(/msie/i) && navigator.userAgent.match(/6/)) { 57 | // IE doesn't support Canvas so use it's horrible filter syntax instead 58 | this.each(function(){ 59 | var greyscale = $options.reverse ? 0 : 1; 60 | $(this).css({ 61 | 'filter': 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=' + greyscale + ')', 62 | 'zoom': '1' 63 | }); 64 | $(this).hover(function() { 65 | var greyscale = $options.reverse ? 1 : 0; 66 | $(this).css({ 67 | 'filter': 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=' + greyscale + ')' 68 | }); 69 | }, function() { 70 | var greyscale = $options.reverse ? 0 : 1; 71 | $(this).css('filter', 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=' + greyscale + ')'); 72 | }); 73 | }); 74 | } else { 75 | this.each(function(index) { 76 | $(this).wrap('
'); 77 | gsWrapper = $(this).parent(); 78 | gsWrapper.css({ 79 | 'position' : 'relative', 80 | 'display' : 'inline-block' 81 | }); 82 | if (window.location.hostname !== this.src.split('/')[2]) { 83 | // If the image is on a different domain proxy the request 84 | $.getImageData({ 85 | url: $(this).attr('src'), 86 | success: $.proxy(function(image) { 87 | can = greyScale(image, image.width, image.height); 88 | if ($options.reverse) { can.appendTo(gsWrapper).css({"display" : "block", "opacity" : "0"}); } 89 | else { can.appendTo(gsWrapper).fadeIn($options.fadeTime); } 90 | }, gsWrapper), 91 | error: function(xhr, text_status){ 92 | // silently fail on error 93 | } 94 | }); 95 | } else { // If the image is on the same domain don't proxy the request 96 | can = greyScale($(this)[0], $(this).width(), $(this).height()); 97 | if ($options.reverse) { can.appendTo(gsWrapper).css({"display" : "block", "opacity" : "0"}); } 98 | else { can.appendTo(gsWrapper).fadeIn($options.fadeTime); } 99 | } 100 | }); 101 | 102 | $(this).parent().delegate('.gsCanvas', 'mouseover mouseout', function(event) { 103 | over = $options.reverse ? 1 : 0; 104 | out = $options.reverse ? 0 : 1; 105 | (event.type == 'mouseover') && $(this).stop().animate({'opacity': over}, $options.fadeTime); 106 | (event.type == 'mouseout') && $(this).stop().animate({'opacity': out}, $options.fadeTime); 107 | }); 108 | } 109 | }; 110 | })( jQuery ); 111 | 112 | /* 113 | * 114 | * jQuery $.getImageData Plugin 0.3 115 | * http://www.maxnov.com/getimagedata 116 | * 117 | * Written by Max Novakovic (http://www.maxnov.com/) 118 | * Date: Thu Jan 13 2011 119 | * 120 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 121 | * 122 | * Includes jQuery JSONP Core Plugin 2.1.4 123 | * http://code.google.com/p/jquery-jsonp/ 124 | * Copyright 2010, Julian Aubourg 125 | * Released under the MIT License. 126 | * 127 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 128 | * 129 | * Copyright 2011, Max Novakovic 130 | * Dual licensed under the MIT or GPL Version 2 licenses. 131 | * http://www.maxnov.com/getimagedata/#license 132 | * 133 | */ 134 | (function(c,g){function n(){}function o(a){s=[a]}function e(a,j,k){return a&&a.apply(j.context||j,k)}function h(a){function j(b){!l++&&g(function(){p();q&&(t[d]={s:[b]});z&&(b=z.apply(a,[b]));e(a.success,a,[b,A]);e(B,a,[a,A])},0)}function k(b){!l++&&g(function(){p();q&&b!=C&&(t[d]=b);e(a.error,a,[a,b]);e(B,a,[a,b])},0)}a=c.extend({},D,a);var B=a.complete,z=a.dataFilter,E=a.callbackParameter,F=a.callback,R=a.cache,q=a.pageCache,G=a.charset,d=a.url,f=a.data,H=a.timeout,r,l=0,p=n;a.abort=function(){!l++&& 135 | p()};if(e(a.beforeSend,a,[a])===false||l)return a;d=d||u;f=f?typeof f=="string"?f:c.param(f,a.traditional):u;d+=f?(/\?/.test(d)?"&":"?")+f:u;E&&(d+=(/\?/.test(d)?"&":"?")+encodeURIComponent(E)+"=?");!R&&!q&&(d+=(/\?/.test(d)?"&":"?")+"_"+(new Date).getTime()+"=");d=d.replace(/=\?(&|$)/,"="+F+"$1");q&&(r=t[d])?r.s?j(r.s[0]):k(r):g(function(b,m,v){if(!l){v=H>0&&g(function(){k(C)},H);p=function(){v&&clearTimeout(v);b[I]=b[w]=b[J]=b[x]=null;i[K](b);m&&i[K](m)};window[F]=o;b=c(L)[0];b.id=M+S++;if(G)b[T]= 136 | G;var O=function(y){(b[w]||n)();y=s;s=undefined;y?j(y[0]):k(N)};if(P.msie){b.event=w;b.htmlFor=b.id;b[I]=function(){/loaded|complete/.test(b.readyState)&&O()}}else{b[x]=b[J]=O;P.opera?(m=c(L)[0]).text="jQuery('#"+b.id+"')[0]."+x+"()":b[Q]=Q}b.src=d;i.insertBefore(b,i.firstChild);m&&i.insertBefore(m,i.firstChild)}},0);return a}var Q="async",T="charset",u="",N="error",M="_jqjsp",w="onclick",x="on"+N,J="onload",I="onreadystatechange",K="removeChild",L="