2 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
28 #include <ipxe/ansiesc.h>
29 #include <ipxe/ansicol.h>
30 #include <config/colour.h>
34 * ANSI colour definitions
39 * Construct ANSI colour definition
41 * @v basic Basic colour
42 * @v rgb 24-bit RGB value (or ANSICOL_NO_RGB)
43 * @ret ansicol ANSI colour definition
45 #define ANSICOL_DEFINE( basic, rgb ) ( ( (basic) << 28 ) | (rgb) )
48 * Extract basic colour from ANSI colour definition
50 * @v ansicol ANSI colour definition
51 * @ret basic Basic colour
53 #define ANSICOL_BASIC( ansicol ) ( (ansicol) >> 28 )
56 * Extract 24-bit RGB value from ANSI colour definition
58 * @v ansicol ANSI colour definition
59 * @ret rgb 24-bit RGB value
61 #define ANSICOL_RGB( ansicol ) ( ( (ansicol) >> 0 ) & 0xffffffUL )
64 * Extract 24-bit RGB value red component from ANSI colour definition
66 * @v ansicol ANSI colour definition
67 * @ret red Red component
69 #define ANSICOL_RED( ansicol ) ( ( (ansicol) >> 16 ) & 0xff )
72 * Extract 24-bit RGB value green component from ANSI colour definition
74 * @v ansicol ANSI colour definition
75 * @ret green Green component
77 #define ANSICOL_GREEN( ansicol ) ( ( (ansicol) >> 8 ) & 0xff )
80 * Extract 24-bit RGB value blue component from ANSI colour definition
82 * @v ansicol ANSI colour definition
83 * @ret blue Blue component
85 #define ANSICOL_BLUE( ansicol ) ( ( (ansicol) >> 0 ) & 0xff )
88 * Construct default ANSI colour definition
90 * @v basic Basic colour
91 * @ret ansicol ANSI colour definition
93 * Colours default to being just a basic colour. If the colour
94 * matches the normal UI text background colour, then its basic colour
95 * value is set to @c ANSICOL_MAGIC.
97 #define ANSICOL_DEFAULT( basic ) \
98 ANSICOL_DEFINE ( ( ( (basic) == COLOR_NORMAL_BG ) ? \
99 ANSICOL_MAGIC : (basic) ), \
102 /** ANSI colour definitions */
103 static uint32_t ansicols[] = {
104 [COLOR_BLACK] = ANSICOL_DEFAULT ( COLOR_BLACK ),
105 [COLOR_RED] = ANSICOL_DEFAULT ( COLOR_RED ),
106 [COLOR_GREEN] = ANSICOL_DEFAULT ( COLOR_GREEN ),
107 [COLOR_YELLOW] = ANSICOL_DEFAULT ( COLOR_YELLOW ),
108 [COLOR_BLUE] = ANSICOL_DEFAULT ( COLOR_BLUE ),
109 [COLOR_MAGENTA] = ANSICOL_DEFAULT ( COLOR_MAGENTA ),
110 [COLOR_CYAN] = ANSICOL_DEFAULT ( COLOR_CYAN ),
111 [COLOR_WHITE] = ANSICOL_DEFAULT ( COLOR_WHITE ),
114 /** Magic basic colour */
115 static uint8_t ansicol_magic = COLOR_NORMAL_BG;
120 * @v colour Colour index
121 * @v basic Basic colour
122 * @v rgb 24-bit RGB value (or ANSICOL_NO_RGB)
123 * @ret rc Return status code
125 int ansicol_define ( unsigned int colour, unsigned int basic, uint32_t rgb ) {
128 /* Fail if colour index is out of range */
129 if ( colour >= ( sizeof ( ansicols ) / sizeof ( ansicols[0] ) ) )
132 /* Update colour definition */
133 ansicol = ANSICOL_DEFINE ( basic, rgb );
134 ansicols[colour] = ansicol;
135 DBGC ( &ansicols[0], "ANSICOL redefined colour %d as basic %d RGB "
136 "%#06lx%s\n", colour, ANSICOL_BASIC ( ansicol ),
137 ANSICOL_RGB ( ansicol ),
138 ( ( ansicol & ANSICOL_NO_RGB ) ? " [norgb]" : "" ) );
144 * Set ANSI colour (using colour definitions)
146 * @v colour Colour index
147 * @v which Foreground/background selector
149 void ansicol_set ( unsigned int colour, unsigned int which ) {
153 /* Use default colour if colour index is out of range */
154 if ( colour < ( sizeof ( ansicols ) / sizeof ( ansicols[0] ) ) ) {
155 ansicol = ansicols[colour];
157 ansicol = ANSICOL_DEFINE ( COLOUR_DEFAULT, ANSICOL_NO_RGB );
160 /* If basic colour is out of range, use the magic colour */
161 basic = ANSICOL_BASIC ( ansicol );
163 basic = ansicol_magic;
165 /* Set basic colour first */
166 printf ( CSI "%c%dm", which, basic );
168 /* Set 24-bit RGB colour, if applicable */
169 if ( ! ( ansicol & ANSICOL_NO_RGB ) ) {
170 printf ( CSI "%c8;2;%d;%d;%dm", which, ANSICOL_RED ( ansicol ),
171 ANSICOL_GREEN ( ansicol ), ANSICOL_BLUE ( ansicol ) );
179 void ansicol_reset_magic ( void ) {
181 /* Set to the compile-time default background colour */
182 ansicol_magic = COLOR_NORMAL_BG;
186 * Set magic colour to transparent
189 void ansicol_set_magic_transparent ( void ) {
191 /* Set to the console default colour (which will give a
192 * transparent background on the framebuffer console).
194 ansicol_magic = COLOR_DEFAULT;