这是调用C语言自带函数,效率略慢

    1. #include <string.h> //调用 sprintf
    2. char str[8];
    3. char* str_num;//临时变量
    4. int get_color_PK(char* str,char* str_num,int co,int ko,int s){
    5. sprintf(str, "%08x", co);
    6. /* printf("co,color:%s\r\n", str); */
    7. /* int a = get_color_A(str,str_num); //传入一个颜色,一个临时变量 */
    8. int r = get_color_R(str,str_num);
    9. int g = get_color_G(str,str_num);
    10. int b = get_color_B(str,str_num);
    11. sprintf(str, "%08x", ko);
    12. /* printf("ko,color:%s\r\n", str1); */
    13. /* int a2 = get_color_A(str,str_num); //传入一个颜色,一个临时变量 */
    14. int r2 = get_color_R(str,str_num);
    15. int g2 = get_color_G(str,str_num);
    16. int b2 = get_color_B(str,str_num);
    17. if ( abs(r2-r)>s || abs(g2-g)>s || abs(b2-b)>s ) { //不能加入透明度判断
    18. /* printf("不匹配\r\n"); */
    19. return -1; //不匹配
    20. }
    21. /* printf("匹配\r\n"); */
    22. return 0;
    23. }
    24. int get_color_A(char* str,char* str_num){
    25. char a[]={str[0],str[1],'\0'}; //透明度 如果不加'\0' 后续提取会出现连串问题
    26. int a1 = strtol(a, &str_num, 16);
    27. /* printf("A:%d\r\n", a1); */
    28. return a1;
    29. }
    30. int get_color_R(char* str,char* str_num){ //转16进制字符串 支持双字节32位和四字节64位
    31. char a[]={str[2],str[3],'\0'};
    32. int a1 = strtol(a, &str_num, 16);
    33. /* printf("R:%d\r\n", a1); */
    34. return a1;
    35. }
    36. int get_color_G(char* str,char* str_num){
    37. char a[]={str[4],str[5],'\0'};
    38. int a1 = strtol(a, &str_num, 16);
    39. /* printf("G:%d\r\n", a1); */
    40. return a1;
    41. }
    42. int get_color_B(char* str,char* str_num){
    43. char a[]={str[6],str[7],'\0'};
    44. int a1 = strtol(a, &str_num, 16);
    45. /* printf("B:%d\r\n", a1); */
    46. return a1;
    47. }

    直接学习计算颜色值,效率提高3倍

    1. #include <stdlib.h>//调用abs函数
    2. int color_RGB(int r,int g,int b,int rgb,int degree){//直接学习计算颜色值,效率提高3倍
    3. int RGB,r3,g3,b3;
    4. RGB=16777216;
    5. if (rgb>-1 && rgb<RGB){
    6. RGB=rgb;
    7. }else{
    8. RGB+=rgb;
    9. }
    10. r3 = RGB/65536;
    11. g3 = RGB%65536/256;
    12. b3 = RGB%256;
    13. double s = floor(0xff * (100 - degree) * 0.01);
    14. //printf("s = %lf\n",s);
    15. if ( abs(r-r3)>s || abs(g-g3)>s || abs(b-b3)>s ) {
    16. //不满足
    17. return 0;
    18. } //比色 1成功 0失败
    19. return 1;
    20. }