Actionscript: ColorMatrixFilter for Photoshop's brightness adjust?

436 Views Asked by At

I have a B/W picture and I need to make some adjustments to it - I need to increase it's 'visibility' (it is too gray, I need to make it darker). Does anybody know how to reproduce Photoshop's brightness adjust in AS3? Please note that this is NOT the same as adjusting the brightness in Flash. The difference is:

  • in PS: brightness adjusts only the pixels that have a color different to white. It does not do anything to white pixels, so white pixels actually stay white
  • in PS: lightness adjusts all the pixels, so it affects the white pixels as well. Bringing lightness down makes all the image darker; this is unusable for me and this is exactly what Flash does as well (although it is called 'brightness' there)

I could reproduce Flash's brightness with this matrix:

var m:Array = new Array();
m = m.concat([1, 0, 0, 0, value]);  // red
m = m.concat([0, 1, 0, 0, value]);  // green
m = m.concat([0, 0, 1, 0, value]);  // blue
m = m.concat([0, 0, 0, 1, 0]);      // alpha

new ColorMatrixFilter(m);

...however this is exactly what doesn't work well as it sets all the image darker, including the white parts.

Any ideas how to reproduce PS's brigthness setting? Or any other matrix that actually keeps white/light pixels light while darkening the darker ones? Thank you!

1

There are 1 best solutions below

0
On

I doubt this replicates Photoshop's "brightness" exactly (it's more like adjusting the contrast), but you could try scaling the RGB values, then adjusting them so that whites remain white:

var scaling:Number = 4;
var adjustment:Number = 255 * (1 - scaling);

var m:Array = new Array();
m = m.concat([scaling, 0, 0, 0, adjustment]);  // red
m = m.concat([0, scaling, 0, 0, adjustment]);  // green
m = m.concat([0, 0, scaling, 0, adjustment]);  // blue
m = m.concat([0, 0, 0, 1, 0]);      // alpha

new ColorMatrixFilter(m);

If you wanted a more traditional contrast, the adjustment would be:

var value:Number = 255 * (1 - scaling) * 0.5;