You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ansicoldef.c 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  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.
  8. *
  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.
  13. *
  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
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <ipxe/ansiesc.h>
  23. #include <ipxe/ansicol.h>
  24. #include <config/colour.h>
  25. /** @file
  26. *
  27. * ANSI colour definitions
  28. *
  29. */
  30. /**
  31. * Construct ANSI colour definition
  32. *
  33. * @v basic Basic colour
  34. * @v rgb 24-bit RGB value (or ANSICOL_NO_RGB)
  35. * @ret ansicol ANSI colour definition
  36. */
  37. #define ANSICOL_DEFINE( basic, rgb ) ( ( (basic) << 28 ) | (rgb) )
  38. /**
  39. * Extract basic colour from ANSI colour definition
  40. *
  41. * @v ansicol ANSI colour definition
  42. * @ret basic Basic colour
  43. */
  44. #define ANSICOL_BASIC( ansicol ) ( (ansicol) >> 28 )
  45. /**
  46. * Extract 24-bit RGB value from ANSI colour definition
  47. *
  48. * @v ansicol ANSI colour definition
  49. * @ret rgb 24-bit RGB value
  50. */
  51. #define ANSICOL_RGB( ansicol ) ( ( (ansicol) >> 0 ) & 0xffffffUL )
  52. /**
  53. * Extract 24-bit RGB value red component from ANSI colour definition
  54. *
  55. * @v ansicol ANSI colour definition
  56. * @ret red Red component
  57. */
  58. #define ANSICOL_RED( ansicol ) ( ( (ansicol) >> 16 ) & 0xff )
  59. /**
  60. * Extract 24-bit RGB value green component from ANSI colour definition
  61. *
  62. * @v ansicol ANSI colour definition
  63. * @ret green Green component
  64. */
  65. #define ANSICOL_GREEN( ansicol ) ( ( (ansicol) >> 8 ) & 0xff )
  66. /**
  67. * Extract 24-bit RGB value blue component from ANSI colour definition
  68. *
  69. * @v ansicol ANSI colour definition
  70. * @ret blue Blue component
  71. */
  72. #define ANSICOL_BLUE( ansicol ) ( ( (ansicol) >> 0 ) & 0xff )
  73. /**
  74. * Construct default ANSI colour definition
  75. *
  76. * @v basic Basic colour
  77. * @ret ansicol ANSI colour definition
  78. *
  79. * Colours default to being just a basic colour. If the colour
  80. * matches the normal UI text background colour, then its basic colour
  81. * value is set to @c ANSICOL_MAGIC.
  82. */
  83. #define ANSICOL_DEFAULT( basic ) \
  84. ANSICOL_DEFINE ( ( ( (basic) == COLOR_NORMAL_BG ) ? \
  85. ANSICOL_MAGIC : (basic) ), \
  86. ANSICOL_NO_RGB )
  87. /** ANSI colour definitions */
  88. static uint32_t ansicols[] = {
  89. [COLOR_BLACK] = ANSICOL_DEFAULT ( COLOR_BLACK ),
  90. [COLOR_RED] = ANSICOL_DEFAULT ( COLOR_RED ),
  91. [COLOR_GREEN] = ANSICOL_DEFAULT ( COLOR_GREEN ),
  92. [COLOR_YELLOW] = ANSICOL_DEFAULT ( COLOR_YELLOW ),
  93. [COLOR_BLUE] = ANSICOL_DEFAULT ( COLOR_BLUE ),
  94. [COLOR_MAGENTA] = ANSICOL_DEFAULT ( COLOR_MAGENTA ),
  95. [COLOR_CYAN] = ANSICOL_DEFAULT ( COLOR_CYAN ),
  96. [COLOR_WHITE] = ANSICOL_DEFAULT ( COLOR_WHITE ),
  97. };
  98. /** Magic basic colour */
  99. static uint8_t ansicol_magic = COLOR_NORMAL_BG;
  100. /**
  101. * Define ANSI colour
  102. *
  103. * @v colour Colour index
  104. * @v basic Basic colour
  105. * @v rgb 24-bit RGB value (or ANSICOL_NO_RGB)
  106. * @ret rc Return status code
  107. */
  108. int ansicol_define ( unsigned int colour, unsigned int basic, uint32_t rgb ) {
  109. uint32_t ansicol;
  110. /* Fail if colour index is out of range */
  111. if ( colour >= ( sizeof ( ansicols ) / sizeof ( ansicols[0] ) ) )
  112. return -EINVAL;
  113. /* Update colour definition */
  114. ansicol = ANSICOL_DEFINE ( basic, rgb );
  115. ansicols[colour] = ansicol;
  116. DBGC ( &ansicols[0], "ANSICOL redefined colour %d as basic %d RGB "
  117. "%#06lx%s\n", colour, ANSICOL_BASIC ( ansicol ),
  118. ANSICOL_RGB ( ansicol ),
  119. ( ( ansicol & ANSICOL_NO_RGB ) ? " [norgb]" : "" ) );
  120. return 0;
  121. }
  122. /**
  123. * Set ANSI colour (using colour definitions)
  124. *
  125. * @v colour Colour index
  126. * @v which Foreground/background selector
  127. */
  128. void ansicol_set ( unsigned int colour, unsigned int which ) {
  129. uint32_t ansicol;
  130. unsigned int basic;
  131. /* Use default colour if colour index is out of range */
  132. if ( colour < ( sizeof ( ansicols ) / sizeof ( ansicols[0] ) ) ) {
  133. ansicol = ansicols[colour];
  134. } else {
  135. ansicol = ANSICOL_DEFINE ( COLOUR_DEFAULT, ANSICOL_NO_RGB );
  136. }
  137. /* If basic colour is out of range, use the magic colour */
  138. basic = ANSICOL_BASIC ( ansicol );
  139. if ( basic >= 10 )
  140. basic = ansicol_magic;
  141. /* Set basic colour first */
  142. printf ( CSI "%c%dm", which, basic );
  143. /* Set 24-bit RGB colour, if applicable */
  144. if ( ! ( ansicol & ANSICOL_NO_RGB ) ) {
  145. printf ( CSI "%c8;2;%d;%d;%dm", which, ANSICOL_RED ( ansicol ),
  146. ANSICOL_GREEN ( ansicol ), ANSICOL_BLUE ( ansicol ) );
  147. }
  148. }
  149. /**
  150. * Reset magic colour
  151. *
  152. */
  153. void ansicol_reset_magic ( void ) {
  154. /* Set to the compile-time default background colour */
  155. ansicol_magic = COLOR_NORMAL_BG;
  156. }
  157. /**
  158. * Set magic colour to transparent
  159. *
  160. */
  161. void ansicol_set_magic_transparent ( void ) {
  162. /* Set to the console default colour (which will give a
  163. * transparent background on the framebuffer console).
  164. */
  165. ansicol_magic = COLOR_DEFAULT;
  166. }