1/* @TAG(CUSTOM) */
2/* Copyright (c) 2012, Ryan Fox
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
19 * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22#pragma once
23
24#define COLOR_PREFIX     "\x1B["
25#define COLOR_SEP        ";"
26#define COLOR_SUFFIX     "m"
27
28#define COLOR_BLACK      "0"
29#define COLOR_RED        "1"
30#define COLOR_GREEN      "2"
31#define COLOR_YELLOW     "3"
32#define COLOR_BLUE       "4"
33#define COLOR_MAGENTA    "5"
34#define COLOR_CYAN       "6"
35#define COLOR_WHITE      "7"
36
37#define COLOR_FOREGROUND "3"
38#define COLOR_BACKGROUND "4"
39
40#define COLOR_SGR_CODE(number) COLOR_SEP #number
41#define COLOR_BOLD      COLOR_SGR_CODE(1)
42#define COLOR_ITALIC    COLOR_SGR_CODE(3)
43#define COLOR_UNDERLINE COLOR_SGR_CODE(4)
44#define COLOR_BLINK     COLOR_SGR_CODE(5)
45#define COLOR_REVERSE   COLOR_SGR_CODE(7)
46#define COLOR_INVISIBLE COLOR_SGR_CODE(8)
47
48#define COLOR_RESET COLOR_PREFIX "0" COLOR_SUFFIX
49
50/* Handle unspecified macro arguments. */
51#define COLOR_ ""
52
53/* Support up to 8 attributes */
54#define COLOR_ATTR_REC8(attr,...) COLOR_##attr
55#define COLOR_ATTR_REC7(attr,...) COLOR_##attr COLOR_ATTR_REC8(__VA_ARGS__)
56#define COLOR_ATTR_REC6(attr,...) COLOR_##attr COLOR_ATTR_REC7(__VA_ARGS__)
57#define COLOR_ATTR_REC5(attr,...) COLOR_##attr COLOR_ATTR_REC6(__VA_ARGS__)
58#define COLOR_ATTR_REC4(attr,...) COLOR_##attr COLOR_ATTR_REC5(__VA_ARGS__)
59#define COLOR_ATTR_REC3(attr,...) COLOR_##attr COLOR_ATTR_REC4(__VA_ARGS__)
60#define COLOR_ATTR_REC2(attr,...) COLOR_##attr COLOR_ATTR_REC3(__VA_ARGS__)
61#define COLOR_ATTR_REC(attr,...) COLOR_##attr COLOR_ATTR_REC2(__VA_ARGS__)
62
63#define ANSI_COLOR(forecolor,...)               \
64    COLOR_PREFIX                                \
65    COLOR_FOREGROUND COLOR_##forecolor          \
66    COLOR_ATTR_REC(__VA_ARGS__)                 \
67    COLOR_SUFFIX
68
69#define ANSI_COLOR2(forecolor,backcolor,...)    \
70    COLOR_PREFIX                                \
71    COLOR_FOREGROUND COLOR_##forecolor          \
72    COLOR_SEP                                   \
73    COLOR_BACKGROUND COLOR_##backcolor          \
74    COLOR_ATTR_REC(__VA_ARGS__)                 \
75    COLOR_SUFFIX
76
77#define COLORIZE(text,forecolor,...) ANSI_COLOR(forecolor,__VA_ARGS__) text COLOR_RESET
78#define COLORIZE2(text,forecolor,backcolor,...) ANSI_COLOR2(forecolor,backcolor,__VA_ARGS__) text COLOR_RESET
79
80