colorable_windows.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. package colorable
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "math"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "syscall"
  11. "unsafe"
  12. "github.com/mattn/go-isatty"
  13. )
  14. const (
  15. foregroundBlue = 0x1
  16. foregroundGreen = 0x2
  17. foregroundRed = 0x4
  18. foregroundIntensity = 0x8
  19. foregroundMask = (foregroundRed | foregroundBlue | foregroundGreen | foregroundIntensity)
  20. backgroundBlue = 0x10
  21. backgroundGreen = 0x20
  22. backgroundRed = 0x40
  23. backgroundIntensity = 0x80
  24. backgroundMask = (backgroundRed | backgroundBlue | backgroundGreen | backgroundIntensity)
  25. )
  26. type wchar uint16
  27. type short int16
  28. type dword uint32
  29. type word uint16
  30. type coord struct {
  31. x short
  32. y short
  33. }
  34. type smallRect struct {
  35. left short
  36. top short
  37. right short
  38. bottom short
  39. }
  40. type consoleScreenBufferInfo struct {
  41. size coord
  42. cursorPosition coord
  43. attributes word
  44. window smallRect
  45. maximumWindowSize coord
  46. }
  47. var (
  48. kernel32 = syscall.NewLazyDLL("kernel32.dll")
  49. procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
  50. procSetConsoleTextAttribute = kernel32.NewProc("SetConsoleTextAttribute")
  51. procSetConsoleCursorPosition = kernel32.NewProc("SetConsoleCursorPosition")
  52. procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW")
  53. procFillConsoleOutputAttribute = kernel32.NewProc("FillConsoleOutputAttribute")
  54. )
  55. type Writer struct {
  56. out io.Writer
  57. handle syscall.Handle
  58. lastbuf bytes.Buffer
  59. oldattr word
  60. }
  61. func NewColorable(file *os.File) io.Writer {
  62. if file == nil {
  63. panic("nil passed instead of *os.File to NewColorable()")
  64. }
  65. if isatty.IsTerminal(file.Fd()) {
  66. var csbi consoleScreenBufferInfo
  67. handle := syscall.Handle(file.Fd())
  68. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  69. return &Writer{out: file, handle: handle, oldattr: csbi.attributes}
  70. } else {
  71. return file
  72. }
  73. }
  74. func NewColorableStdout() io.Writer {
  75. return NewColorable(os.Stdout)
  76. }
  77. func NewColorableStderr() io.Writer {
  78. return NewColorable(os.Stderr)
  79. }
  80. var color256 = map[int]int{
  81. 0: 0x000000,
  82. 1: 0x800000,
  83. 2: 0x008000,
  84. 3: 0x808000,
  85. 4: 0x000080,
  86. 5: 0x800080,
  87. 6: 0x008080,
  88. 7: 0xc0c0c0,
  89. 8: 0x808080,
  90. 9: 0xff0000,
  91. 10: 0x00ff00,
  92. 11: 0xffff00,
  93. 12: 0x0000ff,
  94. 13: 0xff00ff,
  95. 14: 0x00ffff,
  96. 15: 0xffffff,
  97. 16: 0x000000,
  98. 17: 0x00005f,
  99. 18: 0x000087,
  100. 19: 0x0000af,
  101. 20: 0x0000d7,
  102. 21: 0x0000ff,
  103. 22: 0x005f00,
  104. 23: 0x005f5f,
  105. 24: 0x005f87,
  106. 25: 0x005faf,
  107. 26: 0x005fd7,
  108. 27: 0x005fff,
  109. 28: 0x008700,
  110. 29: 0x00875f,
  111. 30: 0x008787,
  112. 31: 0x0087af,
  113. 32: 0x0087d7,
  114. 33: 0x0087ff,
  115. 34: 0x00af00,
  116. 35: 0x00af5f,
  117. 36: 0x00af87,
  118. 37: 0x00afaf,
  119. 38: 0x00afd7,
  120. 39: 0x00afff,
  121. 40: 0x00d700,
  122. 41: 0x00d75f,
  123. 42: 0x00d787,
  124. 43: 0x00d7af,
  125. 44: 0x00d7d7,
  126. 45: 0x00d7ff,
  127. 46: 0x00ff00,
  128. 47: 0x00ff5f,
  129. 48: 0x00ff87,
  130. 49: 0x00ffaf,
  131. 50: 0x00ffd7,
  132. 51: 0x00ffff,
  133. 52: 0x5f0000,
  134. 53: 0x5f005f,
  135. 54: 0x5f0087,
  136. 55: 0x5f00af,
  137. 56: 0x5f00d7,
  138. 57: 0x5f00ff,
  139. 58: 0x5f5f00,
  140. 59: 0x5f5f5f,
  141. 60: 0x5f5f87,
  142. 61: 0x5f5faf,
  143. 62: 0x5f5fd7,
  144. 63: 0x5f5fff,
  145. 64: 0x5f8700,
  146. 65: 0x5f875f,
  147. 66: 0x5f8787,
  148. 67: 0x5f87af,
  149. 68: 0x5f87d7,
  150. 69: 0x5f87ff,
  151. 70: 0x5faf00,
  152. 71: 0x5faf5f,
  153. 72: 0x5faf87,
  154. 73: 0x5fafaf,
  155. 74: 0x5fafd7,
  156. 75: 0x5fafff,
  157. 76: 0x5fd700,
  158. 77: 0x5fd75f,
  159. 78: 0x5fd787,
  160. 79: 0x5fd7af,
  161. 80: 0x5fd7d7,
  162. 81: 0x5fd7ff,
  163. 82: 0x5fff00,
  164. 83: 0x5fff5f,
  165. 84: 0x5fff87,
  166. 85: 0x5fffaf,
  167. 86: 0x5fffd7,
  168. 87: 0x5fffff,
  169. 88: 0x870000,
  170. 89: 0x87005f,
  171. 90: 0x870087,
  172. 91: 0x8700af,
  173. 92: 0x8700d7,
  174. 93: 0x8700ff,
  175. 94: 0x875f00,
  176. 95: 0x875f5f,
  177. 96: 0x875f87,
  178. 97: 0x875faf,
  179. 98: 0x875fd7,
  180. 99: 0x875fff,
  181. 100: 0x878700,
  182. 101: 0x87875f,
  183. 102: 0x878787,
  184. 103: 0x8787af,
  185. 104: 0x8787d7,
  186. 105: 0x8787ff,
  187. 106: 0x87af00,
  188. 107: 0x87af5f,
  189. 108: 0x87af87,
  190. 109: 0x87afaf,
  191. 110: 0x87afd7,
  192. 111: 0x87afff,
  193. 112: 0x87d700,
  194. 113: 0x87d75f,
  195. 114: 0x87d787,
  196. 115: 0x87d7af,
  197. 116: 0x87d7d7,
  198. 117: 0x87d7ff,
  199. 118: 0x87ff00,
  200. 119: 0x87ff5f,
  201. 120: 0x87ff87,
  202. 121: 0x87ffaf,
  203. 122: 0x87ffd7,
  204. 123: 0x87ffff,
  205. 124: 0xaf0000,
  206. 125: 0xaf005f,
  207. 126: 0xaf0087,
  208. 127: 0xaf00af,
  209. 128: 0xaf00d7,
  210. 129: 0xaf00ff,
  211. 130: 0xaf5f00,
  212. 131: 0xaf5f5f,
  213. 132: 0xaf5f87,
  214. 133: 0xaf5faf,
  215. 134: 0xaf5fd7,
  216. 135: 0xaf5fff,
  217. 136: 0xaf8700,
  218. 137: 0xaf875f,
  219. 138: 0xaf8787,
  220. 139: 0xaf87af,
  221. 140: 0xaf87d7,
  222. 141: 0xaf87ff,
  223. 142: 0xafaf00,
  224. 143: 0xafaf5f,
  225. 144: 0xafaf87,
  226. 145: 0xafafaf,
  227. 146: 0xafafd7,
  228. 147: 0xafafff,
  229. 148: 0xafd700,
  230. 149: 0xafd75f,
  231. 150: 0xafd787,
  232. 151: 0xafd7af,
  233. 152: 0xafd7d7,
  234. 153: 0xafd7ff,
  235. 154: 0xafff00,
  236. 155: 0xafff5f,
  237. 156: 0xafff87,
  238. 157: 0xafffaf,
  239. 158: 0xafffd7,
  240. 159: 0xafffff,
  241. 160: 0xd70000,
  242. 161: 0xd7005f,
  243. 162: 0xd70087,
  244. 163: 0xd700af,
  245. 164: 0xd700d7,
  246. 165: 0xd700ff,
  247. 166: 0xd75f00,
  248. 167: 0xd75f5f,
  249. 168: 0xd75f87,
  250. 169: 0xd75faf,
  251. 170: 0xd75fd7,
  252. 171: 0xd75fff,
  253. 172: 0xd78700,
  254. 173: 0xd7875f,
  255. 174: 0xd78787,
  256. 175: 0xd787af,
  257. 176: 0xd787d7,
  258. 177: 0xd787ff,
  259. 178: 0xd7af00,
  260. 179: 0xd7af5f,
  261. 180: 0xd7af87,
  262. 181: 0xd7afaf,
  263. 182: 0xd7afd7,
  264. 183: 0xd7afff,
  265. 184: 0xd7d700,
  266. 185: 0xd7d75f,
  267. 186: 0xd7d787,
  268. 187: 0xd7d7af,
  269. 188: 0xd7d7d7,
  270. 189: 0xd7d7ff,
  271. 190: 0xd7ff00,
  272. 191: 0xd7ff5f,
  273. 192: 0xd7ff87,
  274. 193: 0xd7ffaf,
  275. 194: 0xd7ffd7,
  276. 195: 0xd7ffff,
  277. 196: 0xff0000,
  278. 197: 0xff005f,
  279. 198: 0xff0087,
  280. 199: 0xff00af,
  281. 200: 0xff00d7,
  282. 201: 0xff00ff,
  283. 202: 0xff5f00,
  284. 203: 0xff5f5f,
  285. 204: 0xff5f87,
  286. 205: 0xff5faf,
  287. 206: 0xff5fd7,
  288. 207: 0xff5fff,
  289. 208: 0xff8700,
  290. 209: 0xff875f,
  291. 210: 0xff8787,
  292. 211: 0xff87af,
  293. 212: 0xff87d7,
  294. 213: 0xff87ff,
  295. 214: 0xffaf00,
  296. 215: 0xffaf5f,
  297. 216: 0xffaf87,
  298. 217: 0xffafaf,
  299. 218: 0xffafd7,
  300. 219: 0xffafff,
  301. 220: 0xffd700,
  302. 221: 0xffd75f,
  303. 222: 0xffd787,
  304. 223: 0xffd7af,
  305. 224: 0xffd7d7,
  306. 225: 0xffd7ff,
  307. 226: 0xffff00,
  308. 227: 0xffff5f,
  309. 228: 0xffff87,
  310. 229: 0xffffaf,
  311. 230: 0xffffd7,
  312. 231: 0xffffff,
  313. 232: 0x080808,
  314. 233: 0x121212,
  315. 234: 0x1c1c1c,
  316. 235: 0x262626,
  317. 236: 0x303030,
  318. 237: 0x3a3a3a,
  319. 238: 0x444444,
  320. 239: 0x4e4e4e,
  321. 240: 0x585858,
  322. 241: 0x626262,
  323. 242: 0x6c6c6c,
  324. 243: 0x767676,
  325. 244: 0x808080,
  326. 245: 0x8a8a8a,
  327. 246: 0x949494,
  328. 247: 0x9e9e9e,
  329. 248: 0xa8a8a8,
  330. 249: 0xb2b2b2,
  331. 250: 0xbcbcbc,
  332. 251: 0xc6c6c6,
  333. 252: 0xd0d0d0,
  334. 253: 0xdadada,
  335. 254: 0xe4e4e4,
  336. 255: 0xeeeeee,
  337. }
  338. func (w *Writer) Write(data []byte) (n int, err error) {
  339. var csbi consoleScreenBufferInfo
  340. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  341. er := bytes.NewBuffer(data)
  342. loop:
  343. for {
  344. r1, _, err := procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  345. if r1 == 0 {
  346. break loop
  347. }
  348. c1, _, err := er.ReadRune()
  349. if err != nil {
  350. break loop
  351. }
  352. if c1 != 0x1b {
  353. fmt.Fprint(w.out, string(c1))
  354. continue
  355. }
  356. c2, _, err := er.ReadRune()
  357. if err != nil {
  358. w.lastbuf.WriteRune(c1)
  359. break loop
  360. }
  361. if c2 != 0x5b {
  362. w.lastbuf.WriteRune(c1)
  363. w.lastbuf.WriteRune(c2)
  364. continue
  365. }
  366. var buf bytes.Buffer
  367. var m rune
  368. for {
  369. c, _, err := er.ReadRune()
  370. if err != nil {
  371. w.lastbuf.WriteRune(c1)
  372. w.lastbuf.WriteRune(c2)
  373. w.lastbuf.Write(buf.Bytes())
  374. break loop
  375. }
  376. if ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '@' {
  377. m = c
  378. break
  379. }
  380. buf.Write([]byte(string(c)))
  381. }
  382. var csbi consoleScreenBufferInfo
  383. switch m {
  384. case 'A':
  385. n, err = strconv.Atoi(buf.String())
  386. if err != nil {
  387. continue
  388. }
  389. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  390. csbi.cursorPosition.y -= short(n)
  391. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  392. case 'B':
  393. n, err = strconv.Atoi(buf.String())
  394. if err != nil {
  395. continue
  396. }
  397. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  398. csbi.cursorPosition.y += short(n)
  399. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  400. case 'C':
  401. n, err = strconv.Atoi(buf.String())
  402. if err != nil {
  403. continue
  404. }
  405. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  406. csbi.cursorPosition.x -= short(n)
  407. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  408. case 'D':
  409. n, err = strconv.Atoi(buf.String())
  410. if err != nil {
  411. continue
  412. }
  413. if n, err = strconv.Atoi(buf.String()); err == nil {
  414. var csbi consoleScreenBufferInfo
  415. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  416. csbi.cursorPosition.x += short(n)
  417. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  418. }
  419. case 'E':
  420. n, err = strconv.Atoi(buf.String())
  421. if err != nil {
  422. continue
  423. }
  424. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  425. csbi.cursorPosition.x = 0
  426. csbi.cursorPosition.y += short(n)
  427. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  428. case 'F':
  429. n, err = strconv.Atoi(buf.String())
  430. if err != nil {
  431. continue
  432. }
  433. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  434. csbi.cursorPosition.x = 0
  435. csbi.cursorPosition.y -= short(n)
  436. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  437. case 'G':
  438. n, err = strconv.Atoi(buf.String())
  439. if err != nil {
  440. continue
  441. }
  442. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  443. csbi.cursorPosition.x = short(n)
  444. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  445. case 'H':
  446. token := strings.Split(buf.String(), ";")
  447. if len(token) != 2 {
  448. continue
  449. }
  450. n1, err := strconv.Atoi(token[0])
  451. if err != nil {
  452. continue
  453. }
  454. n2, err := strconv.Atoi(token[1])
  455. if err != nil {
  456. continue
  457. }
  458. csbi.cursorPosition.x = short(n2)
  459. csbi.cursorPosition.x = short(n1)
  460. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  461. case 'J':
  462. n, err := strconv.Atoi(buf.String())
  463. if err != nil {
  464. continue
  465. }
  466. var cursor coord
  467. switch n {
  468. case 0:
  469. cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
  470. case 1:
  471. cursor = coord{x: csbi.window.left, y: csbi.window.top}
  472. case 2:
  473. cursor = coord{x: csbi.window.left, y: csbi.window.top}
  474. }
  475. var count, written dword
  476. count = dword(csbi.size.x - csbi.cursorPosition.x + (csbi.size.y-csbi.cursorPosition.y)*csbi.size.x)
  477. procFillConsoleOutputCharacter.Call(uintptr(w.handle), uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  478. procFillConsoleOutputAttribute.Call(uintptr(w.handle), uintptr(csbi.attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  479. case 'K':
  480. n, err := strconv.Atoi(buf.String())
  481. if err != nil {
  482. continue
  483. }
  484. var cursor coord
  485. switch n {
  486. case 0:
  487. cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
  488. case 1:
  489. cursor = coord{x: csbi.window.left, y: csbi.window.top + csbi.cursorPosition.y}
  490. case 2:
  491. cursor = coord{x: csbi.window.left, y: csbi.window.top + csbi.cursorPosition.y}
  492. }
  493. var count, written dword
  494. count = dword(csbi.size.x - csbi.cursorPosition.x)
  495. procFillConsoleOutputCharacter.Call(uintptr(w.handle), uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  496. procFillConsoleOutputAttribute.Call(uintptr(w.handle), uintptr(csbi.attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  497. case 'm':
  498. attr := csbi.attributes
  499. cs := buf.String()
  500. if cs == "" {
  501. procSetConsoleTextAttribute.Call(uintptr(w.handle), uintptr(w.oldattr))
  502. continue
  503. }
  504. token := strings.Split(cs, ";")
  505. for i := 0; i < len(token); i += 1 {
  506. ns := token[i]
  507. if n, err = strconv.Atoi(ns); err == nil {
  508. switch {
  509. case n == 0 || n == 100:
  510. attr = w.oldattr
  511. case 1 <= n && n <= 5:
  512. attr |= foregroundIntensity
  513. case n == 7:
  514. attr = ((attr & foregroundMask) << 4) | ((attr & backgroundMask) >> 4)
  515. case 22 == n || n == 25 || n == 25:
  516. attr |= foregroundIntensity
  517. case n == 27:
  518. attr = ((attr & foregroundMask) << 4) | ((attr & backgroundMask) >> 4)
  519. case 30 <= n && n <= 37:
  520. attr = (attr & backgroundMask)
  521. if (n-30)&1 != 0 {
  522. attr |= foregroundRed
  523. }
  524. if (n-30)&2 != 0 {
  525. attr |= foregroundGreen
  526. }
  527. if (n-30)&4 != 0 {
  528. attr |= foregroundBlue
  529. }
  530. case n == 38: // set foreground color.
  531. if i < len(token)-2 && (token[i+1] == "5" || token[i+1] == "05") {
  532. if n256, err := strconv.Atoi(token[i+2]); err == nil {
  533. if n256foreAttr == nil {
  534. n256setup()
  535. }
  536. attr &= backgroundMask
  537. attr |= n256foreAttr[n256]
  538. i += 2
  539. }
  540. } else {
  541. attr = attr & (w.oldattr & backgroundMask)
  542. }
  543. case n == 39: // reset foreground color.
  544. attr &= backgroundMask
  545. attr |= w.oldattr & foregroundMask
  546. case 40 <= n && n <= 47:
  547. attr = (attr & foregroundMask)
  548. if (n-40)&1 != 0 {
  549. attr |= backgroundRed
  550. }
  551. if (n-40)&2 != 0 {
  552. attr |= backgroundGreen
  553. }
  554. if (n-40)&4 != 0 {
  555. attr |= backgroundBlue
  556. }
  557. case n == 48: // set background color.
  558. if i < len(token)-2 && token[i+1] == "5" {
  559. if n256, err := strconv.Atoi(token[i+2]); err == nil {
  560. if n256backAttr == nil {
  561. n256setup()
  562. }
  563. attr &= foregroundMask
  564. attr |= n256backAttr[n256]
  565. i += 2
  566. }
  567. } else {
  568. attr = attr & (w.oldattr & foregroundMask)
  569. }
  570. case n == 49: // reset foreground color.
  571. attr &= foregroundMask
  572. attr |= w.oldattr & backgroundMask
  573. case 90 <= n && n <= 97:
  574. attr = (attr & backgroundMask)
  575. attr |= foregroundIntensity
  576. if (n-90)&1 != 0 {
  577. attr |= foregroundRed
  578. }
  579. if (n-90)&2 != 0 {
  580. attr |= foregroundGreen
  581. }
  582. if (n-90)&4 != 0 {
  583. attr |= foregroundBlue
  584. }
  585. case 100 <= n && n <= 107:
  586. attr = (attr & foregroundMask)
  587. attr |= backgroundIntensity
  588. if (n-100)&1 != 0 {
  589. attr |= backgroundRed
  590. }
  591. if (n-100)&2 != 0 {
  592. attr |= backgroundGreen
  593. }
  594. if (n-100)&4 != 0 {
  595. attr |= backgroundBlue
  596. }
  597. }
  598. procSetConsoleTextAttribute.Call(uintptr(w.handle), uintptr(attr))
  599. }
  600. }
  601. }
  602. }
  603. return len(data) - w.lastbuf.Len(), nil
  604. }
  605. type consoleColor struct {
  606. rgb int
  607. red bool
  608. green bool
  609. blue bool
  610. intensity bool
  611. }
  612. func (c consoleColor) foregroundAttr() (attr word) {
  613. if c.red {
  614. attr |= foregroundRed
  615. }
  616. if c.green {
  617. attr |= foregroundGreen
  618. }
  619. if c.blue {
  620. attr |= foregroundBlue
  621. }
  622. if c.intensity {
  623. attr |= foregroundIntensity
  624. }
  625. return
  626. }
  627. func (c consoleColor) backgroundAttr() (attr word) {
  628. if c.red {
  629. attr |= backgroundRed
  630. }
  631. if c.green {
  632. attr |= backgroundGreen
  633. }
  634. if c.blue {
  635. attr |= backgroundBlue
  636. }
  637. if c.intensity {
  638. attr |= backgroundIntensity
  639. }
  640. return
  641. }
  642. var color16 = []consoleColor{
  643. consoleColor{0x000000, false, false, false, false},
  644. consoleColor{0x000080, false, false, true, false},
  645. consoleColor{0x008000, false, true, false, false},
  646. consoleColor{0x008080, false, true, true, false},
  647. consoleColor{0x800000, true, false, false, false},
  648. consoleColor{0x800080, true, false, true, false},
  649. consoleColor{0x808000, true, true, false, false},
  650. consoleColor{0xc0c0c0, true, true, true, false},
  651. consoleColor{0x808080, false, false, false, true},
  652. consoleColor{0x0000ff, false, false, true, true},
  653. consoleColor{0x00ff00, false, true, false, true},
  654. consoleColor{0x00ffff, false, true, true, true},
  655. consoleColor{0xff0000, true, false, false, true},
  656. consoleColor{0xff00ff, true, false, true, true},
  657. consoleColor{0xffff00, true, true, false, true},
  658. consoleColor{0xffffff, true, true, true, true},
  659. }
  660. type hsv struct {
  661. h, s, v float32
  662. }
  663. func (a hsv) dist(b hsv) float32 {
  664. dh := a.h - b.h
  665. switch {
  666. case dh > 0.5:
  667. dh = 1 - dh
  668. case dh < -0.5:
  669. dh = -1 - dh
  670. }
  671. ds := a.s - b.s
  672. dv := a.v - b.v
  673. return float32(math.Sqrt(float64(dh*dh + ds*ds + dv*dv)))
  674. }
  675. func toHSV(rgb int) hsv {
  676. r, g, b := float32((rgb&0xFF0000)>>16)/256.0,
  677. float32((rgb&0x00FF00)>>8)/256.0,
  678. float32(rgb&0x0000FF)/256.0
  679. min, max := minmax3f(r, g, b)
  680. h := max - min
  681. if h > 0 {
  682. if max == r {
  683. h = (g - b) / h
  684. if h < 0 {
  685. h += 6
  686. }
  687. } else if max == g {
  688. h = 2 + (b-r)/h
  689. } else {
  690. h = 4 + (r-g)/h
  691. }
  692. }
  693. h /= 6.0
  694. s := max - min
  695. if max != 0 {
  696. s /= max
  697. }
  698. v := max
  699. return hsv{h: h, s: s, v: v}
  700. }
  701. type hsvTable []hsv
  702. func toHSVTable(rgbTable []consoleColor) hsvTable {
  703. t := make(hsvTable, len(rgbTable))
  704. for i, c := range rgbTable {
  705. t[i] = toHSV(c.rgb)
  706. }
  707. return t
  708. }
  709. func (t hsvTable) find(rgb int) consoleColor {
  710. hsv := toHSV(rgb)
  711. n := 7
  712. l := float32(5.0)
  713. for i, p := range t {
  714. d := hsv.dist(p)
  715. if d < l {
  716. l, n = d, i
  717. }
  718. }
  719. return color16[n]
  720. }
  721. func minmax3f(a, b, c float32) (min, max float32) {
  722. if a < b {
  723. if b < c {
  724. return a, c
  725. } else if a < c {
  726. return a, b
  727. } else {
  728. return c, b
  729. }
  730. } else {
  731. if a < c {
  732. return b, c
  733. } else if b < c {
  734. return b, a
  735. } else {
  736. return c, a
  737. }
  738. }
  739. }
  740. var n256foreAttr []word
  741. var n256backAttr []word
  742. func n256setup() {
  743. n256foreAttr = make([]word, 256)
  744. n256backAttr = make([]word, 256)
  745. t := toHSVTable(color16)
  746. for i, rgb := range color256 {
  747. c := t.find(rgb)
  748. n256foreAttr[i] = c.foregroundAttr()
  749. n256backAttr[i] = c.backgroundAttr()
  750. }
  751. }