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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.
  80. */
  81. #define ANSICOL_DEFAULT( basic ) ANSICOL_DEFINE ( (basic), ANSICOL_NO_RGB )
  82. /** ANSI colour definitions */
  83. static uint32_t ansicols[] = {
  84. [COLOR_BLACK] = ANSICOL_DEFAULT ( COLOR_BLACK ),
  85. [COLOR_RED] = ANSICOL_DEFAULT ( COLOR_RED ),
  86. [COLOR_GREEN] = ANSICOL_DEFAULT ( COLOR_GREEN ),
  87. [COLOR_YELLOW] = ANSICOL_DEFAULT ( COLOR_YELLOW ),
  88. [COLOR_BLUE] = ANSICOL_DEFAULT ( COLOR_BLUE ),
  89. [COLOR_MAGENTA] = ANSICOL_DEFAULT ( COLOR_MAGENTA ),
  90. [COLOR_CYAN] = ANSICOL_DEFAULT ( COLOR_CYAN ),
  91. [COLOR_WHITE] = ANSICOL_DEFAULT ( COLOR_WHITE ),
  92. };
  93. /**
  94. * Define ANSI colour
  95. *
  96. * @v colour Colour index
  97. * @v basic Basic colour
  98. * @v rgb 24-bit RGB value (or ANSICOL_NO_RGB)
  99. * @ret rc Return status code
  100. */
  101. int ansicol_define ( unsigned int colour, unsigned int basic, uint32_t rgb ) {
  102. uint32_t ansicol;
  103. /* Fail if colour index is out of range */
  104. if ( colour >= ( sizeof ( ansicols ) / sizeof ( ansicols[0] ) ) )
  105. return -EINVAL;
  106. /* Update colour definition */
  107. ansicol = ANSICOL_DEFINE ( basic, rgb );
  108. ansicols[colour] = ansicol;
  109. DBGC ( &ansicols[0], "ANSICOL redefined colour %d as basic %d RGB "
  110. "%#06lx%s\n", colour, ANSICOL_BASIC ( ansicol ),
  111. ANSICOL_RGB ( ansicol ),
  112. ( ( ansicol & ANSICOL_NO_RGB ) ? " [norgb]" : "" ) );
  113. return 0;
  114. }
  115. /**
  116. * Set ANSI colour (using colour definitions)
  117. *
  118. * @v colour Colour index
  119. * @v which Foreground/background selector
  120. */
  121. void ansicol_set ( unsigned int colour, unsigned int which ) {
  122. uint32_t ansicol;
  123. unsigned int basic;
  124. /* Use default colour if colour index is out of range */
  125. if ( colour < ( sizeof ( ansicols ) / sizeof ( ansicols[0] ) ) ) {
  126. ansicol = ansicols[colour];
  127. } else {
  128. ansicol = ANSICOL_DEFINE ( COLOUR_DEFAULT, ANSICOL_NO_RGB );
  129. }
  130. /* If basic colour is out of range, use the default colour */
  131. basic = ANSICOL_BASIC ( ansicol );
  132. if ( basic >= 10 )
  133. basic = COLOR_DEFAULT;
  134. /* Set basic colour first */
  135. printf ( CSI "%c%dm", which, basic );
  136. /* Set 24-bit RGB colour, if applicable */
  137. if ( ! ( ansicol & ANSICOL_NO_RGB ) ) {
  138. printf ( CSI "%c8;2;%d;%d;%dm", which, ANSICOL_RED ( ansicol ),
  139. ANSICOL_GREEN ( ansicol ), ANSICOL_BLUE ( ansicol ) );
  140. }
  141. }