Q: how to match colors?


I draw a polygon using a color value {1.f, 1.f, 1.f, 0.1f}. The color is black but the 4th value 0.1f makes it very light (translucent effects). I have to use Microsoft GDI to write some text on the polygon and wish to match the text’s background color and the polygon’s color. I try RGB(255/10, 255/10, 255/10) because RGB(255, 255, 255) is black and the translucent factor is 0.1. But I can still see the color difference. Do you have a solution? I want to make it automatic because users may choose any polygon color he/she wants. Please help. Thanks.



Actually 1.0, 1.0, 1.0 is white 0.0, 0.0, 0.0 is black. And RGB(0,0,0) is black, RGB(255,255,255) is white. I don’t quite understand what the description of your problem is so I can’t tell if this is the root of your problem or not.

It seems that your polygon is transparent and your text is not. You’ll see a difference between the two if you’re drawing them over anything but black (or white in this case).

Deiussum and MV,

Thanks for the correction. I was wrong in describing the black color value (but was right in programming). The pologon is drawn in a color value {1.f, 1.f, 1.f, .1f}. It is transparent and looks very light gray. When I use Mircosoft GDI to write text on the top of the pologon, I wish the text’s backgound has the same color and transparency setting as the polygon. If I can do this, the text’s backgound will “disappear” (user will see the text and the polygon without noticing the text’s background). Would it be possible? Your further help is much appreciated.


I think I’m understandding your problem a bit better now. You are using the GDI TextOut function to write your text? I think most people here would consider it bad to try and mix GDI functions with OpenGL.

That being said, I don’t think you are calculating the background color correctly. The polygon you are drawing is being blended with what is currently in the scene if I understand you correctly. That doesn’t mean that the actual color of the polygon will be ra, ga, b*a, as you are trying to use for the GDI color. What blending function are you using to do the transparency? That will affect what the resulting color will be.

Also the color of things already on the screen will affect the resulting color, so using the GDI, it probably won’t be possible to get exactly what you want. (i.e. when the polygon moves in front of another object of a different color, the border of the text will be visible because you won’t be able to change it on a pixel-by-pixel scale.)

Now if you are using a black background, with nothing else in the scene and the polygon is blended using glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) then your calculation should be essentially correct. Any difference you see might be simply the result of some sort of incompatibility in the colors used by the GDI and OpenGL.

You might want to consider using textured quads for your text instead. You could then use an actual alpha value and get the results you want, much easier.

If I understand that correctly you simply want the GDI text background to be transparent, showing what is underneath, and only the letters to be opaque.

Ripped from MS help:

int SetBkMode(
HDC hdc, // handle to DC
int iBkMode // background mode
[in] Handle to the device context.
[in] Specifies the background mode. This parameter can be one of the following values.
Value Description
OPAQUE Background is filled with the current background color before the text, hatched brush, or pen is drawn.
TRANSPARENT Background remains untouched.

Thanks for your posts, Deiussum and Relic.

By using Relic’s method, I soloved the problem. I can make text’s background transparent. But when I use GDI to draw a rectangular around the text, the problem comes back.

Deiussum, you are right. It’s a bad idea to mix OpenGL and GDI. But I have to provide different text fonts. GDI can directly use windows stock fonts. From my understanding, OpenGL cannot. Am I right? I don’t know if textured quads can help it or not.



Maybe opengl cant use the fonts, but you can. You could create a texture with all the characters just use textout on a bitmap and upload it as a texture, or you could get the font outline and create a mesh for each character.

If you are willing to spend time working on it, you can use OpenGL to make a texture of any Windows font available. Its not that hard either. You just have to watch out for the details concerning nonproportional fonts.

I would go for outline any day. Much higher quality and more efficient with memory, but it ups the poly counts a bit, which, for some, would not be very nice. Just don’t use too much outlined text and you’ll be fine.