RGB和十六进制颜色值互转

昨天修改fcitx的颜色时发现了一个麻烦,fcitx配置文config中使用的颜色是RGB值的形式,而我目前只有十六进制值的。
因为当时已经是12点多了,学校已经断网了,不能上网找在线转换工具 ,我又没装GIMP,只好用C编写一个小工具了。
声明:此程序实用性并不大,本着折腾的精神,自己动手,丰衣足食。纯当练手啦。 :mrgreen:

源码下载
先贴出源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <stdio.h>
#include <stdlib.h>

typedef struct {
  int r;
  int g;
  int b;
} rgb ;

int
main()
{
  int rgb2hex(int,int,int);
  rgb hex2rgb(int);
  int check_rgb(int , int , int );
  int check_hex( int );
  void print_star(void);
  void print_title(void);
  void print_msg(void);
  int r,g,b;
  int hex_color;
  int op;
 
  print_title();

 while( ( (  op=getchar() )!= EOF  ) && op!='q')
  {


    switch (op)
    {
      case '1':
        printf("请输入R G B值:\n");
      scanf("%3d %3d %3d",&r,&g,&b);
      if(!check_rgb(r,g,b))
      {
        printf("错误的RGB颜色代码!\n");
        exit(-1);
      }
      printf("RGB颜色代码[%03d %03d %03d]转换为十六进制颜色值为:\n#",r,g,b);
      printf("%06x\n",rgb2hex(r,g,b));
      print_msg();
      break;
      case '2':
      printf("请输入十六进制颜色代码(不带#号):\n");
      scanf("%06x",&hex_color);
      if(!check_hex(hex_color))
          {
          printf("错误的十六进制颜色值!\n");
          exit(-1);
          }
      rgb color=hex2rgb(hex_color);
      printf("十六进制颜色值[#%06x]转换为RGB颜色值为:\n",hex_color);
      printf(" R   G   B  \n");
      printf("%03d %03d %03d\n",color.r , color.g , color.b );
      print_msg();
      break;
    }//end switch
  }//end while
      return 0;
}

//将RGB颜色转换为十六进制颜色
int
rgb2hex(int r,int g,int b)
{
  return r<<16 | g <<8 |b;
}

//将十六进制颜色转换为RGB颜色
rgb
hex2rgb(int hex)
{
  rgb color;
 color.r=( 0xff <<16 & hex ) >> 16;
 color.g=( 0xff <<8 & hex ) >> 8;
 color.b=0xff & hex;
return color;
}


int
check_rgb(int r,int g,int b)
{
  if( (r>=0 && r <=255 ) && ( g>=0 && g <= 255 ) && (b >=0 && b<=255) )
    return 1;
  else
    return 0;
}

int
check_hex(int hex)
{
  if((hex >= 0x0 ) && ( hex <= 0xffffff) )
    return 1;
  else
    return 0;
}

void
print_star(void)
{
  printf("#######################");
}

void
print_title(void)
{
  printf("                         ######################\n");
  print_star();
  printf("  RGB&十六进制颜色值互转  ");
  print_star();
  printf("\n");
  print_star();
  printf("         ————By 荒野无灯  ");
  print_star();
  printf("\n");
  printf("                         ######################\n");
  printf("请选择操作:\n");
  printf("[1]:RGB颜色转换为十六进制颜色    ");
  printf("[2]:十六进制颜色转换为RGB颜色    ");
  printf("[q]:退出程序\n");
}
 
void
print_msg(void)
{
  printf("请选择操作:\n");
  printf("[1]:RGB颜色转换为十六进制颜色     ");
  printf("[2]:十六进制颜色转换为RGB颜色     ");
  printf("[q]:退出程序\n");
}

源码下载:


C这东东的话,以前还真没怎么折腾过,呵呵,所以,现在要多多折腾。

1
gcc -o color.exe color.c

喜欢这篇文章吗?

请订阅本站 RSS feed填写您的邮件地址,订阅我们的精彩内容:

相关日志

回复 (2)

  1. 无冷  / 回复

    这个算法是从那里得到的

  2. 猪八戒  / 回复

    不太懂。

发表评论 修改评论取消编辑

允许使用的标签 - 您可以在评论中使用如下的 HTML 标签以及属性。

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 :wink:  :-|  :-x  :twisted:  :)  8-O  :(  :roll:  :-P  :oops:  :-o  :mrgreen:  :lol:  :idea:  :-D  :evil:  :cry:  8)  :arrow:  :-?  :?:  :!:

引用通告 (0)

› 尚无引用通告。

开灯