sgr.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package aec
  2. import (
  3. "fmt"
  4. )
  5. // RGB3Bit is a 3bit RGB color.
  6. type RGB3Bit uint8
  7. // RGB8Bit is a 8bit RGB color.
  8. type RGB8Bit uint8
  9. func newSGR(n uint) ANSI {
  10. return newAnsi(fmt.Sprintf(esc+"%dm", n))
  11. }
  12. // NewRGB3Bit create a RGB3Bit from given RGB.
  13. func NewRGB3Bit(r, g, b uint8) RGB3Bit {
  14. return RGB3Bit((r >> 7) | ((g >> 6) & 0x2) | ((b >> 5) & 0x4))
  15. }
  16. // NewRGB8Bit create a RGB8Bit from given RGB.
  17. func NewRGB8Bit(r, g, b uint8) RGB8Bit {
  18. return RGB8Bit(16 + 36*(r/43) + 6*(g/43) + b/43)
  19. }
  20. // Color3BitF set the foreground color of text.
  21. func Color3BitF(c RGB3Bit) ANSI {
  22. return newAnsi(fmt.Sprintf(esc+"%dm", c+30))
  23. }
  24. // Color3BitB set the background color of text.
  25. func Color3BitB(c RGB3Bit) ANSI {
  26. return newAnsi(fmt.Sprintf(esc+"%dm", c+40))
  27. }
  28. // Color8BitF set the foreground color of text.
  29. func Color8BitF(c RGB8Bit) ANSI {
  30. return newAnsi(fmt.Sprintf(esc+"38;5;%dm", c))
  31. }
  32. // Color8BitB set the background color of text.
  33. func Color8BitB(c RGB8Bit) ANSI {
  34. return newAnsi(fmt.Sprintf(esc+"48;5;%dm", c))
  35. }
  36. // FullColorF set the foreground color of text.
  37. func FullColorF(r, g, b uint8) ANSI {
  38. return newAnsi(fmt.Sprintf(esc+"38;2;%d;%d;%dm", r, g, b))
  39. }
  40. // FullColorB set the foreground color of text.
  41. func FullColorB(r, g, b uint8) ANSI {
  42. return newAnsi(fmt.Sprintf(esc+"48;2;%d;%d;%dm", r, g, b))
  43. }
  44. // Style
  45. var (
  46. // Bold set the text style to bold or increased intensity.
  47. Bold ANSI
  48. // Faint set the text style to faint.
  49. Faint ANSI
  50. // Italic set the text style to italic.
  51. Italic ANSI
  52. // Underline set the text style to underline.
  53. Underline ANSI
  54. // BlinkSlow set the text style to slow blink.
  55. BlinkSlow ANSI
  56. // BlinkRapid set the text style to rapid blink.
  57. BlinkRapid ANSI
  58. // Inverse swap the foreground color and background color.
  59. Inverse ANSI
  60. // Conceal set the text style to conceal.
  61. Conceal ANSI
  62. // CrossOut set the text style to crossed out.
  63. CrossOut ANSI
  64. // Frame set the text style to framed.
  65. Frame ANSI
  66. // Encircle set the text style to encircled.
  67. Encircle ANSI
  68. // Overline set the text style to overlined.
  69. Overline ANSI
  70. )
  71. // Foreground color of text.
  72. var (
  73. // DefaultF is the default color of foreground.
  74. DefaultF ANSI
  75. // Normal color
  76. BlackF ANSI
  77. RedF ANSI
  78. GreenF ANSI
  79. YellowF ANSI
  80. BlueF ANSI
  81. MagentaF ANSI
  82. CyanF ANSI
  83. WhiteF ANSI
  84. // Light color
  85. LightBlackF ANSI
  86. LightRedF ANSI
  87. LightGreenF ANSI
  88. LightYellowF ANSI
  89. LightBlueF ANSI
  90. LightMagentaF ANSI
  91. LightCyanF ANSI
  92. LightWhiteF ANSI
  93. )
  94. // Background color of text.
  95. var (
  96. // DefaultB is the default color of background.
  97. DefaultB ANSI
  98. // Normal color
  99. BlackB ANSI
  100. RedB ANSI
  101. GreenB ANSI
  102. YellowB ANSI
  103. BlueB ANSI
  104. MagentaB ANSI
  105. CyanB ANSI
  106. WhiteB ANSI
  107. // Light color
  108. LightBlackB ANSI
  109. LightRedB ANSI
  110. LightGreenB ANSI
  111. LightYellowB ANSI
  112. LightBlueB ANSI
  113. LightMagentaB ANSI
  114. LightCyanB ANSI
  115. LightWhiteB ANSI
  116. )
  117. func init() {
  118. Bold = newSGR(1)
  119. Faint = newSGR(2)
  120. Italic = newSGR(3)
  121. Underline = newSGR(4)
  122. BlinkSlow = newSGR(5)
  123. BlinkRapid = newSGR(6)
  124. Inverse = newSGR(7)
  125. Conceal = newSGR(8)
  126. CrossOut = newSGR(9)
  127. BlackF = newSGR(30)
  128. RedF = newSGR(31)
  129. GreenF = newSGR(32)
  130. YellowF = newSGR(33)
  131. BlueF = newSGR(34)
  132. MagentaF = newSGR(35)
  133. CyanF = newSGR(36)
  134. WhiteF = newSGR(37)
  135. DefaultF = newSGR(39)
  136. BlackB = newSGR(40)
  137. RedB = newSGR(41)
  138. GreenB = newSGR(42)
  139. YellowB = newSGR(43)
  140. BlueB = newSGR(44)
  141. MagentaB = newSGR(45)
  142. CyanB = newSGR(46)
  143. WhiteB = newSGR(47)
  144. DefaultB = newSGR(49)
  145. Frame = newSGR(51)
  146. Encircle = newSGR(52)
  147. Overline = newSGR(53)
  148. LightBlackF = newSGR(90)
  149. LightRedF = newSGR(91)
  150. LightGreenF = newSGR(92)
  151. LightYellowF = newSGR(93)
  152. LightBlueF = newSGR(94)
  153. LightMagentaF = newSGR(95)
  154. LightCyanF = newSGR(96)
  155. LightWhiteF = newSGR(97)
  156. LightBlackB = newSGR(100)
  157. LightRedB = newSGR(101)
  158. LightGreenB = newSGR(102)
  159. LightYellowB = newSGR(103)
  160. LightBlueB = newSGR(104)
  161. LightMagentaB = newSGR(105)
  162. LightCyanB = newSGR(106)
  163. LightWhiteB = newSGR(107)
  164. }