-
Notifications
You must be signed in to change notification settings - Fork 3
/
rgb2cref.cpp
80 lines (73 loc) · 2.17 KB
/
rgb2cref.cpp
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
//********************************************************************************
// Copyright (c) 2020 Daniel D Miller
// License: freeware
//
// rgb2cref.cpp - convert RGB value into hex/decimal form
// This is typically to aid in updating syntax-highlighting values for
// programming editors
//
// build: g++ -Wall -O2 -s rgb2cref.cpp -o rgb2cref.exe
//********************************************************************************
#include <windows.h>
#include <stdio.h>
#ifdef _lint
#include <stdlib.h> // strtoul()
#endif
//lint -esym(818, argv)
//lint -e10 Expecting '}'
typedef unsigned int uint ;
typedef unsigned char u8 ;
typedef union ul2uc_u {
uint ul ;
u8 uc[4] ;
} ul2uc_t ;
//********************************************************************************
void usage(void)
{
puts("Usage: rgb2cref [ 0xRRGGBB or rrr.ggg.bbb ]") ;
puts("rgb2cref 0xRRGGBB outputs equivalent color in rrr.ggg.bbb") ;
puts("rgb2cref rrr.ggg.bbb outputs equivalent color in 0xRRGGBB") ;
}
//********************************************************************************
int main(int argc, char **argv)
{
ul2uc_t uconv ;
if (argc == 1) {
usage() ;
return 1;
}
bool to_rgb = false ;
char *instr = argv[1] ;
if (strncasecmp(instr, "0x", 2) == 0) {
to_rgb = true ;
}
// now convert and display the result
if (to_rgb) {
uconv.ul = (uint) strtoul(instr, NULL, 16) ;
} else {
uint utemp ;
char *cptr ;
uconv.ul = 0 ;
utemp = (uint) strtoul(instr, &cptr, 10) ;
if (cptr == NULL) {
usage() ;
return 1;
}
cptr++ ; // skip the dot
uconv.uc[2] = (u8) utemp ;
utemp = (uint) strtoul(cptr, &cptr, 10) ;
if (cptr == NULL) {
usage() ;
return 1;
}
cptr++ ; // skip the dot
uconv.uc[1] = (u8) utemp ;
utemp = (uint) strtoul(cptr, NULL, 10) ;
uconv.uc[0] = (u8) utemp ;
}
printf("0x%06X (%10u)= %u.%u.%u\n", uconv.ul, uconv.ul,
(uint) uconv.uc[2],
(uint) uconv.uc[1],
(uint) uconv.uc[0]) ;
return 0;
}