RGB转换为HSL的算法一般需要自己编写,但是在C++Builder中可以利用FireMonkey的函数实现RGB与HSL之间的转换。
下面是利用FireMonkey的RGBtoHSL和HSLtoRGB函数(需要注意的是函数参数H、S、L都是0-1之间的数据,所以显示的色相H、饱和度S、亮度L都要经过转换)实现的4个自编函数的代码:
#include <System.UIConsts.hpp>
// ---------------------------------------------------------------------------
// 把TAlphaColor转换为HSL
void __fastcall AlphaColorToHSL(TAlphaColor aColor, float &fH, float &fS, float &fL)
{
// void __fastcall RGBtoHSL(System::Uitypes::TAlphaColor RGB, float &H, float &S, float &L);
RGBtoHSL(aColor, fH, fS, fL);
fH = fH * 360.0; // 其值在0-1.0之间,所以要转换
fS = fS * 100.0; // 其值在0-1.0之间,所以要转换
fL = fL * 100.0; // 其值在0-1.0之间,所以要转换
}
// ---------------------------------------------------------------------------
// 把TAlphaColor转换为字符串HSL
void __fastcall AlphaColorToHSL(TAlphaColor aColor, AnsiString &Hstr, AnsiString &Sstr, AnsiString &Lstr)
{
float fH, fS, fL;
// 把TAlphaColor转换为HSL
AlphaColorToHSL(aColor, fH, fS, fL);
Hstr = FloatToStrF(fH, ffFixed, 15, 2);
Sstr = FloatToStrF(fS, ffFixed, 15, 2);
Lstr = FloatToStrF(fL, ffFixed, 15, 2);
}
// ---------------------------------------------------------------------------
// 把HSL转换为TAlphaColor
TAlphaColor __fastcall HSLToAlphaColor(float &fH, float &fS, float &fL)
{
TAlphaColor aColor;
// 判断在合适的范围内
if (fH < 0.0)
fH = 0.0;
if (fH > 360.0)
fH = 360.0;
if (fS < 0.0)
fS = 0.0;
if (fS > 100.0)
fS = 100.0;
if (fL < 0.0)
fL = 0.0;
if (fL > 100.0)
fL = 100.0;
fH = fH / 360.0; // 其值在0-1.0之间,所以要转换
fS = fS / 100.0; // 其值在0-1.0之间,所以要转换
fL = fL / 100.0; // 其值在0-1.0之间,所以要转换
// System::Uitypes::TAlphaColor __fastcall HSLtoRGB(float H, float S, float L);
aColor = HSLtoRGB(fH, fS, fL);
return aColor;
}
// ---------------------------------------------------------------------------
// 把字符串HSL转换为TAlphaColor
TAlphaColor __fastcall HSLToAlphaColor(AnsiString &Hstr, AnsiString &Sstr, AnsiString &Lstr)
{
float fH, fS, fL;
TAlphaColor aColor;
// 判断是否为数字,不是返回0
fH = StrToFloatDef(Hstr, 0);
fS = StrToFloatDef(Sstr, 0);
fL = StrToFloatDef(Lstr, 0);
// 把HSL转换为TAlphaColor
aColor = HSLToAlphaColor(fH, fS, fL);
Hstr = FloatToStrF(fH, ffFixed, 15, 2);
Sstr = FloatToStrF(fS, ffFixed, 15, 2);
Lstr = FloatToStrF(fL, ffFixed, 15, 2);
return aColor;
}
// ---------------------------------------------------------------------------
顺便放出TAlphaColor与RGB之间转换的4个自编函数。
// ---------------------------------------------------------------------------
// 把TAlphaColor转换为整数RGB
void __fastcall AlphaColorToRGB(TAlphaColor aColor, int &iR, int &iG, int &iB)
{
BYTE red23, green, blue2;
DWORD col;
col = AlphaColorToColor(aColor);
red23 = GetRValue(col);
green = GetGValue(col);
blue2 = GetBValue(col);
iR = (int)red23;
iG = (int)green;
iB = (int)blue2;
}
// ---------------------------------------------------------------------------
// 把TAlphaColor转换为字符串RGB
void __fastcall AlphaColorToRGB(TAlphaColor aColor, AnsiString &Rstr, AnsiString &Gstr, AnsiString &Bstr)
{
int iR, iG, iB;
// 把TAlphaColor转换为整数RGB
AlphaColorToRGB(aColor, iR, iG, iB);
Rstr = IntToStr(iR);
Gstr = IntToStr(iG);
Bstr = IntToStr(iB);
}
// ---------------------------------------------------------------------------
// 把整数RGB转换为TAlphaColor,RGB不在0-255之间要修改
TAlphaColor __fastcall RGBToAlphaColor(int &iR, int &iG, int &iB)
{
BYTE red, green, blue;
DWORD col;
TAlphaColor aColor;
if (iR < 0)
iR = 0;
if (iR > 255)
iR = 255;
if (iG < 0)
iG = 0;
if (iG > 255)
iG = 255;
if (iB < 0)
iB = 0;
if (iB > 255)
iB = 255;
red = (BYTE)iR;
green = (BYTE)iG;
blue = (BYTE)iB;
// 转换RGB为AlphaColor
aColor = MakeColor(red, green, blue);
return aColor;
}
// ---------------------------------------------------------------------------
// 把字符串RGB转换为TAlphaColor,RGB不在0-255之间要修改
TAlphaColor __fastcall RGBToAlphaColor(AnsiString &Rstr, AnsiString &Gstr, AnsiString &Bstr)
{
int iR, iG, iB;
BYTE red, green, blue;
DWORD col;
TAlphaColor aColor;
// iR = StrToInt(Rstr);
// iG = StrToInt(Gstr);
// iB = StrToInt(Bstr);
// 判断是否为数字,不是返回0
iR = StrToIntDef(Rstr, 0);
iG = StrToIntDef(Gstr, 0);
iB = StrToIntDef(Bstr, 0);
// 把整数RGB转换为TAlphaColor,RGB不在0-255之间要修改
aColor = RGBToAlphaColor(iR, iG, iB);
Rstr = IntToStr(iR);
Gstr = IntToStr(iG);
Bstr = IntToStr(iB);
return aColor;
}
// ---------------------------------------------------------------------------
利用TStringGrid设计的RGB与HSL互相转换的程序界面。