• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

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_pBrush->CreateSolidBrush(clrCurrent);
    m_pPen->CreatePen(PS_SOLID, 1, clrCurrent);
    pOldBrush = pDC->SelectObject(m_pBrush);
    pOldPen = pDC->SelectObject(m_pPen);
    pDC->Ellipse(rcCurrentRect);
    pDC->SelectObject(pOldBrush);
    pDC->SelectObject(pOldPen);
    m_pBrush.DeleteObject();
    m_pPen.DeleteObject();      
    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.
0
hhan
Asked:
hhan
1 Solution
 
hhanAuthor Commented:
Edited text of question
0
 
hhanAuthor Commented:
Edited text of question
0
 
dr_funfrockCommented:
 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 :)

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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

Thank you anyway. :-)


0
 
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 http://www.codeguru.com/bitmap/bitmap_toc.shtml for a bunch of articles and code for palette.


0
 
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.  

0
 
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.

0
 
hhanAuthor Commented:
So, is there anyway that, from an ActiveX control, to change the color palette and use it for drawing???  
0
 
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.

0
 
shan1Commented:

Use PALETTERGB(r, g, b)
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now