librnr  1.14.5
RoadNarrows Robotics Common Library 1
color.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 /*! \file
3  *
4  * \brief Non-GUI color systems.
5  *
6  * Supported Color Systems:
7  * - ANSI escape sequence colors.
8  *
9  * \pkgsynopsis
10  * RoadNarrows Robotics Common Library 1
11  *
12  * \pkgcomponent{Library}
13  * librnr
14  *
15  * \pkgfile{rnr/color.h}
16  *
17  * \author Robin Knight (robin.knight@roadnarrows.com)
18  *
19  * \pkgcopyright{2013-2018,RoadNarrows LLC.,http://www.roadnarrows.com}
20  *
21  * \license{MIT}
22  *
23  * \EulaBegin
24  * See the README and EULA files for any copyright and licensing information.
25  * \EulaEnd
26  */
27 ////////////////////////////////////////////////////////////////////////////////
28 
29 #ifndef _RNR_COLOR_H
30 #define _RNR_COLOR_H
31 
32 #include <stdio.h>
33 
34 #include "rnr/rnrconfig.h"
35 
37 
38 /*!
39  * \defgroup rnr_color_ansi
40  *
41  * ANSI escape color codes.
42  *
43  * \{
44  */
45 
46 #define ANSI_CSI "\033[" ///< Control Sequence Introducer
47 
48 //
49 // Start, separator, and end color escape sequences.
50 //
51 #define ANSI_COLOR_PRE ANSI_CSI ///< color escape sequence prefix
52 #define ANSI_COLOR_SEP ";" ///< color values separator
53 #define ANSI_COLOR_MODE "m" ///< color mode
54 
55 //
56 // Select Graphic Rendition text attribute codes.
57 //
58 #define ANSI_SGR_TEXT_NORMAL "0" ///< color normal and reset to default
59 #define ANSI_SGR_TEXT_BOLD "1" ///< bold or increase intensity
60 #define ANSI_SGR_TEXT_UNDERSCORE "4" ///< underscore
61 #define ANSI_SGR_TEXT_BLINK "5" ///< slow blink
62 #define ANSI_SGR_TEXT_REVERSE "7" ///< swap fg and bg colors
63 
64 //
65 // Select Graphic Rendition foreground color codes.
66 //
67 #define ANSI_SGR_FG_COLOR_BLACK "30" ///< normal black
68 #define ANSI_SGR_FG_COLOR_RED "31" ///< normal red
69 #define ANSI_SGR_FG_COLOR_GREEN "32" ///< normal green
70 #define ANSI_SGR_FG_COLOR_YELLOW "33" ///< normal yellow (brown)
71 #define ANSI_SGR_FG_COLOR_BLUE "34" ///< normal blue
72 #define ANSI_SGR_FG_COLOR_MAGENTA "35" ///< normal magenta
73 #define ANSI_SGR_FG_COLOR_CYAN "36" ///< normal cyan
74 #define ANSI_SGR_FG_COLOR_WHITE "37" ///< normal white (gray)
75 
76 //
77 // Select Graphic Rendition background color codes.
78 //
79 #define ANSI_SGR_BG_COLOR_BLACK "40" ///< normal black
80 #define ANSI_SGR_BG_COLOR_RED "41" ///< normal red
81 #define ANSI_SGR_BG_COLOR_GREEN "42" ///< normal green
82 #define ANSI_SGR_BG_COLOR_YELLOW "43" ///< normal yellow (brown)
83 #define ANSI_SGR_BG_COLOR_BLUE "44" ///< normal blue
84 #define ANSI_SGR_BG_COLOR_MAGENTA "45" ///< normal magenta
85 #define ANSI_SGR_BG_COLOR_CYAN "46" ///< normal cyan
86 #define ANSI_SGR_BG_COLOR_WHITE "47" ///< normal white (gray)
87 
88 /*!
89  * \brief Macro to build color escape sequence string.
90  *
91  * \param _attr Text attribute.
92  * \param _fg Foreground color.
93  * \param _bg Background color.
94  */
95 #define ANSI_COLOR(_attr, _fg, _bg) \
96  ANSI_COLOR_PRE _attr ANSI_COLOR_SEP _fg ANSI_COLOR_SEP _bg ANSI_COLOR_MODE
97 
98 /*!
99  * \brief Macro to build normal foreground color escape sequence string.
100  *
101  * Background color is left at current setting.
102  *
103  * \param _fg Foreground color.
104  */
105 #define ANSI_FG_COLOR(_fg) \
106  ANSI_COLOR_PRE ANSI_SGR_TEXT_NORMAL ANSI_COLOR_SEP _fg ANSI_COLOR_MODE
107 
108 /*!
109  * \brief Macro to build bright foreground color escape sequence string.
110  *
111  * Background color is left at current setting.
112  *
113  * \param _fg Foreground color.
114  */
115 #define ANSI_FG_BRIGHT_COLOR(_fg) \
116  ANSI_COLOR_PRE ANSI_SGR_TEXT_BOLD ANSI_COLOR_SEP _fg ANSI_COLOR_MODE
117 
118 /*!
119  * \brief Macro to build foreground color with the given text attribute
120  * escape sequence string.
121  *
122  * Background color is left at current setting.
123  *
124  * \param _attr Text attribute.
125  * \param _fg Foreground color.
126  */
127 #define ANSI_FG_ATTR_COLOR(_attr, _fg) \
128  ANSI_COLOR_PRE _attr ANSI_COLOR_SEP _fg ANSI_COLOR_MODE
129 
130 /*!
131  * \brief Macro to build background color escape sequence string.
132  *
133  * Foreground color is left at current setting.
134  *
135  * \param _bg Foreground color.
136  */
137 #define ANSI_BG_COLOR(_bg) ANSI_COLOR_PRE _bg ANSI_COLOR_MODE
138 
139 /*!
140  * \brief Macro to build normal color escape sequence string.
141  *
142  * Actual colors are dependent on the terminal emulator.
143  *
144  * \param _fg Foreground color.
145  * \param _bg Background color.
146  */
147 #define ANSI_NORMAL_COLOR(_fg, _bg) ANSI_COLOR(ANSI_SGR_TEXT_NORMAL, _fg, _bg)
148 
149 /*!
150  * \brief Macro to build bright color escape sequence string.
151  *
152  * Actual colors are dependent on the terminal emulator.
153  *
154  * \param _fg Foreground color.
155  * \param _bg Background color.
156  */
157 #define ANSI_BRIGHT_COLOR(_fg, _bg) ANSI_COLOR(ANSI_SGR_TEXT_BOLD, _fg, _bg)
158 
159 //
160 // Handy, common color escape sequences.
161 //
162 
163 /*!
164  * \brief color reset to default
165  */
166 #define ANSI_COLOR_RESET \
167  ANSI_CSI ANSI_SGR_TEXT_NORMAL ANSI_COLOR_MODE
168 
169 /*!
170  * \brief Foreground colors escape sequences.
171  * \{
172  */
173 #define ANSI_FG_BLACK ANSI_FG_COLOR(ANSI_SGR_FG_COLOR_BLACK)
174 #define ANSI_FG_RED ANSI_FG_COLOR(ANSI_SGR_FG_COLOR_RED)
175 #define ANSI_FG_GREEN ANSI_FG_COLOR(ANSI_SGR_FG_COLOR_GREEN)
176 #define ANSI_FG_YELLOW ANSI_FG_COLOR(ANSI_SGR_FG_COLOR_YELLOW)
177 #define ANSI_FG_BLUE ANSI_FG_COLOR(ANSI_SGR_FG_COLOR_BLUE)
178 #define ANSI_FG_MAGENTA ANSI_FG_COLOR(ANSI_SGR_FG_COLOR_MAGENTA)
179 #define ANSI_FG_CYAN ANSI_FG_COLOR(ANSI_SGR_FG_COLOR_CYAN)
180 #define ANSI_FG_WHITE ANSI_FG_COLOR(ANSI_SGR_FG_COLOR_WHITE)
181 
182 #define ANSI_FG_BRIGHT_BLACK ANSI_FG_BRIGHT_COLOR(ANSI_SGR_FG_COLOR_BLACK)
183 #define ANSI_FG_BRIGHT_RED ANSI_FG_BRIGHT_COLOR(ANSI_SGR_FG_COLOR_RED)
184 #define ANSI_FG_BRIGHT_GREEN ANSI_FG_BRIGHT_COLOR(ANSI_SGR_FG_COLOR_GREEN)
185 #define ANSI_FG_BRIGHT_YELLOW ANSI_FG_BRIGHT_COLOR(ANSI_SGR_FG_COLOR_YELLOW)
186 #define ANSI_FG_BRIGHT_BLUE ANSI_FG_BRIGHT_COLOR(ANSI_SGR_FG_COLOR_BLUE)
187 #define ANSI_FG_BRIGHT_MAGENTA ANSI_FG_BRIGHT_COLOR(ANSI_SGR_FG_COLOR_MAGENTA)
188 #define ANSI_FG_BRIGHT_CYAN ANSI_FG_BRIGHT_COLOR(ANSI_SGR_FG_COLOR_CYAN)
189 #define ANSI_FG_BRIGHT_WHITE ANSI_FG_BRIGHT_COLOR(ANSI_SGR_FG_COLOR_WHITE)
190 /*! \} */
191 
192 /*!
193  * \brief Background colors escape sequences.
194  * \{
195  */
196 #define ANSI_BG_BLACK ANSI_FG_COLOR(ANSI_SGR_BG_COLOR_BLACK)
197 #define ANSI_BG_RED ANSI_FG_COLOR(ANSI_SGR_BG_COLOR_RED)
198 #define ANSI_BG_GREEN ANSI_FG_COLOR(ANSI_SGR_BG_COLOR_GREEN)
199 #define ANSI_BG_YELLOW ANSI_FG_COLOR(ANSI_SGR_BG_COLOR_YELLOW)
200 #define ANSI_BG_BLUE ANSI_FG_COLOR(ANSI_SGR_BG_COLOR_BLUE)
201 #define ANSI_BG_MAGENTA ANSI_FG_COLOR(ANSI_SGR_BG_COLOR_MAGENTA)
202 #define ANSI_BG_CYAN ANSI_FG_COLOR(ANSI_SGR_BG_COLOR_CYAN)
203 #define ANSI_BG_WHITE ANSI_FG_COLOR(ANSI_SGR_BG_COLOR_WHITE)
204 /*! \} */
205 
206 
207 //-----------------------------------------------------------------------------
208 // Color Functions
209 //-----------------------------------------------------------------------------
210 
211 /*!
212  * \brief Set foreground or background color.
213  *
214  * The color is in effect until a new foreground/background color is specified
215  * or the colors are reset to default values.
216  *
217  * \param sColor ANSI SGR color string.
218  */
219 INLINE_IN_H void set_color(const char *sColor)
220 {
221  printf("%s%s%s", ANSI_COLOR_PRE, sColor, ANSI_COLOR_MODE);
222 }
223 
224 /*!
225  * \brief Reset foreground and background colors to defaults.
226  */
228 {
229  printf("%s", ANSI_COLOR_RESET);
230 }
231 
232 /*!
233  * \brief Print output in the specified foreground color.
234  *
235  * After printing, the colors are reset to defaults. Subsequent output will be
236  * in the default color.
237  *
238  * \param sFgColor ANSI SGR foreground color string.
239  * \param sFmt Format string following printf(3) syntax.
240  * \param ... Variable argument list to print.
241  *
242  * \return
243  * On success, returns the number of characters printed sans any color escape
244  * sequents.\n
245  * On output error, a negative value is returned.
246  */
247 extern int colorprintf(const char *sFgColor, const char *sFmt, ...);
248 
249 /*
250  * \}
251  */
252 
254 
255 
256 #endif // _RNR_COLOR_H
int colorprintf(const char *sFgColor, const char *sFmt,...)
Print output in the specified foreground color.
Definition: color.c:32
INLINE_IN_H void set_color(const char *sColor)
Set foreground or background color.
Definition: color.h:219
INLINE_IN_H void reset_colors()
Reset foreground and background colors to defaults.
Definition: color.h:227
#define ANSI_COLOR_MODE
color mode
Definition: color.h:53
#define ANSI_COLOR_RESET
color reset to default
Definition: color.h:166
#define C_DECLS_BEGIN
C declaration block begin in C.
Definition: rnrconfig.h:71
RoadNarrows Robotics common configuration file.
#define INLINE_IN_H
inline C funtion in C header
Definition: rnrconfig.h:157
#define C_DECLS_END
C declaration block end in C.
Definition: rnrconfig.h:72
#define ANSI_COLOR_PRE
color escape sequence prefix
Definition: color.h:51