// ColorButton.cpp : implementation file
//

#include "stdafx.h"
#include "mw4gameed2.h"
#include "ColorButton.h"


/////////////////////////////////////////////////////////////////////////////
// CColorButton

CColorButton::CColorButton()
{

}

CColorButton::~CColorButton()
{
}


BEGIN_MESSAGE_MAP(CColorButton, CButton)
	//{{AFX_MSG_MAP(CColorButton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColorButton message handlers

void CColorButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
	CBrush brush;
	CPen WhitePen,BlackPen;
	COLORREF hicol,locol;
	int r,g,b;
	r=GetRValue(Color)<<1;
	r=(r>0xff)?0xff:r;
	g=GetGValue(Color)<<1;
	g=(g>0xff)?0xff:g;
	b=GetBValue(Color)<<1;
	b=(b>0xff)?0xff:b;
	hicol=RGB(r,g,b);
	locol=RGB((GetRValue(Color)>>1),GetGValue(Color)>>1,GetBValue(Color)>>1);
	
	WhitePen.CreatePen(PS_SOLID,1,hicol);
	BlackPen.CreatePen(PS_SOLID,1,locol);
	brush.CreateSolidBrush(Color);
	CRect crct=lpDrawItemStruct->rcItem;
	pDC->FillRect(&crct,&brush);
	
	pDC->SelectObject(WhitePen);
	pDC->MoveTo(crct.left,crct.bottom-1);
	pDC->LineTo(crct.left,crct.top);
	pDC->LineTo(crct.right-1,crct.top);

	pDC->SelectObject(BlackPen);
	pDC->LineTo(crct.right-1,crct.bottom-1);
	pDC->LineTo(crct.left,crct.bottom-1);

	brush.DeleteObject();	
	WhitePen.DeleteObject();
	BlackPen.DeleteObject();
}



void CColorButton::SetColor(const RGBAColor &col)
{
COLORREF rcol=RGB((BYTE)(col.red*255),
				(BYTE)(col.green*255),
				(BYTE)(col.blue*255));

SetColor((COLORREF)rcol);	
}

RGBAColor CColorButton::GetRGBAColor()
{
return RGBAColor((Scalar)(GetRValue(Color)/255.0f),
												  (Scalar)(GetGValue(Color)/255.0f),
												  (Scalar)(GetBValue(Color)/255.0f),
												  1.0f);
}

void CColorButton::SetColor(const RGBColor &col)
{
COLORREF rcol=RGB((BYTE)(col.red*255),
				(BYTE)(col.green*255),
				(BYTE)(col.blue*255));

SetColor((COLORREF)rcol);	
}

RGBColor CColorButton::GetRGBColor()
{

	return RGBColor((Scalar)(GetRValue(Color)/255.0f),
												  (Scalar)(GetGValue(Color)/255.0f),
												  (Scalar)(GetBValue(Color)/255.0f)
												  );
}

LRESULT CColorButton::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	return CButton::WindowProc(message, wParam, lParam);
}
