Click here to Skip to main content
15,885,985 members
Articles / Desktop Programming / MFC
Tip/Trick

The Photoshop-Like Color Palette Dialog

Rate me:
Please Sign up or sign in to vote.
4.25/5 (3 votes)
17 Apr 2012CPOL 12.6K   969   10  
A Photoshop-Like Color Palette Dialog in MFC

Introduction 

I developed a Photoshop-like color palette. It support  RGB color space, HSB color space, CMYK color space and Lab color space. The dialog is shown below:

Image 1

Background

The palette dialog is developed under MFC framework.  

Using the code

The palette dialog is named CSelectColorDialogDlg. Its base class is CDialog. This dialog has only two parameters: the input color and the father dialog pointer (optional). The input color parameter is a reference type which also gives the output color when the dialog closed.  The input color type is COLORREF

The illustration of my dialog.

C++
//
//How to use the dialog
//
COLORREF rgb = RGB(250,70,0);
CSelectColorDialogDlg dlg(rgb);
if (dlg.DoModal() == IDOK)
{
   COLORREF output = rgb;
}

//
//The main process is the transform between different color spaces
//
class CSelectColorDialogDlg : public CDialog
{
...
    //The transform functions
    COLORREF getColorFromRGB(struct rgbcolor rgb);
    COLORREF getColorFromCMYK(struct cmykcolor cmyk);
    COLORREF getColorFromHSB(struct hsbcolor hsb);
    COLORREF getColorFromLab(struct labcolor lab);

    struct rgbcolor getRGBFromColor(COLORREF color);
    struct cmykcolor getCMYKFromColor(COLORREF color);
    struct hsbcolor getHSBFromColor(COLORREF color);
    struct labcolor getLabFromColor(COLORREF color);
    //Get the color form coordinate  or get coordinate from color;
    void getCoordinateFromColor(COLORREF color,ColorType type,SelectWeight selectw);
    COLORREF getColorFromCoordinate(int x,int y,int w,ColorType type,SelectWeight selectw);
...
}

//
//draw the color palette on the dialog
//
void CSelectColorDialogDlg::drawColorPalette(){
    CDC* pDC  = GetDC();

    getCoordinateFromColor(currentColor,curType,curSelectWeight);

    for(int i = 0; i < 256; ++i){
        for(int j = 0; j < 256; ++j){
            COLORREF color = getColorFromOrdinate(i,j,curWeight,curType,curSelectWeight);
            memdc.SetPixel(poffsetx + i,poffsety + j,   color   ); 
        }
    }

    for(int i = 0; i < 25; ++i){
        for(int j = 0; j < 256; ++j){
            
            COLORREF color;
            if (curSelectWeight == H1 && curType == HSBTYPE){
                color = getColorFromCoordinate(255,255,j,curType,curSelectWeight);
            }else{
                color = getColorFromCoordinate(curX,curY,j,curType,curSelectWeight);
            }
            memdc.SetPixel(boffsetx + i,boffsety + j,color); 
        }
    }


    for(int i = 0; i < 65; ++i){
        for(int j = 0; j < 30; ++j){
            memdc.SetPixel(roffsetx + i,roffsety + j,currentColor); 
            memdc.SetPixel(roffsetx + i,roffsety + j + 30,inputColor); 
        }
    }
    pDC->BitBlt(0,0,350,400,&memdc,0,0,SRCCOPY);
    pDC->BitBlt(roffsetx,roffsety,65,60,&memdc,roffsetx,roffsety,SRCCOPY);


    CBrush *pbrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
    pDC->SelectObject(pbrush);
    pDC->RoundRect(poffsetx + curX - 6, poffsety + curY - 6,
        poffsetx + curX + 6,poffsety + curY + 6,12,12);

    POINT   labelPoints[4] = {CPoint(boffsetx + 30,boffsety + curWeight) , 
        CPoint(boffsetx + 40,boffsety - 5 + curWeight) , 
        CPoint(boffsetx + 40,boffsety + 5 + curWeight),
        CPoint(boffsetx + 30,boffsety + curWeight) }; 
    BYTE   types[4] = {PT_MOVETO ,PT_LINETO,PT_LINETO,PT_LINETO}; 
    pDC->PolyDraw(labelPoints,types,4);

    //pen.DeleteObject();
    ReleaseDC(pDC);
}

Drawbacks  

The color transform between different color space still have some problem and it will be settled later. 

History  

Version 1.0  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student Institute of Software, Chinese Academy of Sciences
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --