StrToColor function
Implementation
TColor? StrToColor(String data, [TColor? def = clBlack, TLocale? lng] )
{
if(data.isEmpty)
return def;
data = data.toLowerCase().trim();
var tmp = data.split('(');
if(tmp.length==2 && tmp[1].isNotEmpty) // rgb(r,g,b) || rgba(r,g,b,a)
{
var rgba = tmp[1].substring(0, tmp[1].length-1).split(',');
if(tmp[0]=='rgb' && rgba.length==3 || tmp[0]=='rgba' && rgba.length==4)
{
int val = 0;
for(int i=0; i<3; i++)
val = val + (int.parse(rgba[i])<<((2-i)*8));
double alpha = 1;
if(rgba.length==4)
alpha = double.parse(rgba[3]);
if(alpha==1.0)
return Colors.IntToColor(val);
return TAlphaColor(val, (alpha*255).round());
}
}
LPPTR src = LPPTR(data);
if(src[0]==CHAR.HASH || src[1]==CHAR.x)
{
var res = 0;
var inv = src.code!=CHAR.HASH;
src.ptr+= src.code==CHAR.HASH? 1 : 2;
if(src.eof)
return def;
int ptr = src.ptr;
do
{
int? val = SysUtils.CharToHex(src.code);
if(val==null)
return def;
res<<=4;
res+=val;
}
while(src.next());
if(src.ptr-ptr==3) // expand 24 bit
res = ((res&0x0f00)<<12) + ((res&0x0f00)<<8)+
((res&0x00f0)<<8) + ((res&0xf0)<<4)+
((res&0x0f)<<4) + (res&0x0f);
if(inv)
res = ((res&0xff)<<16) + (res&0xff00) + ((res&0xff0000)>>16);
return Colors.IntToColor(res);
}
return Colors.StringToColor(data, lng) ?? def;
}