0

Would anyone know how to specify a ColorMatrix (Specifically a System::Drawing::Imaging::ColorMatrix in C++/CLI) to set an alpha threshold? For example if I were to use 10 (10/255) as my threshold then any pixel with an RGBA Alpha of 10 or less would have 0.0f Alpha and every pixel above would get 1.0f.

I'm trying to implement ColorID picking in a 2D scene editor as I'm sick of using my current unwieldy method of reversing my drawing transformations to determine which pixel of a given bitmap the mouse is pointing at. So what I want to do instead is do a ColorID rendering pass like in OpenGL as described here http://content.gpwiki.org/index.php/OpenGL_Selection_Using_Unique_Color_IDs However I can't just compare the locations of bitmaps onscreen as most of them include tons of white-space that I don't want being picked up by mouse picking which leaves me with color-picking.

For now my ColorMatrix looks like

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 1 0
R G B 0 1

following Hans' answer to GDI+: Set all pixels to given color while retaining existing alpha value but I'd like it to also apply a threshold to the Alpha component (provided that's even possible using a ColorMatrix)

Community
  • 1
  • 1
Stomy
  • 213
  • 1
  • 2
  • 7

1 Answers1

0

Maybe you're looking for the ImageAttributes.SetThreshold method.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Well not particularly, see this only seems to affect the RGB values, whereas I want the threshold to be applied only to Alpha values with the colors just being altogether replaced with a single solid color. I also would prefer it to be in the form of a ColorMatrix rather than an API-provided function, but that relies on a ColorMatrix even being capable of doing Threshold operations. – Stomy Sep 14 '12 at 04:40
  • @Stony: Well, the operation you're asking for is non-linear, so a matrix multiply isn't going to cut it. A matrix multiply followed by clipping just might, but I wasn't able to tell whether there's a clipping step involved with the color transform. – Ben Voigt Sep 14 '12 at 08:07
  • Yeah I had a feeling that was the case, like I said ImageAttributes.SetThreshold would do the trick if it could be used for Alpha but I guess I'm stuck in that regard. Maybe I'll just loop through each pixel in a texture myself before drawing and set the Alpha Threshold manually, then use the ColorMatrix to set the bitmap's flat color like I already do. It'll be ugly and slow but it should do the trick – Stomy Sep 24 '12 at 07:36