Hi,
How to change colors in a surface ?
supose that I have a surface that has a png of a character
and I want to change a color RGB(169,80,0) to RGB (250,0,0)
it didn't work for me , I kept testing I found that
when the bitmap is loaded into the surface the color changes
from RGB(169,80,0) to RGB(168,80,0)
can you please clarify this ? I don't get it
I want to test for certain colors & change them in real time
if (pColor = RGB (169,80,0))
backbuffer.SetPixel (x,y,RGB (250,0,0))
Thanks in advance
What you are experiencing is the limitation of 16-bit colors. There are operations in CGapiSurface called ColorrefToNative and NativeToColorref you could use to get the correct color codes for your pixels...
Johan Thanks a lot it worked !!
One additional question
Does these functions (ColorrefToNative - NativeToColorref) take care about the pixel format 555 & 565
or should I test for them and deal with each format ?
if yes could you guide me how ?
Thanks a lot
Yes, they do!
The fastest way to do all this is to use GetBuffer.
(1) Get the native color
Calculate this using:
DWORD mycol;
pSurface->ColorrefToNative(RGB(168, 0, 0), &mycol);
(2) Replace pixels
Use GetBuffer() to get raw access to the surface. You can then use
pBuf = pBuffer + x*xPitch + y*yPitch;
if (pBuf == mycol)
dosomething
GetBuffer is done with a minimum of overhead...
hi Johan,
Thanks for your reply and concern.
it didn't work when I tried to use the getbuffer()
- Code:
GDBUFFERDESC buffer;
sSurface.GetBuffer(&buffer;);
long xPitch = buffer.xPitch >> 1;
long yPitch = buffer.yPitch >> 1;
DWORD pBuf;
pBuf = (DWORD ) buffer.pBuffer + (x*xPitch) + (y*yPitch);
if (pBuf == mycol)
sSurface.SetPixel (x,y,RGB(255,255,255));
sSurface.ReleaseBuffer();
although this code worked
- Code:
DWORD pNative;
DWORD pNative2;
COLORREF pColor;
COLORREF pColor2 = RGB (201, 0,201);
sSurface.GetPixel (x,y,&pColor;);
sSurface.ColorrefToNative(pColor ,&pNative;);
sSurface.ColorrefToNative(pColor2,&pNative2;);
if (pNative == pNative2)
sSurface.SetPixel (x,y,RGB (230,125,45));
any suggestions ?

It's not clear from your non-working example what type mycol is - if it's a COLORREF then you're comparing a 16-bit color against a 24-bit color, which won't work correctly. And even if it is a 16-bit color, you need to understand what's going on.
Converting a 24-bit color (COLORREF) to a 16-bit native color is a lossy operation - you're throwing away some of the bits - so you have to be careful when doing comparisons. Comparing a 24-bit color to something that has been converted from 24-bit to 16-bit and back to 24-bit will rarely work.
For example, let's look at the 8-bit red color channel in your sample color:
201 = 0xC9 = 11001001 (binary)
When converted to a native 16-bit color this channel gets squashed down t 5 pixels, taking the most significant bits:
11001
Now if you convert this color back to a 24-bit COLORREF the channel gets expanded with zeros:
11001000 = 0xC8 = 200
So if you ever try and extract a COLORREF from a surface and compare it with your source color, they simply won't match. 200 != 201
In the example you have that works both colors - the pixel color and the test color - are both converted to native 16-bit colors and compared directly. This is the correct way to compare colors. (You wouldn't want to put this code into a loop, though - calling GetPixel is doing a Native --> COLORREF and then you're doing the reverse.)
In the example you have that doesn't work, it's not clear what type mycol is. If mycol is a COLORREF then you're comparing a COLORREF to a 16-bit Native color, which will not work as expected. You need to use the ColorrefToNative() function on mycol
InexorableTash , Thanks for replying,
Sorry I forgot the first 2 line, the mycol is DWORD
I did like johan wrote but I don't know what I'm doing wrong.
- Code:
DWORD mycol;
sSurface.ColorrefToNative(RGB(169, 0, 0), &mycol;);
GDBUFFERDESC buffer;
sSurface.GetBuffer(&buffer;);
long xPitch = buffer.xPitch >> 1;
long yPitch = buffer.yPitch >> 1;
DWORD pBuf;
pBuf = (DWORD ) buffer.pBuffer + (x*xPitch) + (y*yPitch);
if (pBuf == mycol)
sSurface.SetPixel (x,y,RGB(255,255,255));
sSurface.ReleaseBuffer();
Ah, you're mixing up pointers with values. This should work:
- Code:
DWORD mycol;
sSurface.ColorrefToNative(RGB(169, 0, 0), &mycol);
GDBUFFERDESC buffer;
sSurface.GetBuffer(&buffer);
long xPitch = buffer.xPitch >> 1;
long yPitch = buffer.yPitch >> 1;
// the "p" prefix indicates you want this to be a pointer
// ("Hungarian notation") but you're just using a value!
// DWORD pBuf;
// pBuf = (DWORD ) buffer.pBuffer + (x*xPitch) + (y*yPitch);
//
// Let's break down what this line is doing:
// Set the value of pBuf (a DWORD) to:
// the value of buffer.pBuffer (a void*) cast to a DWORD value
// plus the x offset, plus the y offset
//
// That's not what you want - can you see why?
// Here's what I think you mean:
DWORD dwNativeColor; // Store the color value we'll look up in the buffer
WORD* pPixel; // A pointer to the pixel we're looking up (a 16-bit WORD)
// Compute the offset to the pixel
pPixel = ((WORD*)buffer.pBuffer) + (x*xPitch) + (y*yPitch);
// Look up the value of the pixel - a WORD - by de-referenceing the pointer, then cast to DWORD
dwNativeColor = (DWORD) (*pPixel);
//if (pBuf == mycol)
if( dwNativeColor == mycol )
sSurface.SetPixel (x,y,RGB(255,255,255));
// And it'll be faster if you modify the buffer directly:
//
// Do this outside the loop:
//
// DWORD dwNewColor;
// sSurface.ColorrefToNative(RGB(255,255,255), &dwNewColor);
//
// Then inside the loop:
//
// *pPixel = dwNewColor;
sSurface.ReleaseBuffer();
Hungarian notation looks messy, especially if you have a background in Java or another strongly typed language. But when you're programming in C/C++ it's worth using (at least the basics) so you can keep track of types - is this a pointer (pSomething) or a value (dwValue, wPixel, etc)
[/code]
InexorableTash, Thanks for helping me out
It worked !!