colors.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package fancycolors
  2. import (
  3. "fmt"
  4. "log"
  5. "regexp"
  6. "strings"
  7. door "git.red-green.com/redgreen/doorgo"
  8. )
  9. const pipeCodeRegex = "(\\`(.))+"
  10. var match_reg = regexp.MustCompile(pipeCodeRegex)
  11. // Replaces all short color codes into their ANSI colors
  12. func ReplaceAll(input string) string {
  13. for {
  14. hit := match_reg.FindStringIndex(input)
  15. if len(hit) == 0 {
  16. return input
  17. }
  18. match := input[hit[0]:hit[1]]
  19. input = strings.ReplaceAll(input, match, Color(match))
  20. }
  21. }
  22. // Removes all short color codes from the string
  23. func StripAll(input string) string {
  24. for {
  25. hit := match_reg.FindStringIndex(input)
  26. if len(hit) == 0 {
  27. return input
  28. }
  29. match := input[hit[0]:hit[1]]
  30. input = strings.ReplaceAll(input, match, "")
  31. }
  32. }
  33. // I use the official Darkness v2.0 short codes! (Taken directly from the game)
  34. //
  35. // Foreground only (Background is always black, and no blinking)
  36. func Color(code string) string {
  37. if code[0] == '`' { // In case we call it manually
  38. code = code[1:]
  39. }
  40. switch code {
  41. case "1": // Dark Blue
  42. return door.ColorText("BLU ON BLA")
  43. case "2": // Dark Green
  44. return door.ColorText("GRE ON BLA")
  45. case "3": // Dark Cyan
  46. return door.ColorText("CYA ON BLA")
  47. case "4": // Dark Red
  48. return door.ColorText("RED ON BLA")
  49. case "5": // Dark Magenta
  50. return door.ColorText("MAG ON BLA")
  51. case "6": // Brown
  52. return door.ColorText("BRO ON BLA")
  53. case "7": // Gray
  54. return door.ColorText("WHI ON BLA")
  55. case "8": // Dark Gray
  56. return door.ColorText("BRI BLA ON BLA")
  57. case "9": // Bright Blue
  58. return door.ColorText("BRI BLU ON BLA")
  59. case "0": // Bright Green
  60. return door.ColorText("BRI GRE ON BLA")
  61. case "!": // Bright Cyan
  62. return door.ColorText("BRI CYA ON BLA")
  63. case "@": // Bright Red
  64. return door.ColorText("BRI RED ON BLA")
  65. case "#": // Bright Magenta
  66. return door.ColorText("BRI MAG ON BLA")
  67. case "$": // Yellow
  68. return door.ColorText("BRI BRO ON BLA")
  69. case "%": // White
  70. return door.ColorText("BRI WHI ON BLA")
  71. default:
  72. log.Printf("PipeColor.Color('%s') Invalid code", code)
  73. return ""
  74. }
  75. }
  76. var cachedPanel *door.Panel
  77. // Makes a panel to show each of the colors and their codes
  78. func MakePanel() *door.Panel {
  79. if cachedPanel == nil {
  80. width := 22
  81. p := &door.Panel{
  82. Width: width,
  83. Style: door.SINGLE,
  84. BorderColor: Color("`3"),
  85. Title: "[ Colors ]",
  86. TitleColor: Color("`!"),
  87. TitleOffset: width/2 - 5,
  88. }
  89. bar := "\xdb"
  90. if door.Unicode {
  91. bar = "\u2588"
  92. }
  93. nice := func(colorname, colorsymbol string) {
  94. p.Lines = append(p.Lines, door.Line{
  95. Text: fmt.Sprintf(Color("%")+"%-22s"+p.BorderColor, fmt.Sprintf("%-15s %s %s"+bar+bar+bar, colorname, colorsymbol, Color(colorsymbol))),
  96. })
  97. }
  98. nice("Dark Blue", "`1")
  99. nice("Dark Green", "`2")
  100. nice("Dark Cyan", "`3")
  101. nice("Dark Red", "`4")
  102. nice("Dark Magenta", "`5")
  103. nice("Brown", "`6")
  104. nice("Gray", "`7")
  105. nice("Dark Gray", "`8")
  106. p.Lines = append(p.Lines, p.Spacer())
  107. nice("Bright Blue", "`9")
  108. nice("Bright Green", "`0")
  109. nice("Bright Cyan", "`!")
  110. nice("Bright Red", "`@")
  111. nice("Bright Magenta", "`#")
  112. nice("Yellow", "`$")
  113. nice("White", "`%")
  114. p.Lines = append(p.Lines, door.Line{
  115. Text: fmt.Sprintf(Color("%")+"%-22s"+p.BorderColor,
  116. "Black Not Available"),
  117. })
  118. cachedPanel = p
  119. }
  120. return cachedPanel
  121. }