Advanced color mixing with GDI +

Using GDI + with Windows Forms, I want to draw with a pen and combine color based on the color of the destination pixel.

For example, if I draw a line and it passes through black pixels, I want it to be a lighter color (for example, white) so that it is visible. When the same line passes over white pixels, it should be darker in color (for example, black) so that it is still clearly visible.

Is there any way to do this using GDI +?

+7
source share
3 answers

As suggested by Hans Passant, you can paint using what is currently on the canvas as an image for a texture brush (you may need double buffering to work correctly) and use ColorMatrix to change the colors painted on the canvas.

There is a color matrix that inverts colors similar to XOR, the problem is that it will not work with medium gray. A color matrix that inverts RGB and leaves alpha unchanged will be:

 -1, 0, 0, 0, 0 0,-1, 0, 0, 0 0, 0,-1, 0, 0 0, 0, 0, 1, 0 1, 1, 1, 0, 1 

Something similar, albeit slower, would be to copy the canvas onto the image and process that image pixel into a pixel with rules such as if the color were brighter than 0.5, make it a little darker, make it a little brighter. Then you draw the processed image as a texture. This will give a better result, but it will be significantly slower than using ColorMatrix .

+3
source

You can try XORing pen color. Paint.NET does this with a selection frame to make it visible on any color.

+1
source

Oh, I don’t think it is too complicated. You can create a pen that automatically changes colors based on where it is. Just read the pixel where the pen’s location is located (see Example), get the Alpha component and set the pen color to black or white if it’s more or less than 255/2, respectively :)

0
source

All Articles