<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ajaxBlender.com&#8212; A Whole Lot of AJAX! Directory for Web 2.0 AJAX scripts, demos and tutorials. Custom AJAX Coding Service for Your Web 2.0 Website.</title>
	<atom:link href="http://www.ajaxblender.com/tag/beginner-guide/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ajaxblender.com</link>
	<description>A Whole Lot of AJAX!</description>
	<lastBuildDate>Mon, 02 May 2011 07:38:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Convert Images to Grayscale</title>
		<link>http://www.ajaxblender.com/howto-convert-image-to-grayscale-using-javascript.html</link>
		<comments>http://www.ajaxblender.com/howto-convert-image-to-grayscale-using-javascript.html#comments</comments>
		<pubDate>Mon, 07 Dec 2009 15:33:03 +0000</pubDate>
		<dc:creator>ajaxBlender.com</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[beginner guide]]></category>
		<category><![CDATA[desaturate]]></category>
		<category><![CDATA[grayscale]]></category>
		<category><![CDATA[image effects]]></category>

		<guid isPermaLink="false">http://www.ajaxblender.com/?p=1113</guid>
		<description><![CDATA[Read this tutorial to find out how to desaturate an image (convert color images to grayscale) using JavaScript. A cross-browser code sample is supplied.
Recently we published a tutorial explaining how to rotate images using JavaScript. To continue this series of tutorials on image manipulation using JavaScript, we would like to explain how to change color [...]]]></description>
			<content:encoded><![CDATA[<p>Read this tutorial to find out how to desaturate an image (convert color images to grayscale) using JavaScript. A cross-browser code sample is supplied.<span id="more-1113"></span><br />
Recently we published a tutorial explaining <a title="How to Rotate Images using JavaScript" href="http://www.ajaxblender.com/howto-rotate-image-using-javascript-canvas.html" target="_blank">how to rotate images using JavaScript</a>. To continue this series of tutorials on image manipulation using JavaScript, we would like to explain how to change color images to grayscale using JavaScript.</p>
<p>This is a relatively simple task, but there are two methods (one method for Internet Explorer and one method for all other browsers).</p>
<h3>Canvas Method</h3>
<p>As you probably know, screen color consists of 3 components: red, green and blue. Each component or color (for example, red) has a value from 0 to 255. The value 0 indicates there is no red color and the value 255 would be the brightest possible red color. To convert a color to grayscale you just need to calculate the average for all three components. This can be done using the simple formula below:</p>
<pre class="php">
grayscalecolor <span class="phpOperator">=</span> <span class="phpOperator">(</span>red <span class="phpOperator">+</span> green <span class="phpOperator">+</span> blue<span class="phpOperator">)</span> / 3;
</pre>
<p>All modern browsers support the CANVAS tag which is allowed access to the image’s pixels. The code below will go through each pixel in the image and replace the color values with an average value. Here is the code:</p>
<pre class="php">
<span class="phpKeyword">
var </span>canvas <span class="phpOperator">=</span> document.createElement<span class="phpOperator">(</span><span class="phpString">'canvas'</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpKeyword">
var </span>canvasContext <span class="phpOperator">=</span> canvas<span class="phpOperator">.</span>getContext<span class="phpOperator">(</span><span class="phpString">'2d'</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpKeyword">
var </span>imgW <span class="phpOperator">=</span> imgObj<span class="phpOperator">.</span>width<span class="phpText">;</span>
<span class="phpKeyword">
var </span>imgH <span class="phpOperator">=</span> imgObj<span class="phpOperator">.</span>height;
canvas<span class="phpOperator">.</span>width <span class="phpOperator">=</span> imgW<span class="phpText">;</span>
canvas<span class="phpOperator">.</span>height <span class="phpOperator">=</span> imgH<span class="phpText">;</span>
canvasContext.drawImage<span class="phpOperator">(</span>imgObj, <span class="phpNumber">0</span>, <span class="phpNumber">0</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpKeyword">
var </span>imgPixels <span class="phpOperator">=</span> canvasContext.getImageData<span class="phpOperator">(</span><span class="phpNumber">0</span>, <span class="phpNumber">0</span>, imgW, imgH<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpKeyword">
for<span class="phpOperator">(</span></span>>var y <span class="phpOperator">=</span> 0; y <span class="phpOperator">&lt;</span> imgPixels<span class="phpOperator">.</span>height; y<span class="phpOperator"><span class="phpOperator">+</span><span class="phpOperator">+</span></span><span class="phpOperator">)</span><span class="phpOperator">{</span>
    <span class="phpKeyword"> for<span class="phpOperator">(</span></span>>var x <span class="phpOperator">=</span> 0; x <span class="phpOperator">&lt;</span> imgPixels<span class="phpOperator">.</span>width<span class="phpText">;</span> x<span class="phpOperator"><span class="phpOperator">+</span><span class="phpOperator">+</span></span><span class="phpOperator">)</span><span class="phpOperator">{</span>
         <span class="phpKeyword"> var </span>i <span class="phpOperator">=</span> <span class="phpOperator">(</span>y * <span class="phpNumber">4</span><span class="phpOperator">)</span> * imgPixels<span class="phpOperator">.</span>width <span class="phpOperator">+</span> x * <span class="phpNumber">4</span><span class="phpText">;</span>
         <span class="phpKeyword"> var </span>avg <span class="phpOperator">=</span> <span class="phpOperator">(</span>imgPixels<span class="phpOperator">.</span>data<span class="phpOperator">[</span>i<span class="phpOperator">]</span> <span class="phpOperator">+</span> imgPixels<span class="phpOperator">.</span>data<span class="phpOperator">[</span>i <span class="phpOperator">+</span> <span class="phpNumber">1</span><span class="phpOperator">]</span> <span class="phpOperator">+</span> imgPixels<span class="phpOperator">.</span>data<span class="phpOperator">[</span>i <span class="phpOperator">+</span> <span class="phpNumber">2</span><span class="phpOperator">]</span><span class="phpOperator">)</span> / 3;
          imgPixels<span class="phpOperator">.</span>data<span class="phpOperator">[</span>i<span class="phpOperator">]</span> <span class="phpOperator">=</span> avg;
          imgPixels<span class="phpOperator">.</span>data<span class="phpOperator">[</span>i <span class="phpOperator">+</span> <span class="phpNumber">1</span><span class="phpOperator">]</span> <span class="phpOperator">=</span> avg;
          imgPixels<span class="phpOperator">.</span>data<span class="phpOperator">[</span>i <span class="phpOperator">+</span> <span class="phpNumber">2</span><span class="phpOperator">]</span> <span class="phpOperator">=</span> avg;
     <span class="phpOperator">}</span>
<span class="phpOperator">}</span>
canvasContext.putImageData<span class="phpOperator">(</span>imgPixels, <span class="phpNumber">0</span>, <span class="phpNumber">0</span>, <span class="phpNumber">0</span>, <span class="phpNumber">0</span>, imgPixels<span class="phpOperator">.</span>width, imgPixels<span class="phpOperator">.</span>height<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpKeyword">
return </span>canvas<span class="phpOperator">.</span>toDataURL<span class="phpOperator">(</span><span class="phpOperator">)</span><span class="phpText">;</span>
</pre>
<p>From the above code sample, you see that we created a canvas, loaded the image into it, and then changed the color values of each pixel in the image to an average value. After that we simply replaced the SRC of our image to the image changed in the canvas.</p>
<p>Using the same method you can also flip images using JavaScript. (We will create a tutorial for this in the future).</p>
<h3>MSIE (Internet Explorer) method</h3>
<p>Microsoft Internet Explorer does not support the CANVAS tag, but Microsoft does provide image manipulation through filters. The filter to use for converting images to grayscale is the filter named <strong>DXImageTransform.Microsoft.BasicImage</strong>. Therefore, we simply apply this filter to our image, and it will work in all versions of MSIE (starting from version 6).</p>
<pre class="php">
imgObj<span class="phpOperator">.</span>style.filter <span class="phpOperator">=</span> <span class="phpString">'progid<span class="phpOperator">:</span>DXImageTransform<span class="phpOperator">.</span>Microsoft.BasicImage<span class="phpOperator">(</span>grayScale=<span class="phpNumber">1</span><span class="phpOperator">)</span>'</span><span class="phpText">;</span>
</pre>
<p>That&#8217;s it ;) Click the <strong>Demo</strong> to see it in action.</p>
<img src="http://www.ajaxblender.com/?ak_action=api_record_view&id=1113&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.ajaxblender.com/howto-convert-image-to-grayscale-using-javascript.html/feed</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
	</channel>
</rss>

