/* -------------------------------------------------------------------------
//MyCell Library - MyCell version 1.0
//
// This file is a part of the MyCell Library.
// The use and distribution terms for this software are covered by the
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
// which can be found in the file CPL.txt at this distribution. By using
// this software in any fashion, you are agreeing to be bound by the terms
// of this license. You must not remove this notice, or any other, from
// this software.
//
// Creator: yanxueming
// Email: xueming.yan@gmail.com
// -----------------------------------------------------------------------*/
#include "stdafx.h"
#include <vector>
#include <map>
#include <algorithm>
//#include "../include/mycell.h"
#include "../include/ColorTable.h"
using namespace std;
namespace mycell{
ColorEntry ColorTable::color_table[COLORTABLE_COUNT]=
{
{RGB(0,0,0),_T("��ɫ")},
{RGB(0x99,0x33,0x00),_T("��ɫ")},
{RGB(0x33,0x33,0),_T("���ɫ")},
{RGB(0,0x33,0),_T("����")},
{RGB(0,0x33,0x66),_T("����")},
{RGB(0,0,0x80),_T("����")},
{RGB(0x33,0x33,0x99),_T("����")},
{RGB(0x33,0x33,0x33),_T("��ɫ-80%")},
{RGB(0x80,0,0),_T("���")},
{RGB(0xff,0x66,0),_T("��ɫ")},
{RGB(0x80,0x80,0),_T("���")},
{RGB(0,0x80,0),_T("��ɫ")},
{RGB(0,0x80,0x80),_T("��ɫ")},
{RGB(0,0,0xff),_T("��ɫ")},
{RGB(0x66,0x66,0x99),_T("��-��")},
{RGB(0x80,0x80,0x80),_T("��ɫ-50%")},
{RGB(0xff,0,0),_T("��ɫ")},
{RGB(0xff,0x99,0),_T("dz��ɫ")},
{RGB(0x99,0xcc,0),_T("���ɫ")},
{RGB(0x33,0x99,0x66),_T("����")},
{RGB(0x33,0xcc,0xcc),_T("ˮ��ɫ")},
{RGB(0x33,0x66,0xff),_T("dz��")},
{RGB(0x80,0,0x80),_T("������")},
{RGB(0x96,0x96,0x96),_T("��ɫ-40%")},
{RGB(0xFF,0,0xFF),_T("�ۺ�")},
{RGB(0xff,0xcc,0),_T("��ɫ")},
{RGB(0xff,0xff,0),_T("��ɫ")},
{RGB(0,0xff,0),_T("����")},
{RGB(0,0xff,0xff),_T("����")},
{RGB(0,0xcc,0xff),_T("����")},
{RGB(0x99,0x33,0x66),_T("÷��")},
{RGB(0xc0,0xc0,0xc0),_T("��ɫ-25%")},
{RGB(0xff,0x99,0xcc),_T("õ���")},
{RGB(0xff,0xcc,0x99),_T("��ɫ")},
{RGB(0xff,0xff,0x99),_T("dz��")},
{RGB(0xcc,0xff,0xcc),_T("dz��")},
{RGB(0xcc,0xff,0xff),_T("dz����")},
{RGB(0x99,0xcc,0xff),_T("����")},
{RGB(0xcc,0x99,0xff),_T("����")},
{RGB(0xff,0xff,0xff),_T("��ɫ")},
{RGB(0x99,0x99,0xff),_T("����")},
{RGB(0x99,0x33,0x66),_T("÷��")},
{RGB(0xff,0xff,0xcc),_T("����ɫ")},
{RGB(0xcc,0xff,0xcc),_T("dz����")},
{RGB(0x66,0,0x66),_T("����")},
{RGB(0xff,0x80,0x80),_T("ɺ����")},
{RGB(0,0x66,0xcc),_T("����")},
{RGB(0xcc,0xcc,0xff),_T("����")},
/*
{RGB(0,0,0x80),_T("����")},
{RGB(0xff,0,0xff),_T("�ۺ�")},
{RGB(0xff,0xff,0),_T("��ɫ")},
{RGB(0,0xff,0xff),_T("����")},
{RGB(0x80,0,0x80),_T("������")},
{RGB(0x80,0,0),_T("���")},
{RGB(0,0x80,0x80),_T("��ɫ")},
{RGB(0,0,0xff),_T("��ɫ")},
*/
};
struct HLSDelta
{
ColorTable::TIndex dh,dl,ds,index;
bool operator<(HLSDelta rhs)const
{
if(dh<rhs.dh)
return true;
else if(dh>rhs.dh)
return false;
else{
if(dl<rhs.dl)
return true;
else if(dl>rhs.dl)
return false;
else{
return ds<rhs.ds;
}
}
}
};
//����һ��RGB��ɫֵ��������ɫ�������ֵ������Ҳ���
//��ȵ���ɫ�������������ɫ����
ColorTable::TIndex ColorTable::find_color(COLORREF clr)const
{
const TIndex cnt=GetCount();
for(TIndex i=0;i<cnt;++i)
{
if(clr==color_table[i].color)
return i;
}
vector<HLSDelta> vec(cnt);
const HLS lhs0=RGBtoHLS(clr);
for(TIndex i=0;i<cnt;++i)
{
HLSDelta& item=vec[i];
item.index=i;
HLS lhs=color_table[i].lhs;
item.dh=lhs.H>lhs0.H?lhs.H-lhs0.H:lhs0.H-lhs.H;
item.dl=lhs.L>lhs0.L?lhs.L-lhs0.L:lhs0.L-lhs.L;
item.ds=lhs.S>lhs0.S?lhs.S-lhs0.S:lhs0.S-lhs.S;
vec.push_back(item);
}
sort(vec.begin(),vec.end());
return vec[0].index;
}
}//namespace mycell