この前紹介したAdobe AIRマルチメディアアプリケーションツールリスト(写真とビデオなど)のような画像/写真の編集ツールに対して、カラーの画像をグレースケール(gray scale)に変換するのは、基本な機能の一つだと思います。Flex/ActionScriptで実現するのは難しいですか?実は簡単です。以下のような一つの関数で変換できます。
※解説:グレースケール【gray scale】とは、コンピュータ上での色の表現方法の一つ。画像を白から黒までの明暗だけで表現し、色の情報は含まない「モノクロ」のこと。灰色を何階調で表現するかをビット数によって表す。1ビットの場合は白と黒のみで中間色がない状態で、8ビットなら(白と黒を含めて)256階調、16ビットなら65536階調の灰色で表現する。
・以下は画像の色をグレースケール(gray scale)に変換する関数のソースコードです。
- private function convertToGrayscale(image:DisplayObject):void
- {
- var matrix:Array = [0.3, 0.59, 0.11, 0, 0,
- 0.3, 0.59, 0.11, 0, 0,
- 0.3, 0.59, 0.11, 0, 0,
- 0, 0, 0, 1, 0];
- var grayscaleFilter:ColorMatrixFilter = new ColorMatrixFilter(matrix);
- image.filters = [grayscaleFilter];
- }
MXMLで同じの機能も実現できます。
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
- <mx:Image id="photo1" source="@Embed('mountains.jpg')">
- <mx:filters>
- <mx:ColorMatrixFilter matrix="[0.3, 0.59, 0.11, 0, 0, 0.3, 0.59, 0.11, 0, 0, 0.3, 0.59, 0.11, 0, 0, 0, 0, 0, 1, 0]“
- />
- </mx:filters>
- </mx:Image>
- <mx:Image id="photo2" source="@Embed('mountains.jpg')">
- <mx:filters>
- <mx:ColorMatrixFilter matrix="[0.33, 0.33, 0.33, 0, 0, 0.33, 0.33, 0.33, 0, 0, 0.33, 0.33, 0.33, 0, 0, 0, 0, 0, 1, 0]“/>
- </mx:filters>
- </mx:Image>
- </mx:Application>
メインコンテンツEND ■
Posted on Monday, 2nd February 2009 by admin
Tags: ActionScript, カラー, グレースケール, 関数
Posted in ActionScript | Comments (3) | 15,320 views

(1 votes, average: 4.00 out of 5)
September 2nd, 2009 at 1:05 pm
[...] MXMLのでもColorMatrixFilterが使えるみたい ActionScript/MXMLで画像の色(カラー)をグレースケール(gray scale)に変換する関数 [...]
August 30th, 2010 at 7:51 pm
this is a laugh for you from me
Love may be blind but marriage is a real eye-opener.
September 1st, 2010 at 12:27 pm
This made me smile and hopefully after your last post it will do the same for you:
Good judgment comes from bad experience and a lot of that comes from bad judgment.