这是调用C语言自带函数,效率略慢
#include <string.h> //调用 sprintf
char str[8];
char* str_num;//临时变量
int get_color_PK(char* str,char* str_num,int co,int ko,int s){
sprintf(str, "%08x", co);
/* printf("co,color:%s\r\n", str); */
/* int a = get_color_A(str,str_num); //传入一个颜色,一个临时变量 */
int r = get_color_R(str,str_num);
int g = get_color_G(str,str_num);
int b = get_color_B(str,str_num);
sprintf(str, "%08x", ko);
/* printf("ko,color:%s\r\n", str1); */
/* int a2 = get_color_A(str,str_num); //传入一个颜色,一个临时变量 */
int r2 = get_color_R(str,str_num);
int g2 = get_color_G(str,str_num);
int b2 = get_color_B(str,str_num);
if ( abs(r2-r)>s || abs(g2-g)>s || abs(b2-b)>s ) { //不能加入透明度判断
/* printf("不匹配\r\n"); */
return -1; //不匹配
}
/* printf("匹配\r\n"); */
return 0;
}
int get_color_A(char* str,char* str_num){
char a[]={str[0],str[1],'\0'}; //透明度 如果不加'\0' 后续提取会出现连串问题
int a1 = strtol(a, &str_num, 16);
/* printf("A:%d\r\n", a1); */
return a1;
}
int get_color_R(char* str,char* str_num){ //转16进制字符串 支持双字节32位和四字节64位
char a[]={str[2],str[3],'\0'};
int a1 = strtol(a, &str_num, 16);
/* printf("R:%d\r\n", a1); */
return a1;
}
int get_color_G(char* str,char* str_num){
char a[]={str[4],str[5],'\0'};
int a1 = strtol(a, &str_num, 16);
/* printf("G:%d\r\n", a1); */
return a1;
}
int get_color_B(char* str,char* str_num){
char a[]={str[6],str[7],'\0'};
int a1 = strtol(a, &str_num, 16);
/* printf("B:%d\r\n", a1); */
return a1;
}
直接学习计算颜色值,效率提高3倍
#include <stdlib.h>//调用abs函数
int color_RGB(int r,int g,int b,int rgb,int degree){//直接学习计算颜色值,效率提高3倍
int RGB,r3,g3,b3;
RGB=16777216;
if (rgb>-1 && rgb<RGB){
RGB=rgb;
}else{
RGB+=rgb;
}
r3 = RGB/65536;
g3 = RGB%65536/256;
b3 = RGB%256;
double s = floor(0xff * (100 - degree) * 0.01);
//printf("s = %lf\n",s);
if ( abs(r-r3)>s || abs(g-g3)>s || abs(b-b3)>s ) {
//不满足
return 0;
} //比色 1成功 0失败
return 1;
}