Palette Problem? Color Gradient Looks Bad in 256C Mode

Currently, I am working on a project that requires me to draw a gradient-colored ellipse.  The input is the base color of the ellipse.  The logic is something like the following code fragment.

for (i=0; i<nGradient; i++)
    clrCurrent = m_clrGradient[i];
    m_pPen->CreatePen(PS_SOLID, 1, clrCurrent);
    pOldBrush = pDC->SelectObject(m_pBrush);
    pOldPen = pDC->SelectObject(m_pPen);
    rcCurrentRect.DeflateRect(CSize(x, y));

If the display mode was in 16-bit or 24-bit, the result looks great.  However, if it was in 8-bit (256 color), the result looks ugly.

I thought it might be the palette problem.  So, I tried to create a new palette and stick in all of the colors in m_clrGradient[].  Then selected the palette into the DC and had the DC to "Realize" it OK.  But, I still get the same ugly result.  

Basically, I have no anyother idea.  Is there any one who knows why?  

Thanks a lot.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hhanAuthor Commented:
Edited text of question
hhanAuthor Commented:
Edited text of question
 Your image might have more than 236 colors in it. If it does then windows tries to map the extra colors to a 'best' match.

 If you have less than 236 colors there may be other images in your app that have different colors. These colors ocmbined with your image may add up to more than 236 colors again causing a 'best' match.

 If you weant your image to look perfect you will have to lower the number of colors .. via photoshop or image composer

Bummer, I know :)

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

hhanAuthor Commented:
Actually, I only have 32 levels of gradient.  Besides, it is not an image.  It is drawn dynamically.

Thank you anyway. :-)

Tommy HuiEngineerCommented:
Not only do you need to create a palette and select it into a DC, but you also need to handle the messages WM_QUERYNEWPALETTE, WM_PALETTECHANGED, and WM_PALETTEISCHANGING at your frame window level. One thing to look at is at for a bunch of articles and code for palette.

hhanAuthor Commented:
Actually, it is an ActiveX control.  As you suggested, I have created handlers for each of the messages you mentioned above (using Class Wizard).  However, none of them has ever been reached at run time.

Can you give me more details how do I capture those messages in an ActiveX control (using MFC) and what should I do within those message handlers.

By the way, I have just realized that dithered colors are used to display the gradient.  I could see the Windows uses a mixture of dots for a given RGB color.  

Tommy HuiEngineerCommented:
If this is an activeX control, it will not work because those messages are only sent to the top level windows. This will be the main window for the application. Since you do not have any control over the main window, your control will not ever get those messages.

hhanAuthor Commented:
So, is there anyway that, from an ActiveX control, to change the color palette and use it for drawing???  
hhanAuthor Commented:
Thank you anyway.  I have just figured out the problem was the m_clrGradient[] stored the RGB from the RGB macro.  Instead of using RGB, I changed it to be PALETTERGB and it worked.


Use PALETTERGB(r, g, b)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.