<?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>Flex Examples &#187; ColorMatrixFilter</title>
	<atom:link href="http://blog.flexexamples.com/category/colormatrixfilter/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.flexexamples.com</link>
	<description>Just a bunch of Adobe Flex Examples</description>
	<lastBuildDate>Wed, 26 Jan 2011 18:09:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Converting an image to black and white using the ColorMatrixFilter</title>
		<link>http://blog.flexexamples.com/2007/07/27/converting-an-image-to-black-and-white-using-the-colormatrixfilter/</link>
		<comments>http://blog.flexexamples.com/2007/07/27/converting-an-image-to-black-and-white-using-the-colormatrixfilter/#comments</comments>
		<pubDate>Fri, 27 Jul 2007 20:18:26 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[ColorMatrixFilter]]></category>
		<category><![CDATA[Embed]]></category>
		<category><![CDATA[Image]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2007/07/27/converting-an-image-to-black-and-white-using-the-colormatrixfilter/</guid>
		<description><![CDATA[<p>A quick example of converting an image to black and white using the ColorMatrixFilter class. This was originally written for the Flash documentation&#8217;s &#8220;Learning ActionScript 2.0&#8243; book (<a href="http://livedocs.adobe.com/flash/9.0/main/00000956.html" title="Flash CS3 LiveDocs: Learning ActionScript 2.0: 'Adding color and brightness effects with code'.">click here</a>), but I ported our sample to Flex below.</p> <p>For more information on [...]]]></description>
			<content:encoded><![CDATA[<p>A quick example of converting an image to black and white using the ColorMatrixFilter class. This was originally written for the Flash documentation&#8217;s &#8220;Learning ActionScript 2.0&#8243; book (<a href="http://livedocs.adobe.com/flash/9.0/main/00000956.html" title="Flash CS3 LiveDocs: Learning ActionScript 2.0: 'Adding color and brightness effects with code'.">click here</a>), but I ported our sample to Flex below.</p>
<p>For more information on the ColorMatrixFilter in ActionScript 3.0, check out the Flex 3 Beta documentation on LiveDocs at: <a href="http://livedocs.adobe.com/flex/3/langref/flash/filters/ColorMatrixFilter.html" title="ColorMatrixFilter - Flex 3 Language Reference">ColorMatrixFilter &#8211; Flex 3 Language Reference</a>.</p>
<p><span id="more-34"></span></p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/BlackAndWhite_test/main.mxml">View MXML</a></p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!-- http://blog.flexexamples.com/2007/07/27/converting-an-image-to-black-and-white-using-the-colormatrixfilter/ --&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal" backgroundColor="white"&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            private var rLum:Number = 0.2225;
            private var gLum:Number = 0.7169;
            private var bLum:Number = 0.0606;

            [Bindable]
            private var bwMatrix:Array = [rLum, gLum, bLum, 0, 0,
                            rLum, gLum, bLum, 0, 0,
                            rLum, gLum, bLum, 0, 0,
                            0, 0, 0, 1, 0];

            [Bindable]
            [Embed('assets/image2.jpg')]
            private var image2:Class;

        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:ColorMatrixFilter id="cmf" matrix="{bwMatrix}" /&gt;

    &lt;mx:VBox&gt;
        &lt;mx:Label text="Black and white" /&gt;
        &lt;mx:Image source="{image2}" filters="{[cmf]}" scaleX="0.5" scaleY="0.5" /&gt;
    &lt;/mx:VBox&gt;

    &lt;mx:VBox&gt;
        &lt;mx:Label text="Original" /&gt;
        &lt;mx:Image source="{image2}" scaleX="0.5" scaleY="0.5" /&gt;
    &lt;/mx:VBox&gt;

&lt;/mx:Application&gt;
</pre>
<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/BlackAndWhite_test/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/BlackAndWhite_test/bin/main.html" width="100%" height="250"></iframe></p>
<p class="new"><strong>8/27/2007</strong>: You can also code similar to the following to modify the Bitmap data directly. This allows you to create two icons (one color, one black and white) rather than have to specify the ColorMatrixFilter filter on each Image tag separately.</p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/BlackAndWhite_test_2/main.mxml">View MXML</a></p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!-- http://blog.flexexamples.com/2007/07/27/converting-an-image-to-black-and-white-using-the-colormatrixfilter/ --&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="horizontal"
        backgroundColor="white"
        creationComplete="init()"&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            private const rLum:Number = 0.2225;
            private const gLum:Number = 0.7169;
            private const bLum:Number = 0.0606;

            private const bwMatrix:Array = [rLum, gLum, bLum, 0, 0,
                            rLum, gLum, bLum, 0, 0,
                            rLum, gLum, bLum, 0, 0,
                            0, 0, 0, 1, 0];

            [Bindable]
            [Embed("assets/image2.jpg")]
            private var image2:Class;

            private var image2Color:Bitmap;
            private var image2BlackWhite:Bitmap;

            private function init():void {
                image2Color = new image2();
                image2BlackWhite = new image2();
                image2BlackWhite.filters = [cmf];

                img1.source = image2BlackWhite;
                img2.source = image2Color;
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:ColorMatrixFilter id="cmf" matrix="{bwMatrix}" /&gt;

    &lt;mx:VBox&gt;
        &lt;mx:Label text="Black and white" /&gt;
        &lt;mx:Image id="img1" scaleX="0.5" scaleY="0.5" /&gt;
    &lt;/mx:VBox&gt;

    &lt;mx:VBox&gt;
        &lt;mx:Label text="Original" /&gt;
        &lt;mx:Image id="img2" scaleX="0.5" scaleY="0.5" /&gt;
    &lt;/mx:VBox&gt;

&lt;/mx:Application&gt;
</pre>
<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/BlackAndWhite_test_2/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/BlackAndWhite_test_2/bin/main.html" width="100%" height="250"></iframe></p>
<p class="new"><strong>9/26/2007</strong>: I recently learnt this trick where you can use the flash.* classes directly in your MXML by adding the namespace to the XML node or main &lt;mx:Application /&gt; tag. For more information, see &#8220;<a href="http://blog.flexexamples.com/2007/09/26/using-the-flash-classes-in-mxml/">Using the flash.* classes in MXML</a>&#8220;.</p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"&gt;

    &lt;mx:Number id="rLum"&gt;0.2225&lt;/mx:Number&gt;
    &lt;mx:Number id="gLum"&gt;0.7169&lt;/mx:Number&gt;
    &lt;mx:Number id="bLum"&gt;0.0606&lt;/mx:Number&gt;

    &lt;flash.filters:ColorMatrixFilter
            xmlns:flash.filters="flash.filters.*"
            id="matrix"
            matrix="{[rLum, gLum, bLum, 0, 0,
                      rLum, gLum, bLum, 0, 0,
                      rLum, gLum, bLum, 0, 0,
                      0, 0, 0, 1, 0]}" /&gt;

    &lt;mx:Image id="image"
        source="http://www.helpexamples.com/flash/images/image1.jpg"
        filters="{[matrix]}"
        scaleX="0.5"
        scaleY="0.5" /&gt;

&lt;/mx:Application&gt;
</pre>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Converting an image to black and white using the ColorMatrixFilter on FlexExamples.com',url: 'http://blog.flexexamples.com/2007/07/27/converting-an-image-to-black-and-white-using-the-colormatrixfilter/',contentID: 'post-34',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: '',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2007/07/27/converting-an-image-to-black-and-white-using-the-colormatrixfilter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

