Click here to Skip to main content
15,895,740 members
Articles / Desktop Programming / MFC

A color picker dialog with a color dropper like Photoshop and Frontpage 2000

Rate me:
Please Sign up or sign in to vote.
4.83/5 (10 votes)
23 Jan 20022 min read 94.9K   4.3K   29  
A color picker and dropper that extends the standard Windows dialogs to behave like Photoshop or Frontpage 2000
// FODropPaletteControl.h: interface for the CFODropPaletteControl class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_FODROPPALETTECONTROL_H__25BB50B3_04D9_11D6_A4F2_525400EA266C__INCLUDED_)
#define AFX_FODROPPALETTECONTROL_H__25BB50B3_04D9_11D6_A4F2_525400EA266C__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "FOColorCellObj.h"
#define FO_TOTAL_CELLS		144

class CFODropPaletteControl : public CObject
{
protected:
	DECLARE_SERIAL(CFODropPaletteControl);
public:

	// Constructor
	CFODropPaletteControl();

	// Destructor
	virtual ~CFODropPaletteControl();
	
	// Creates the component and initializes the data members.
	virtual void Create(CWnd *pWnd,COLORREF crDefault,CRect &rcPos,BOOL bPopup = FALSE);

	// Serialize data
	virtual void Serialize(CArchive &ar);

	// Initialize Color Palette
	virtual void InitColorPalette();

	// Adjust Cells
	virtual void AdjustCells(CRect rcPos);

	// Compute Colors
	virtual void ComputeColors();

	// Get RectAngle From Point
	int GetAngleFromPoint(int nX, int nY);

	// Draw
	virtual void OnDraw(CDC *pDC);

	// Update All
	virtual void UpdateAll();

	// Get Current Color
	COLORREF	GetRGB () const				{	return m_crCurColor; }

	// Set Current Color
	void		SetRGB(COLORREF ref);

	//
	void		SetHLS (double hue,double luminance, double saturation);

	//
	void		GetHLS (double *hue,double *luminance, double *saturation);

	// Get Old Colo rRectangle
	CRect		GetOldColorRect();

	// HitTest
	virtual CFOColorCellObj* HitTest(CPoint point);

	// Is Popup Style?
	BOOL		IsPopupStyle() const		{ return m_bPopup; }

	// Set Popup Style
	void		SetPopupStyle(const BOOL bPopup)	{ m_bPopup = bPopup; }
	
	// Define for WM_TFC_SELECTOK message.
	virtual void OnSelectOK(WPARAM wParam, LPARAM lParam);
	
	// Define for WM_TFC_SELECTCANCEL message.
	virtual void OnSelectCancel(WPARAM wParam, LPARAM lParam);
	
protected:

	// Total Cells
	CFOColorCellObj m_crCells[FO_TOTAL_CELLS];
	
public:

	// virtual void Serialize(CArchive &ar);
	// Save Document
	virtual BOOL SaveDocument(LPCTSTR lpszPathName);

	// Open Document
	virtual BOOL OpenDocument(LPCTSTR lpszPathName);

	// Get File
	CFile *		 GetFile(LPCTSTR lpszFileName, UINT nOpenFlags,CFileException* pError);

	// Release File
	virtual void ReleaseFile(CFile* pFile, BOOL bAbort);
	
	// Set wnd handle.
	void SetWnd(CWnd *pWnd)					{ m_pParent = pWnd; }
	
	// Invalidates this gadget.
	void Invalidate(BOOL bRedraw = FALSE);
	
	// Update the given rectangle.
	void InvalRect(CRect rcPos);
	
	// Capture the mouse.
	void CaptureMouse(BOOL bCapture);
	
	// WM_CHAR message.
	void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
	
	// WM_LBUTTONDOWN message.
	void OnLButtonDown(UINT nFlags, CPoint point); 
	
	// WM_LBUTTONDBLCLK message.
	void OnLButtonDblClk(UINT nFlags, CPoint point);
	
	// WM_RBUTTONDOWN message.
	void OnRButtonDown(UINT nFlags, CPoint point); 
	
	// WM_LBUTTONUP message.
	void OnLButtonUp(UINT nFlags, CPoint point);
	
	// WM_MOUSEMOVE message.
	void OnMouseMove(UINT nFlags, CPoint point);

	// WM_KEYDOWN message.
	void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
	
	// Cancel mode.
	virtual void OnCancelMode();

	// Get Rectangle
	CRect GetRect() const						{ return m_rcPosition; }

	// Set Rectangle
	void  SetRect(const CRect rc)				{ m_rcPosition = rc; }

	// Set Custom Text
	void  SetCustomText(const CString strText )	{ strCustom = strText; }

	// Get Custom Text
	CString GetCustomText() const				{ return strCustom; }

	// Set Custom Bar
	void SetCustomBar(const BOOL bCustom);

	// Set Main Border Size
	void SetMainBorderSize(const int nBorder);
 
	// Get Main Border Size
	int  GetMainBorderSize() const				{ return nMainBorderSize; }

	// Set Cell Border Size
	void SetCellBorderSize(const int nBorder);

	// Get Cell Border Size
	int GetCellBorderSize() const				{ return nCellBorderSize; }

	// Set Border Type
	void SetBorderType(const UINT nType)		{ nBorderType = nType; }

	// Get Border Type
	UINT GetBorderType() const					{ return nBorderType; }

	// Update Custom
	void UpdateCustom();

	// Get rgb from hue.
	BYTE GetRGBFromHue(float rm1, float rm2, float rh);

	// Get rgb value from hls.
	COLORREF GetRGBFromHLS( double H, double L, double S );

	// Get rgb value from hls.
	COLORREF GetRGBFromHLSExtend( double H, double L, double S );

	// Convert rgb value to hsl.
	void ConvertRGBToHSL( COLORREF rgb, double *H, double *S, double *L );

public:
	
	// Draw border for the control.
	// Draw Other Border
	virtual void DrawOtherBorder(CDC *pDC,CRect &rcPos,UINT nType);

	// Draw Custom
	virtual void OnCustomDraw(CDC *pDC,BOOL bHasColor);

	// Draw Custom Selected
	virtual void OnCustomDrawSelect(CDC *pDC);
	
	// Draw a rectangle
	void DrawRect(CDC *pDC, RECT rect);

	// Draw a rectangle
	void DrawRect(CDC *pDC, int x, int y, int w, int h);

	// Draw a rectangle	
	void DrawRect(CDC *pDC, RECT rect, COLORREF color);

	// Draw a rectangle
	void DrawRect(CDC *pDC, int x, int y, int w, int h, COLORREF color);
	
	// Draw a circle
	void DrawCircle(CDC *pDC, int x, int y, int radius);

	//Draw a circle
	void DrawCircle(CDC *pDC, int x, int y, int radius, COLORREF color);
	
	// Draw a line
	void DrawLine(CDC *pDC, CPoint& start, CPoint& end);

	// Draw a line
	void DrawLine(CDC *pDC, int xStart, int yStart, int xEnd, int yEnd);

	// Draw a line
	void DrawLine(CDC *pDC, CPoint& start, CPoint& end, COLORREF color);

	// Draw a line
	void DrawLine(CDC *pDC, int xStart, int yStart, int xEnd, int yEnd, COLORREF color);
	
protected:

	// The Rectangle of Cells
	CRect				rcCells;
	
	// The Rectangle of Position
	CRect				m_rcPosition;

	// The Rectangle of Custom
	CRect				rcCustom;

	// the Rectangle of Custom Saved
	CRect				rcCustomSave;

	// Sep Rectangle
	CRect				rcSep;

	// Current Color
	COLORREF			m_crCurColor;

	// The pointer to parent window
	CWnd*				m_pParent;

	// Is it popup style?
	BOOL				m_bPopup;

	// Is it custom selected?
	BOOL				m_bCustomSelect;

	// Custom string
	CString				strCustom;

	// Rows
	int					m_nRows;

	// Columns
	int					m_nCols;

	// Has it custom bar
	BOOL				bHasCustomBar;

	// Main Border Size
	int					nMainBorderSize;

	// Cell Border Size
	int					nCellBorderSize;

	// Border Type
	UINT				nBorderType;

	//
	double				m_dblLum;

	//
	double				m_dblSat;

	//
	double				m_dblHue;

	// Text Font
	CFont				fText;
};

//Define type CTypedPtrList
typedef CTypedPtrList<CObList, CFODropPaletteControl*> CFODropPaletteControlList;


#endif // !defined(AFX_FODROPPALETTECONTROL_H__25BB50B3_04D9_11D6_A4F2_525400EA266C__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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


Written By
Web Developer
United States United States
jack Mesic is the president of UCanCode Software Technology,Inc..

Comments and Discussions