Click here to Skip to main content
15,886,519 members
Articles / Desktop Programming / MFC

Cool Colour Selector Including Hue, Saturation, and Lumination

Rate me:
Please Sign up or sign in to vote.
3.67/5 (14 votes)
9 Nov 2006MIT1 min read 86.7K   2.3K   29  
This colour selector helps to make selecting colours more efficient
// SelColorDlg.h
//

#pragma once
#include "afxwin.h"
#include "resource.h"


// CSelColorDlg dialog

#define COLORSET_TOTAL			20
#define COLORSET_MAX			COLORSET_TOTAL - 1

class CColorSet
{
public:
	// classes constructor/deconstructor
	CColorSet() {
		// set all colours to white by default
		for(int nPos = 0; nPos < COLORSET_TOTAL; nPos++)
			m_rgbColors[nPos] = RGB(255, 255, 255);
	}

	// data storage/manipulation functions
	char GetCount() { return COLORSET_TOTAL; }
	void SetColor(int nPos, COLORREF rgbNewColor) {
		if(nPos < 0 || nPos > COLORSET_MAX) return;
		m_rgbColors[nPos] = rgbNewColor;
	}
	COLORREF GetColor(int nPos) {
		if(nPos < 0 || nPos > COLORSET_MAX) return NULL;
		return m_rgbColors[nPos];
	}
	void SetPos(int nPos, int nLeft, int nTop) {
		if(nPos < 0 || nPos > COLORSET_MAX) return;
		m_ptPos[nPos].x = nLeft;
		m_ptPos[nPos].y = nTop;
	}
	void SetLeft(int nPos, int nLeft) {
		if(nPos < 0 || nPos > COLORSET_MAX) return;
		m_ptPos[nPos].x = nLeft;
	}
	void SetTop(int nPos, int nTop) {
		if(nPos < 0 || nPos > COLORSET_MAX) return;
		m_ptPos[nPos].y = nTop;
	}
	POINT GetPos(int nPos) {
		return m_ptPos[nPos];
	}
	int GetLeft(int nPos) {
		if(nPos < 0 || nPos > COLORSET_MAX) return NULL;
		return m_ptPos[nPos].x;
	}
	int GetTop(int nPos) {
		if(nPos < 0 || nPos > COLORSET_MAX) return NULL;
		return m_ptPos[nPos].y;
	}

private:
	COLORREF m_rgbColors[COLORSET_TOTAL];		// custom colors
	POINT m_ptPos[COLORSET_TOTAL];				// custom color positions
};

#ifndef POINT_2F

struct POINT_2F {
	float x;
	float y;
};

#endif

class CSelColorDlg : public CDialog
{
	DECLARE_DYNAMIC(CSelColorDlg)

public:
	// classes constructor/deconstructor
	CSelColorDlg(CWnd* pParent = NULL);   // standard constructor
	CSelColorDlg(BYTE nRedVal, BYTE nGreenVal, BYTE nBlueVal,
		CWnd *pParent = NULL);
	virtual ~CSelColorDlg();

	// data storage/manipulation
	void SetHLSValue(BYTE hue, BYTE lum, BYTE sat) {
		m_nHue = hue;
		m_nLum = lum;
		m_nSat = sat;
		Invalidate(FALSE);
	}
	void SetHueValue(BYTE hue) {
		m_nHue = hue;
		Invalidate(FALSE);
	}
	void SetLumValue(BYTE lum) {
		m_nLum = lum;
		Invalidate(FALSE);
	}
	void SetSatValue(BYTE sat)
	{
		m_nSat = sat;
		Invalidate(FALSE);
	}
	void SetColor(COLORREF color) {
		SelectColor(GetRValue(color), GetGValue(color), GetBValue(color));
	}

	BYTE GetHueValue() const { return m_nHue; }
	BYTE GetSatValue() const { return m_nSat; }
	BYTE GetLumValue() const { return m_nLum; }
	COLORREF GetColor() const {
		return RGB(m_nNewRed, m_nNewGreen, m_nNewBlue);
	}
	CString GetRefCode() const { return m_strRef; }

	// RGB -> HLS and HLS -> RGB conversion functions
	void RGBtoHLS(COLORREF lRGBColor, BYTE* H, BYTE* L, BYTE* S);
	WORD HueToRGB(WORD n1, WORD n2, WORD hue);
	DWORD HLStoRGB(WORD hue, WORD lum, WORD sat);

	// custom color manipulation
	COLORREF GetCustomColor(char nPos);
	void SetCustomColor(char nPos, COLORREF rgbColor);

// Dialog Data
	enum { IDD = IDD_COLSEL };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

	DECLARE_MESSAGE_MAP()
private:
	afx_msg void OnPaint();
	afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
	afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnEnChangeHue();
	afx_msg void OnEnChangeSat();
	afx_msg void OnEnChangeLum();
	afx_msg void OnEnChangeColor();
	afx_msg void OnEnChangeRef();

	void DDX_TextEx(CDataExchange* pDX, UINT nID, BYTE nMin, BYTE nMax, BYTE &nValue);
	//void DDX_ValidateText(CDataExchange* pDX, UINT nID, BYTE nMax, BYTE &nValue);

	void InitCustomColors(bool bMonochrome = false);
	void InitColorSpectrum();
	void GenColorBar();

	void DrawGradBar(CDC* pDC);
	void DrawFrameBorder(CDC* pDC, LPRECT rect);
	void DrawColorSpectrum(CDC* pDC);
	void DrawColorBar(CDC* pDC);
	void DrawCurColorBox(CDC* pDC);
	void DrawNewColorBox(CDC* pDC);
	void DrawCustomColors(CDC* pDC);
	void DrawCross(CDC* pDC, LPPOINT ptPos);
	void DrawBarArrow(CDC* pDC, int nLum);

	void SelectColor(BYTE nRed, BYTE nGreen, BYTE nBlue);
	char GetCustomBox(LPPOINT ptPos);

	CBitmap m_bmpSpectrum;
	CBitmap m_bmpBar;
	CPen m_tempPen;
	CBrush m_tempBrush;
	bool m_bMouseMove;
	char nDragMode, nDragSrc;
	BYTE m_nDragRed, m_nDragGreen, m_nDragBlue;
	POINT m_ptPos;

	CString m_strRef;
	BYTE m_nHue, m_nSat, m_nLum;
	BYTE m_nRed, m_nGreen, m_nBlue;
	BYTE m_nNewRed, m_nNewGreen, m_nNewBlue;

	char m_nInsertPos;

public:
	afx_msg void OnBnClickedAddcolour();
};

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, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer Rotorz Limited
United Kingdom United Kingdom
I have been fascinated by software and video games since a young age when I was given my first computer, a Dragon 32. Since then I have experimented with numerous methods of development ranging from point-and-click type packages to C++. I soon realized that software development was what I wanted to do.

Having invested a lot of time into programming with various languages and technologies I now find it quite easy to pickup new ideas and methodologies. I relish learning new ideas and concepts.

Throughout my life I have dabbled in game and engine development. I was awarded a first for the degree "BEng Games and Entertainment Systems Software Engineering" at the University of Greenwich. It was good to finally experience video games from a more professional perspective.

Due to various family difficulties I was unable to immediately pursue any sort of software development career. This didn't stop me from dabbling though!

Since then I formed a company to focus upon client projects. Up until now the company has primarily dealt with website design and development. I have since decided that it would be fun to go back to my roots and develop games and tools that other developers can use for their games.

We have recently released our first game on iPhone/iPad called "Munchy Bunny!" (see: http://itunes.apple.com/us/app/munchy-bunny!/id516575993?mt=8). We hope to expand the game and release to additional platforms.

Also, check out our tile system extension for Unity! (see: http://rotorz.com/tilesystem/)

Comments and Discussions