CColor类封装

CColor类封装

  1.  Color.h

  

#pragma once

#include <sstream>
#include <string>
using namespace std;

class CColor
{
public:
	CColor(void);
	~CColor(void);

	CColor(const CColor& color);
	CColor& operator=(const CColor& color);

	CColor(float, float, float);
	CColor(int, int, int);
	CColor(COLORREF clr);

	// 以两个逗号分隔的字符串
	CColor(const string& strColor);

public:
	COLORREF GetRGB();
	DWORD GetARGB();
	float GetR();
	float GetG();
	float GetB();
	void SetStrValue(const string& strColor);
	string GetStrValue();
	string Trans2Txt();

private:
	void ParseStrColor(const string& strColor);

private:
	byte m_tAlpha;
	float m_fRed;
	float m_fGreen;
	float m_fBlue;

};

  2. Color.cpp

  

#include "StdAfx.h"

#include "Color.h"

#include "Utils.h"


CColor::CColor(void) : m_tAlpha(255), m_fRed(.0f), m_fGreen(.0f), m_fBlue(.0f)
{
}

CColor::~CColor(void)
{
}

CColor::CColor( const CColor& color )
{
	*this = color;
}

CColor& CColor::operator=(const CColor& color )
{
	if (this == &color)
	{
		return *this;
	}

	m_tAlpha = color.m_tAlpha;
	m_fRed = color.m_fRed;
	m_fGreen = color.m_fGreen;
	m_fBlue = color.m_fBlue;

	return *this;
}

CColor::CColor( float fRed, float fGreen, float fBlue)
{
	m_tAlpha = 255;
	m_fRed   = fRed;
	m_fGreen = fGreen;
	m_fBlue  = fBlue;
}

CColor::CColor( int nRed, int nGreen, int nBlue)
{
	m_tAlpha = 255;
	m_fRed   = (float)nRed / 255;
	m_fGreen = (float)nGreen / 255;
	m_fBlue  = (float)nBlue / 255;
}

CColor::CColor( COLORREF clr )
{
	byte* p = (byte*)&clr;
	int nRed = *p++;
	int nGreen = *p++;
	int nBlue = *p++;

	m_tAlpha = 255;
	m_fRed   = (float)nRed / 255;
	m_fGreen = (float)nGreen / 255;
	m_fBlue  = (float)nBlue / 255;
}

COLORREF CColor::GetRGB() 
{
	COLORREF colorrrefRGB;
	colorrrefRGB = RGB(byte(m_fRed * 255), byte(m_fGreen * 255), byte(m_fBlue * 255));
	return colorrrefRGB;
}

DWORD CColor::GetARGB()
{
	byte tRed = byte(m_fRed * 255);
	byte tGreen = byte(m_fGreen * 255);
	byte tBlue = byte(m_fBlue * 255);

	return (((DWORD)(tBlue)   << 0)  |
		((DWORD)(tGreen)  << 8)  |
		((DWORD)(tRed)    << 16) |
		((DWORD)(m_tAlpha) << 24));
}

float CColor::GetR()
{
	return m_fRed;
}

float CColor::GetG()
{
	return m_fGreen;
}

float CColor::GetB()
{
	return m_fBlue;
}

/**
* @brief 解析颜色字符串
*
* 根据指定字符串解析出颜色的三个分量
* 字符串以逗号或空格分割,如"strR,strG,strB" 其中strR,strG和strB都在0.0到1.0间取值
* @param[in] const string& strColor
* @return void
*/
void CColor::SetStrValue( const string& strColor )
{
	ParseStrColor(strColor);
}

std::string CColor::GetStrValue()
{
	stringstream ss;
	ss << Float2Str(m_fRed) << ", ";
	ss << Float2Str(m_fGreen) << ", ";
	ss << Float2Str(m_fBlue);

	return ss.str();
}

std::string CColor::Trans2Txt()
{
	return GetStrValue();
}

void CColor::ParseStrColor( const string& strColor )
{
	string strTmpValue = strColor;
	TrimLineSpace(strTmpValue);

	string strSplitSymbol = ",";
	size_t nSplitPos = strTmpValue.find_first_of(strSplitSymbol);

	if (nSplitPos == string::npos)
	{
		strSplitSymbol = " ";
	}

	size_t nFirstPos = strTmpValue.find_first_of(strSplitSymbol);

	if (nFirstPos != string::npos)
	{
		string strValue = strTmpValue.substr(0, nFirstPos);
		m_fRed = Str2Float(strValue);

		strTmpValue = strTmpValue.substr(nFirstPos + 1, string::npos);
		TrimLineSpace(strTmpValue);

		nFirstPos = strTmpValue.find_first_of(strSplitSymbol);

		if (nFirstPos != string::npos)
		{
			strValue = strTmpValue.substr(0, nFirstPos);
			m_fGreen = Str2Float(strValue);
		}

		strTmpValue = strTmpValue.substr(nFirstPos + 1, string::npos);
		TrimLineSpace(strTmpValue);

		if (strTmpValue != "")
		{
			m_fBlue = Str2Float(strTmpValue);
		}
	}

	m_tAlpha = 255;
}
原文地址:https://www.cnblogs.com/xiaobingqianrui/p/7550091.html