Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

The Photoshop-Like Color Palette Dialog

0.00/5 (No votes)
17 Apr 2012 1  
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:

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.

//
//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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here