Solved

256 color palette problem

Posted on 1999-01-20
2
408 Views
Last Modified: 2013-11-20

ok, i have a problem with trying to draw custom colors from a palette on a dc.  The dc is in an overridden and subclassed static box that I'm using as a window in my dialog box to draw in.


I'm creating a palette with 40 entires (shades of blue).

       LPLOGPALETTE lpPal = (LPLOGPALETTE)new BYTE[
sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nNumColors)];

      if (lpPal != NULL)    
      {        
            lpPal->palVersion = 0x300;
            lpPal->palNumEntries = 40;
            PALETTEENTRY *ppe = lpPal->palPalEntry;        
            for (int i=0; i<40-1; i++)
            {
                  lpPal->palPalEntry[i].peRed = 0;
                  lpPal->palPalEntry[i].peGreen = 0;
                  lpPal->palPalEntry[i].peBlue = (255-(i*5));
                  lpPal->palPalEntry[i].peFlags = 0;
            }
            bSucc = m_Pal.CreatePalette(lpPal);        
            delete [](PBYTE)lpPal;
      }


Then in the OnPaint routine, I am first Selecting in the palette to the DC

      CPalette *pPalOld = dc.SelectPalette(&m_Pal, false);

then I am realizing the pallete on the DC.

      dc.RealizePalette();

then i am setting a single pixel to a shade of blue that I have realized in the palette.

int i = 10;
dc.SetPixel( 0, 0, RGB( 0, 0, (255*(i*5)) ) );

then I am checking that pixel to see what color it actually came up with.

COLORREF p;
p = dc.GetPixel( 0, 0 );

now, at this point p should contain rgb values of 0,0,205.  But instead is contains 0,0,128 (a system color).  So what the heck is going on!!!  I know it realized the palette (or at least I think this is what it means) because when I do the following check on the palette, I examine the logpalette it returns and its got all my colors
in there no problem (although its missing all the system colors, not sure why).

CPalette *CTPal = dc.GetCurrentPalette();
tpal = (PALETTEENTRY *)malloc( sizeof(PALETTEENTRY)*256 );
memset( tpal, 0, sizeof(PALETTEENTRY) * 256 );
CTPal->GetPaletteEntries( 0, 256, tpal );
free( tpal );

the return value on GetPaletteEntries is 40 which is the correct number of colors in my custom palette.

so my question is, what the heck is going wrong here!!!!  This is driving me nuts, i've been working on this problem for 2 days now and I can't seem to figure it out.  Any help would be GREATLY appreciated.




note: I also tried using a CPen to draw the dot with.  (insert this code up where the getpixel is).

CPen *tempPen = new CPen( PS_SOLID, 1, RGB( 0, 0, 255-(i*5) ) );
CPen *oldPen = dc.SelectObject( tempPen );
dc.MoveTo(0, 0);
dc.LineTo(0,10);


it should also be noted here that when I run this test in 16 bit mode, the GetPixel comes up with %100 the right value.

0
Comment
Question by:Adept
2 Comments
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 100 total points
ID: 1328051
This code is wrong:

  int i = 10;
  dc.SetPixel( 0, 0, RGB( 0, 0, (255*(i*5)) ) );

it should be
  dc.SetPixel( 0, 0, RGB( 0, 0, (255-(i*5)) ) );

Note the minus sign after the 255.

But you should use SetPixel(0, 0, PALETTERGB(0, 0, 255-(i*5)));


0
 

Author Comment

by:Adept
ID: 1328052

the answer you gave was correct (but I typed that part of my program wrong).  The secondary answer you gave was dead on correct though.  Using PALETTERGB instead of RGB works PERFECTLY.  Why that little nugget of information wasn't in microsofts documentation is beyond me.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
cat dog challenge 18 124
not able to insert into temp table 68 157
Define unique primary key 9 89
modThree challenge 4 77
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now