colorable_windows.go 17 KB

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