background.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package ansi
  2. import (
  3. "fmt"
  4. "image/color"
  5. )
  6. // Colorizer is a [color.Color] interface that can be formatted as a string.
  7. type Colorizer interface {
  8. color.Color
  9. fmt.Stringer
  10. }
  11. // HexColorizer is a [color.Color] that can be formatted as a hex string.
  12. type HexColorizer struct{ color.Color }
  13. var _ Colorizer = HexColorizer{}
  14. // String returns the color as a hex string. If the color is nil, an empty
  15. // string is returned.
  16. func (h HexColorizer) String() string {
  17. if h.Color == nil {
  18. return ""
  19. }
  20. r, g, b, _ := h.RGBA()
  21. // Get the lower 8 bits
  22. r &= 0xff
  23. g &= 0xff
  24. b &= 0xff
  25. return fmt.Sprintf("#%02x%02x%02x", uint8(r), uint8(g), uint8(b)) //nolint:gosec
  26. }
  27. // XRGBColorizer is a [color.Color] that can be formatted as an XParseColor
  28. // rgb: string.
  29. //
  30. // See: https://linux.die.net/man/3/xparsecolor
  31. type XRGBColorizer struct{ color.Color }
  32. var _ Colorizer = XRGBColorizer{}
  33. // String returns the color as an XParseColor rgb: string. If the color is nil,
  34. // an empty string is returned.
  35. func (x XRGBColorizer) String() string {
  36. if x.Color == nil {
  37. return ""
  38. }
  39. r, g, b, _ := x.RGBA()
  40. // Get the lower 8 bits
  41. return fmt.Sprintf("rgb:%04x/%04x/%04x", r, g, b)
  42. }
  43. // XRGBAColorizer is a [color.Color] that can be formatted as an XParseColor
  44. // rgba: string.
  45. //
  46. // See: https://linux.die.net/man/3/xparsecolor
  47. type XRGBAColorizer struct{ color.Color }
  48. var _ Colorizer = XRGBAColorizer{}
  49. // String returns the color as an XParseColor rgba: string. If the color is nil,
  50. // an empty string is returned.
  51. func (x XRGBAColorizer) String() string {
  52. if x.Color == nil {
  53. return ""
  54. }
  55. r, g, b, a := x.RGBA()
  56. // Get the lower 8 bits
  57. return fmt.Sprintf("rgba:%04x/%04x/%04x/%04x", r, g, b, a)
  58. }
  59. // SetForegroundColor returns a sequence that sets the default terminal
  60. // foreground color.
  61. //
  62. // OSC 10 ; color ST
  63. // OSC 10 ; color BEL
  64. //
  65. // Where color is the encoded color number.
  66. //
  67. // See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
  68. func SetForegroundColor(c color.Color) string {
  69. var s string
  70. switch c := c.(type) {
  71. case Colorizer:
  72. s = c.String()
  73. case fmt.Stringer:
  74. s = c.String()
  75. default:
  76. s = HexColorizer{c}.String()
  77. }
  78. return "\x1b]10;" + s + "\x07"
  79. }
  80. // RequestForegroundColor is a sequence that requests the current default
  81. // terminal foreground color.
  82. //
  83. // See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
  84. const RequestForegroundColor = "\x1b]10;?\x07"
  85. // ResetForegroundColor is a sequence that resets the default terminal
  86. // foreground color.
  87. //
  88. // See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
  89. const ResetForegroundColor = "\x1b]110\x07"
  90. // SetBackgroundColor returns a sequence that sets the default terminal
  91. // background color.
  92. //
  93. // OSC 11 ; color ST
  94. // OSC 11 ; color BEL
  95. //
  96. // Where color is the encoded color number.
  97. //
  98. // See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
  99. func SetBackgroundColor(c color.Color) string {
  100. var s string
  101. switch c := c.(type) {
  102. case Colorizer:
  103. s = c.String()
  104. case fmt.Stringer:
  105. s = c.String()
  106. default:
  107. s = HexColorizer{c}.String()
  108. }
  109. return "\x1b]11;" + s + "\x07"
  110. }
  111. // RequestBackgroundColor is a sequence that requests the current default
  112. // terminal background color.
  113. //
  114. // See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
  115. const RequestBackgroundColor = "\x1b]11;?\x07"
  116. // ResetBackgroundColor is a sequence that resets the default terminal
  117. // background color.
  118. //
  119. // See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
  120. const ResetBackgroundColor = "\x1b]111\x07"
  121. // SetCursorColor returns a sequence that sets the terminal cursor color.
  122. //
  123. // OSC 12 ; color ST
  124. // OSC 12 ; color BEL
  125. //
  126. // Where color is the encoded color number.
  127. //
  128. // See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
  129. func SetCursorColor(c color.Color) string {
  130. var s string
  131. switch c := c.(type) {
  132. case Colorizer:
  133. s = c.String()
  134. case fmt.Stringer:
  135. s = c.String()
  136. default:
  137. s = HexColorizer{c}.String()
  138. }
  139. return "\x1b]12;" + s + "\x07"
  140. }
  141. // RequestCursorColor is a sequence that requests the current terminal cursor
  142. // color.
  143. //
  144. // See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
  145. const RequestCursorColor = "\x1b]12;?\x07"
  146. // ResetCursorColor is a sequence that resets the terminal cursor color.
  147. //
  148. // See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
  149. const ResetCursorColor = "\x1b]112\x07"