comparison miscutil/fc-rgbconv.c @ 961:813287f7169c

fc-rgbconv utility written and added
author Mychaela Falconia <falcon@ivan.Harhan.ORG>
date Fri, 06 Nov 2015 19:31:42 +0000
parents
children
comparison
equal deleted inserted replaced
960:120148429b9f 961:813287f7169c
1 /*
2 * This utility is an aid for phone UI development. The color LCDs in the
3 * phones targeted by FreeCalypso implement 16-bit color in RGB 5:6:5 format,
4 * but these 16-bit color words are not particularly intuitive in raw hex form.
5 * In contrast, a 24-bit RGB 8:8:8 color given in RRGGBB hex form is quite
6 * intuitive, at least to those who understand RGB.
7 *
8 * This utility performs the conversion. It takes a single command line
9 * argument that must be either 4 or 6 hex digits. If the argument is 4 hex
10 * digits, it is interpreted as RGB 5:6:5 and converted into RGB 8:8:8.
11 * If the argument is 6 hex digits, it is interpreted as RGB 8:8:8 and
12 * converted into RGB 5:6:5. The output of the conversion is emitted on
13 * stdout as 4 or 6 hex digits.
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <ctype.h>
19 #include <string.h>
20
21 is_arg_all_hex(str)
22 char *str;
23 {
24 char *cp;
25 int c;
26
27 for (cp = str; c = *cp++; )
28 if (!isxdigit(c))
29 return(0);
30 return(1);
31 }
32
33 convert_565_to_888(in)
34 unsigned in;
35 {
36 unsigned r, g, b;
37
38 r = in >> 11;
39 g = (in >> 5) & 0x3F;
40 b = in & 0x1F;
41 printf("%02X%02X%02X\n", r << 3, g << 2, b << 3);
42 }
43
44 convert_888_to_565(in)
45 unsigned in;
46 {
47 unsigned r, g, b;
48
49 r = (in >> 16) >> 3;
50 g = ((in >> 8) & 0xFF) >> 2;
51 b = (in & 0xFF) >> 3;
52 printf("%04X\n", (r << 11) | (g << 5) | b);
53 }
54
55 main(argc, argv)
56 char **argv;
57 {
58 unsigned in;
59
60 if (argc != 2) {
61 usage: fprintf(stderr,
62 "please specify one 4-digit or 6-digit hex RGB value\n");
63 exit(1);
64 }
65 if (!is_arg_all_hex(argv[1]))
66 goto usage;
67 in = strtoul(argv[1], 0, 16);
68 switch (strlen(argv[1])) {
69 case 4:
70 convert_565_to_888(in);
71 exit(0);
72 case 6:
73 convert_888_to_565(in);
74 exit(0);
75 }
76 goto usage;
77 }