views:

227

answers:

4

What diffrent approaches exists out there in actionscript 3.0 ? I found this library: mojocolors But this is more for graphics-color I guess…

Thx

+1  A: 

In general, assuming that you start with an RGB image, you convert it to HSI color space and use the I (intensity) component.

Dima
+1  A: 

To convert an image to grayscale, you need to iterate over each pixel in the image buffer and average the R, G and B component into one entity, then duplicate it three times to get the new color. pseudo-code (assuming 8bit color):

for each pixel in buffer: 
    pixel.rgb = ((pixel.red + pixel.green + pixel.blue) / 3) * 0x010101;

I am sure you can do something with Adobe's PixelBender to achieve this faster.

LiraNuna
Alternatively, intensity is sometimes defined as max(r, g, b).
Dima
+1  A: 
var n:Number = 1/3;
var matrix:Array = [n,n,n,0,0,
                    n,n,n,0,0,
                    n,n,n,0,0, 
                    0,0,0,1,0];

var cmf:ColorMatrixFilter = new ColorMatrixFilter(matrix);
bitmap.filters = [cmf];
alecmce
Isn't LopSae's exactly the same as mine, except that it tweaks luminosity values? And why can't I comment on LopSae's answer? Very odd...For what each value in the matrix does, see:http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/filters/ColorMatrixFilter.html
alecmce
@LopSae - I'm new to StackOverflow, haven't fully worked out all its idiosyncracies thanks! ;) The difference you see may be very subtle depending upon the source image. If you're using a test image with relatively bright, bold colours then the different values will have less of a visible effect than an image with subtler shades...
alecmce
The maths of it aren't too hard... for each pixel, it will take the red, green and blue components of the pixel colour and multiply them by the first second and third element in the matrix (in my case 1/3, in your case 0.2,0.7,0.1), add them together and put that result as the red-value. Then, it takes the same components of the pixel colour and multiply them by the sixth, seventh and eighth elements in the matrix, and them and set that as green. Similar for the blue with the 11th, 12th and 13th. This produces greyscale because in the result parts for the red, green and blue are the same.
alecmce
I just noticed though that you say if you use my example and set n = 1 you don't see any difference? You certainly should. The resultant image will be mostly white, I expect, because the sum of the three component parts of the colour must be less than 255 for the colour to be anything other than white (which means that it has to start out pretty dark). Your values should make greens appear brighter and reds and blues more subdued btw, I was talking nonsense in the last sentence of the second comment above.
alecmce
A: 

Apply the filter created by this method to the DisplayObject that contains the image you want in black and white:

public static function createBlackAndWhiteFilter():ColorMatrixFilter {
 var rLum:Number = 0.2225;
 var gLum:Number = 0.7169;
 var bLum:Number = 0.0606;

 var bwMatrix:Array = [rLum, gLum, bLum, 0, 0, rLum, gLum, bLum, 0, 0, rLum, gLum, bLum, 0, 0, 0, 0, 0, 1, 0];  
 return new ColorMatrixFilter(bwMatrix);
}//createBlackAndWhiteFilter
LopSae
Even if I don't understand it completely, I will try this ! But:For what do the specific values stand for, and why are there exactly 20 values necessary in the array ?Maybe I should not ask to much and just use it… :-J
algro
The 20 values in the array are to make a 5x4 matrix, which actually is almost the same as the one posted in this same question by alecmce, just written it in a line. The different values are related (as far as I know) to the different light intensity of each component color. So you reduce the intensity of each color in different measures so that each becomes a shade of gray.
LopSae
@alecmce: We can’t comment on each other’s answers because we don’t have yet 50 reputation yet (that limits us to comments on our own answers only). And yes, is mostly the same answer, which I figured out just a while ago. Sorry about that. What I found really confusing, is that using your matrix with 1/3, with 1, and my matrix produces exactly the same effect, no different shades, no different intensities... so the actual value really matters for something?
LopSae