Click here to Skip to main content
15,896,402 members
Articles / Desktop Programming / MFC

Color components editor control

Rate me:
Please Sign up or sign in to vote.
4.93/5 (11 votes)
2 Apr 20025 min read 79.8K   2.7K   31  
This control lets you edit RGB or HSL components of a color, like in Paint Shop Pro.
// This is a part of the GWC Library
// Copyright (C) 2001-2002 Aircom software
// All rights reserved.
//
// This source code can be used, distributed or modified
// free of charge only if this header copyright is not removed.
// In other cases, contact us at:
// web:   http://www.aircom.org
// email: info@aircom.org
//

#include "stdafx.h"
#include "GWCColorComponentSet.h"
#include "GWCColorComponentEditCtrl.h"
#include "GWCColorFunc.h"

//================================================================
// Description:
// Returns the color maintained by this object, as RGB components.
//
// Returns:
// The color maintained by this object, as RGB components.
//================================================================
COLORREF GWCColorComponentSet::GetColor()
{
	return m_color;
}

//================================================================
// Description:
// Returns the color maintained by this object, as HSL components.
//
// Parameters:
// hue - A pointer to the hue component of the color.
// sat - A pointer to the saturation component of the color.
// light - A pointer to the light component of the color.
//================================================================
void GWCColorComponentSet::GetColor(BYTE* hue, BYTE* sat, BYTE* light)
{
	*hue = m_hue;
	*sat = m_saturation;
	*light = m_light;
}

//====================================================================
// Description:
// Returns the color maintained by this object, as an HTML color code.
//
// Parameters:
// str - A reference to a string where the HTML color code is placed.
//====================================================================
void GWCColorComponentSet::GetColor(CString& str)
{
	str.Format(_T("#%02X%02X%02X"),GetRValue(m_color),GetGValue(m_color),GetBValue(m_color));
}

//=============================================================
// Description:
// Sets the color maintained by this object, as HSL components.
//
// Parameters:
// hue - The hue component of the color.
// sat -  The saturation component of the color.
// light -  The light component of the color.
//=============================================================
void GWCColorComponentSet::SetColor(BYTE hue, BYTE sat, BYTE light)
{
	// Initialize the new color
	m_hue = hue;
	m_saturation = sat;
	m_light = light;
	m_color = GetColor();

	// Notify all component controls of the new color
	CompVector::iterator iter = m_components.begin();
	while(iter != m_components.end())
	{
		GWCColorComponentEditCtrl* pComp = *iter;

		pComp->OnColorChanged(m_hue,m_saturation,m_light);

		iter++;
	}
}

//=============================================================
// Description:
// Sets the color maintained by this object, as RGB components.
//
// Parameters:
// color - the RGB components of the color.
//=============================================================
void GWCColorComponentSet::SetColor(COLORREF color)
{
	// Initialize the new color
	m_color = color;

	double h,l,s;
	GWCColorFunc::RGBtoHSL(color,&h,&s,&l);
	m_hue = Round(h*255.);
	m_saturation = Round(s*255.);
	m_light = Round(l*255.);

	// Notify all component controls of the new color
	CompVector::iterator iter = m_components.begin();
	while(iter != m_components.end())
	{
		GWCColorComponentEditCtrl* pComp = *iter;

		pComp->OnColorChanged(m_hue,m_saturation,m_light);

		iter++;
	}
}

//===================================================================================
// Description:
// Adds a component control to the list maintained by this control for a given color.
//
// Parameters:
// wnd - The control to add.
//===================================================================================
void GWCColorComponentSet::RegisterComponentCtrl(GWCColorComponentEditCtrl& wnd)
{
	m_components.push_back(&wnd);
	wnd.SetOwnerSet(this);
}

//===============================================================================
// Description:
// Called by one of the control maintained by this object when its value changes.
//
// Parameters:
// comp - The control whose value has changed.
//===============================================================================
void GWCColorComponentSet::OnComponentChanged(GWCColorComponentEditCtrl* comp)
{
	// Change the color maintained by this object regarding the new value sent
	// by one of the components controls

	int type = comp->GetType();

	if (type == GWCColorComponentEditCtrl::RED)
	{
		m_color = RGB(comp->GetValue(),GetGValue(m_color),GetBValue(m_color));
		double h,l,s;
		GWCColorFunc::RGBtoHSL(m_color,&h,&s,&l);
		m_hue = Round(h*255.);
		m_saturation = Round(s*255.);
		m_light = Round(l*255.);
	}
	else if (type == GWCColorComponentEditCtrl::GREEN)
	{
		m_color = RGB(GetRValue(m_color),comp->GetValue(),GetBValue(m_color));
		double h,l,s;
		GWCColorFunc::RGBtoHSL(m_color,&h,&s,&l);
		m_hue = Round(h*255.);
		m_saturation = Round(s*255.);
		m_light = Round(l*255.);
	}
	else if (type == GWCColorComponentEditCtrl::BLUE)
	{
		m_color = RGB(GetRValue(m_color),GetGValue(m_color),comp->GetValue());
		double h,l,s;
		GWCColorFunc::RGBtoHSL(m_color,&h,&s,&l);
		m_hue = Round(h*255.);
		m_saturation = Round(s*255.);
		m_light = Round(l*255.);
	}
	else if (type == GWCColorComponentEditCtrl::HUE)
	{
		m_hue = comp->GetValue();
		m_color = GWCColorFunc::HSLtoRGB((double)m_hue/255.,(double)m_saturation/255.,(double)m_light/255.);
	}
	else if (type == GWCColorComponentEditCtrl::SAT)
	{
		m_saturation = comp->GetValue();
		m_color = GWCColorFunc::HSLtoRGB((double)m_hue/255.,(double)m_saturation/255.,(double)m_light/255.);
	}
	else if (type == GWCColorComponentEditCtrl::LIGHT)
	{
		m_light = comp->GetValue();
		m_color = GWCColorFunc::HSLtoRGB((double)m_hue/255.,(double)m_saturation/255.,(double)m_light/255.);
	}

	// Notify all other component controls

	CompVector::iterator iter = m_components.begin();
	while(iter != m_components.end())
	{
		GWCColorComponentEditCtrl* pComp = *iter;

		if (pComp != comp)
		{
			if ((type == GWCColorComponentEditCtrl::RED) || 
				(type == GWCColorComponentEditCtrl::GREEN) ||
				(type == GWCColorComponentEditCtrl::BLUE))
			{
				if ((pComp->GetType() != GWCColorComponentEditCtrl::RED) &&
					(pComp->GetType() != GWCColorComponentEditCtrl::GREEN) &&
					(pComp->GetType() != GWCColorComponentEditCtrl::BLUE))
					pComp->OnColorChanged(m_color);
			}
			else
			{
				if ((pComp->GetType() != GWCColorComponentEditCtrl::HUE) &&
					(pComp->GetType() != GWCColorComponentEditCtrl::SAT) &&
					(pComp->GetType() != GWCColorComponentEditCtrl::LIGHT))
					pComp->OnColorChanged(m_hue,m_saturation,m_light);
				else if (type == GWCColorComponentEditCtrl::HUE)
				{
					if ((pComp->GetType() == GWCColorComponentEditCtrl::SAT) ||
						(pComp->GetType() == GWCColorComponentEditCtrl::LIGHT))
						pComp->SetColorBar(m_hue,255,127);
				}
			}
		}

		iter++;
	}
}

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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions