palette_builder.dart 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import 'package:flutter/widgets.dart';
  2. import 'package:xterm/ui/terminal_theme.dart';
  3. import 'package:xterm/utils/lookup_table.dart';
  4. class PaletteBuilder {
  5. final TerminalTheme theme;
  6. PaletteBuilder(this.theme);
  7. List<Color> build() {
  8. return List<Color>.generate(
  9. 256,
  10. paletteColor,
  11. growable: false,
  12. );
  13. }
  14. /// https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
  15. Color paletteColor(int colNum) {
  16. switch (colNum) {
  17. case 0:
  18. return theme.black;
  19. case 1:
  20. return theme.red;
  21. case 2:
  22. return theme.green;
  23. case 3:
  24. return theme.yellow;
  25. case 4:
  26. return theme.blue;
  27. case 5:
  28. return theme.magenta;
  29. case 6:
  30. return theme.cyan;
  31. case 7:
  32. return theme.white;
  33. case 8:
  34. return theme.brightBlack;
  35. case 9:
  36. return theme.brightRed;
  37. case 10:
  38. return theme.brightGreen;
  39. case 11:
  40. return theme.brightYellow;
  41. case 12:
  42. return theme.brightBlue;
  43. case 13:
  44. return theme.brightMagenta;
  45. case 14:
  46. return theme.brightCyan;
  47. case 15:
  48. return theme.white;
  49. }
  50. if (colNum < 232) {
  51. var r = 0;
  52. var g = 0;
  53. var b = 0;
  54. final index = colNum - 16;
  55. for (var i = 0; i < index; i++) {
  56. if (b == 0) {
  57. b = 95;
  58. } else if (b < 255) {
  59. b += 40;
  60. } else {
  61. b = 0;
  62. if (g == 0) {
  63. g = 95;
  64. } else if (g < 255) {
  65. g += 40;
  66. } else {
  67. g = 0;
  68. if (r == 0) {
  69. r = 95;
  70. } else if (r < 255) {
  71. r += 40;
  72. } else {
  73. break;
  74. }
  75. }
  76. }
  77. }
  78. return Color.fromARGB(0xFF, r, g, b);
  79. }
  80. return Color(_grayscaleColors[colNum.clamp(232, 255)]!);
  81. }
  82. }
  83. final _grayscaleColors = FastLookupTable({
  84. 232: 0xff080808,
  85. 233: 0xff121212,
  86. 234: 0xff1c1c1c,
  87. 235: 0xff262626,
  88. 236: 0xff303030,
  89. 237: 0xff3a3a3a,
  90. 238: 0xff444444,
  91. 239: 0xff4e4e4e,
  92. 240: 0xff585858,
  93. 241: 0xff626262,
  94. 242: 0xff6c6c6c,
  95. 243: 0xff767676,
  96. 244: 0xff808080,
  97. 245: 0xff8a8a8a,
  98. 246: 0xff949494,
  99. 247: 0xff9e9e9e,
  100. 248: 0xffa8a8a8,
  101. 249: 0xffb2b2b2,
  102. 250: 0xffbcbcbc,
  103. 251: 0xffc6c6c6,
  104. 252: 0xffd0d0d0,
  105. 253: 0xffdadada,
  106. 254: 0xffe4e4e4,
  107. 255: 0xffeeeeee,
  108. });