colorable_windows.go 17 KB

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