1 /****************************************************************************
2 * Copyright 2018-2020,2021 Thomas E. Dickey *
3 * Copyright 2011-2012,2016 Free Software Foundation, Inc. *
4 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29 /*
30 * $Id: color_name.h,v 1.9 2021/04/24 23:25:29 tom Exp $
31 */
32
33 #ifndef __COLORNAME_H
34 #define __COLORNAME_H 1
35
36 #ifndef __TEST_PRIV_H
37 #include <test.priv.h>
38 #endif
39
40 static NCURSES_CONST char *const the_color_names[] =
41 {
42 "black",
43 "red",
44 "green",
45 "yellow",
46 "blue",
47 "magenta",
48 "cyan",
49 "white",
50 "BLACK",
51 "RED",
52 "GREEN",
53 "YELLOW",
54 "BLUE",
55 "MAGENTA",
56 "CYAN",
57 "WHITE"
58 };
59
60 #ifdef NEED_COLOR_CODE
61 static int
color_code(const char * color)62 color_code(const char *color)
63 {
64 int result = 0;
65 char *endp = 0;
66 size_t n;
67
68 if ((result = (int) strtol(color, &endp, 0)) >= 0
69 && (endp == 0 || *endp == 0)) {
70 ;
71 } else if (!strcmp(color, "default")) {
72 result = -1;
73 } else {
74 for (n = 0; n < SIZEOF(the_color_names); ++n) {
75 if (!strcmp(the_color_names[n], color)) {
76 result = (int) n;
77 break;
78 }
79 }
80 }
81 return result;
82 }
83 #endif /* NEED_COLOR_CODE */
84
85 #ifdef NEED_COLOR_NAME
86 static const char *
color_name(int color)87 color_name(int color)
88 {
89 static char temp[20];
90 const char *result = 0;
91
92 if (color >= (int) SIZEOF(the_color_names)) {
93 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "%d", color);
94 result = temp;
95 } else if (color < 0) {
96 result = "default";
97 } else {
98 result = the_color_names[color];
99 }
100 return result;
101 }
102 #endif /* NEED_COLOR_NAME */
103
104 #endif /* __COLORNAME_H */
105