影子论坛
发新帖
- 手机CAPP
- 颜色转换
-
-
-
风的影子
2018-5-13
633
-
/*
将字符串颜色值转换为int类型(argb格式)
*/
int getColor(char *text)
{
int color=0;
int argb[4];
int start=0;
int i=0;
int hex=0; //颜色位数 有3 4 6 8
int len=strlen(text);
for(i=0;i<len;i++)
{
if(text[i]=='#')
{
start=i+1;
hex=len-start;
}
}
if(hex==3)
{
for(i=0;i<3;i++)
{
char c=text[start+i];
argb[0]=0xff;
if(c>='A'&&c<='F')
{
argb[i+1]=(c-'A'+10)*16;
}
else if(c>='a'&&c<='f')
{
argb[i+1]=(c-'a'+10)*16;
}
else if(c>='0'&&c<='9')
{
argb[i+1]=(c-'0')*16;
}
}
}
else if(hex==6)
{
argb[0]=0xff;
for(i=0;i<3;i++)
{
char c=text[start+i*2];
char c2=text[start+i*2+1];
if(c>='A'&&c<='F')
{
argb[i+1]=(c-'A'+10)<<4;
}
else if(c>='a'&&c<='f')
{
argb[i+1]=(c-'a'+10)<<4;
}
else if(c>='0'&&c<='9')
{
argb[i+1]=(c-'0')<<4;
}
if(c2>='A'&&c2<='F')
{
argb[i+1]|=(c2-'A'+10);
}
else if(c2>='a'&&c2<='f')
{
argb[i+1]|=(c2-'a'+10);
}
else if(c2>='0'&&c2<='9')
{
argb[i+1]|=(c2-'0');
}
}
}
else if(hex==4)
{
for(i=0;i<4;i++)
{
char c=text[start+i];
if(c>='A'&&c<='Z')
{
argb[i]=((c-'A')+10)*16;
}
else if(c>='a'&&c<='z')
{
argb[i]=(c-'a'+10)*16;
}
else if(c>='0'&&c<='9')
{
argb[i]=(c-'0')*16;
}
}
}
else if(hex==8)
{
for(i=0;i<4;i++)
{
char c=text[start+i*2];
char c2=text[start+i*2+1];
if(c>='A'&&c<='F')
{
argb[i]=(c-'A'+10)<<4;
}
else if(c>='a'&&c<='f')
{
argb[i]=(c-'a'+10)<<4;
}
else if(c>='0'&&c<='9')
{
argb[i]=(c-'0')<<4;
}
if(c2>='A'&&c2<='F')
{
argb[i]|=(c2-'A'+10);
}
else if(c2>='a'&&c2<='f')
{
argb[i]|=(c2-'a'+10);
}
else if(c2>='0'&&c2<='9')
{
argb[i]|=(c2-'0');
}
}
}
color=(argb[0]<<24)|(argb[1]<<16)|(argb[2]<<8)|argb[3];
return color;
}
返回
Processed Time:0.29325389862061