Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advanced color blending with GDI+

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

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

Is there any way to do this with GDI+?

like image 850
Trevor Elliott Avatar asked Dec 21 '12 21:12

Trevor Elliott


2 Answers

As Hans Passant proposed, you could paint using what's currently in the canvas as the image for a texture brush (you might need double buffering for that to work correctly) and use a ColorMatrix to modify the colors being painted on the canvas.

There is a color matrix that inverts the colors similar to a XOR, the problem is it won't work with the middle gray. A color matrix that inverts RGB and leaves alpha intact would 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 to an image and process that image pixel per pixel with rules such as if the color is brighter than 0.5, make it slightly darker else, make it slightly brighter. Then, you paint with that processed image as a texture brush. This would give a better result but it would be significantly slower than using a ColorMatrix.

like image 107
Coincoin Avatar answered Sep 20 '22 03:09

Coincoin


You could try XORing the pen color. Paint.NET does this with the selection border to make it visible on any color.

like image 45
Peter W. Avatar answered Sep 21 '22 03:09

Peter W.