AnsiColor.kt

  1. package com.hexagonkt.core.text

  2. import com.hexagonkt.core.text.Ansi.CSI

  3. object AnsiColor {
  4.     /** Set black as the foreground color. */
  5.     const val BLACK = "${CSI}30m"
  6.     /** Set red as the foreground color. */
  7.     const val RED = "${CSI}31m"
  8.     /** Set green as the foreground color. */
  9.     const val GREEN = "${CSI}32m"
  10.     /** Set yellow as the foreground color. */
  11.     const val YELLOW = "${CSI}33m"
  12.     /** Set blue as the foreground color. */
  13.     const val BLUE = "${CSI}34m"
  14.     /** Set magenta as the foreground color. */
  15.     const val MAGENTA = "${CSI}35m"
  16.     /** Set cyan as the foreground color. */
  17.     const val CYAN = "${CSI}36m"
  18.     /** Set white as the foreground color. */
  19.     const val WHITE = "${CSI}37m"
  20.     /** Set back the default foreground color. */
  21.     const val DEFAULT = "${CSI}39m"

  22.     /** Set black as the background color. */
  23.     const val BLACK_BG = "${CSI}40m"
  24.     /** Set red as the background color. */
  25.     const val RED_BG = "${CSI}41m"
  26.     /** Set green as the background color. */
  27.     const val GREEN_BG = "${CSI}42m"
  28.     /** Set yellow as the background color. */
  29.     const val YELLOW_BG = "${CSI}43m"
  30.     /** Set blue as the background color. */
  31.     const val BLUE_BG = "${CSI}44m"
  32.     /** Set magenta as the background color. */
  33.     const val MAGENTA_BG = "${CSI}45m"
  34.     /** Set cyan as the background color. */
  35.     const val CYAN_BG = "${CSI}46m"
  36.     /** Set white as the background color. */
  37.     const val WHITE_BG = "${CSI}47m"
  38.     /** Set back the default background color. */
  39.     const val DEFAULT_BG = "${CSI}49m"

  40.     /** Set bright black as the foreground color. */
  41.     const val BRIGHT_BLACK = "${CSI}90m"
  42.     /** Set bright red as the foreground color. */
  43.     const val BRIGHT_RED = "${CSI}91m"
  44.     /** Set bright green as the foreground color. */
  45.     const val BRIGHT_GREEN = "${CSI}92m"
  46.     /** Set bright yellow as the foreground color. */
  47.     const val BRIGHT_YELLOW = "${CSI}93m"
  48.     /** Set bright blue as the foreground color. */
  49.     const val BRIGHT_BLUE = "${CSI}94m"
  50.     /** Set bright magenta as the foreground color. */
  51.     const val BRIGHT_MAGENTA = "${CSI}95m"
  52.     /** Set bright cyan as the foreground color. */
  53.     const val BRIGHT_CYAN = "${CSI}96m"
  54.     /** Set bright white as the foreground color. */
  55.     const val BRIGHT_WHITE = "${CSI}97m"

  56.     /** Set bright black as the background color. */
  57.     const val BRIGHT_BLACK_BG = "${CSI}100m"
  58.     /** Set bright red as the background color. */
  59.     const val BRIGHT_RED_BG = "${CSI}101m"
  60.     /** Set bright green as the background color. */
  61.     const val BRIGHT_GREEN_BG = "${CSI}102m"
  62.     /** Set bright yellow as the background color. */
  63.     const val BRIGHT_YELLOW_BG = "${CSI}103m"
  64.     /** Set bright blue as the background color. */
  65.     const val BRIGHT_BLUE_BG = "${CSI}104m"
  66.     /** Set bright magenta as the background color. */
  67.     const val BRIGHT_MAGENTA_BG = "${CSI}105m"
  68.     /** Set bright cyan as the background color. */
  69.     const val BRIGHT_CYAN_BG = "${CSI}106m"
  70.     /** Set bright white as the background color. */
  71.     const val BRIGHT_WHITE_BG = "${CSI}107m"

  72.     /**
  73.      * Set true color (24 bit) foreground.
  74.      *
  75.      * @param r Red intensity. Must be in the 0..255 range.
  76.      * @param g Green intensity. Must be in the 0..255 range.
  77.      * @param b Blue intensity. Must be in the 0..255 range.
  78.      *
  79.      * @return Escape code to set the foreground color.
  80.      */
  81.     fun fg(r: Int = 0, g: Int = 0, b: Int = 0): String {
  82.         requireRange(r, 0..255, "Red")
  83.         requireRange(g, 0..255, "Green")
  84.         requireRange(b, 0..255, "Blue")
  85.         return "${CSI}38;2;$r;$g;${b}m"
  86.     }

  87.     /**
  88.      * Set true color (24 bit) background.
  89.      *
  90.      * @param r Red intensity. Must be in the 0..255 range.
  91.      * @param g Green intensity. Must be in the 0..255 range.
  92.      * @param b Blue intensity. Must be in the 0..255 range.
  93.      *
  94.      * @return Escape code to set the background color.
  95.      */
  96.     fun bg(r: Int = 0, g: Int = 0, b: Int = 0): String {
  97.         requireRange(r, 0..255, "Red")
  98.         requireRange(g, 0..255, "Green")
  99.         requireRange(b, 0..255, "Blue")
  100.         return "${CSI}48;2;$r;$g;${b}m"
  101.     }

  102.     private fun requireRange(value: Int, range: IntRange, name: String) {
  103.         require(value in range) { "$name value must be in the $range range: $value" }
  104.     }
  105. }