color-stack.rst 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. Color control
  2. ====================
  3. Saving and restoring colors
  4. ------------------------------
  5. It is often useful for a full screen application with its own color themes to
  6. set the default foreground, background, selection and cursor colors and the ANSI
  7. color table. This allows for various performance optimizations when drawing the
  8. screen. The problem is that if the user previously used the escape codes to
  9. change these colors themselves, then running the full screen application will
  10. lose those changes even after it exits. To avoid this, kitty introduces a new
  11. pair of *OSC* escape codes to push and pop the current color values from a
  12. stack::
  13. <ESC>]30001<ESC>\ # push onto stack
  14. <ESC>]30101<ESC>\ # pop from stack
  15. These escape codes save/restore the colors, default background, default
  16. foreground, selection background, selection foreground and cursor color and the
  17. 256 colors of the ANSI color table.
  18. .. note:: In July 2020, after several years, xterm copied this protocol
  19. extension, without acknowledgement, and using incompatible escape codes
  20. (XTPUSHCOLORS, XTPOPCOLORS, XTREPORTCOLORS). And they decided to save not
  21. just the dynamic colors but the entire ANSI color table. In the interests of
  22. promoting interoperability, kitty added support for xterm's escape codes as
  23. well, and changed this extension to also save/restore the entire ANSI color
  24. table.
  25. .. _color_control:
  26. Setting and querying colors
  27. -------------------------------
  28. While there exists a legacy protocol developed by XTerm for querying and
  29. setting colors, as with most XTerm protocols it suffers from the usual design
  30. limitations of being under specified and in-sufficient. XTerm implements
  31. querying of colors using OSC 4,5,6,10-19,104,105,106,110-119. This absurd
  32. profusion of numbers is completely unnecessary, redundant and requires adding
  33. two new numbers for every new color. Also XTerm's protocol doesn't handle the
  34. case of colors that are unknown to the terminal or that are not a set value,
  35. for example, many terminals implement selection as a reverse video effect not a
  36. fixed color. The XTerm protocol has no way to query for this condition. The
  37. protocol also doesn't actually specify the format in which colors are reported,
  38. deferring to a man page for X11!
  39. Instead kitty has developed a single number based protocol that addresses all
  40. these shortcomings and is future proof by virtue of using string keys rather
  41. than numbers. The syntax of the escape code is::
  42. <OSC> 21 ; key=value ; key=value ; ... <ST>
  43. The spaces in the above definition are for reading clarity and should be ignored.
  44. Here, ``<OSC>`` is the two bytes ``0x1b (ESC)`` and ``0x5b ([)``. ``ST`` is
  45. either `0x7 (BEL)` or the two bytes ``0x1b (ESC)`` and ``0x5c (\\)``.
  46. ``key`` is a number from 0-255 to query or set the color values from the
  47. terminals ANSI color table, or one of the strings in the table below for
  48. special colors:
  49. ================================= =============================================== ===============================
  50. key meaning dynamic
  51. ================================= =============================================== ===============================
  52. foreground The default foreground text color Not applicable
  53. background The default background text color Not applicable
  54. selection_background The background color of selections Reverse video
  55. selection_foreground The foreground color of selections Reverse video
  56. cursor The color of the text cursor Foreground color
  57. cursor_text The color of text under the cursor Background color
  58. visual_bell The color of a visual bell Automatic color selection based on current screen colors
  59. second_transparent_background A color that might be rendered semi-transparent No second color is made transparent
  60. in addition to the default background color
  61. ================================= =============================================== ===============================
  62. In this table the third column shows what effect setting the color to *dynamic*
  63. has in kitty and many other terminal emulators. It is advisory only, terminal
  64. emulators may not support dynamic colors for these or they may have other
  65. effects. Setting the ANSI color table colors to dynamic is not allowed.
  66. Querying current color values
  67. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  68. To query colors values, the client program sends this escape code with the
  69. ``value`` field set to ``?`` (the byte ``0x3f``). The terminal then responds
  70. with the same escape code, but with the ``?`` replaced by the :ref:`encoded
  71. color value <color_control_color_encoding>`. If the queried color is one that
  72. does not have a defined value, for example, if the terminal is using a reverse
  73. video effect or a gradient or similar, then the value must be empty, that is
  74. the response contains only the key and ``=``, no value. For example, if the
  75. client sends::
  76. <OSC> 21 ; foreground=? ; cursor=? <ST>
  77. The terminal responds::
  78. <OSC> 21 ; foreground=rgb:ff/00/00 ; cursor= <ST>
  79. This indicates that the foreground color is red and the cursor color is
  80. undefined (typically the cursor takes the color of the text under it and the
  81. text takes the color of the background).
  82. If the terminal does not know a field that a client send to it for a query it
  83. must respond back with the ``field=?``, that is, it must send back a question
  84. mark as the value.
  85. Setting color values
  86. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  87. To set a color value, the client program sends this escape code with the
  88. ``value`` field set to either an :ref:`encoded color value
  89. <color_control_color_encoding>` or the empty value. The empty value means
  90. the terminal should use a dynamic color for example reverse video for
  91. selections or similar. To reset a color to its default value (i.e. the value it
  92. would have if it was never set) the client program should send just the key
  93. name with no ``=`` and no value. For example::
  94. <OSC> 21 ; foreground=green ; cursor= ; background <ST>
  95. This sets the foreground to the color green, sets the cursor color to dynamic
  96. (usually meaning the cursor takes the color of the text under it) and resets
  97. the background color to its default value.
  98. To check if setting succeeded, the client can simply query the color, in fact
  99. the two can be combined into a single escape code, for example::
  100. <OSC> 21 ; foreground=white ; foreground=? <ST>
  101. The terminal will change the foreground color and reply with the new foreground
  102. color.
  103. .. _color_control_color_encoding:
  104. Color value encoding
  105. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  106. The color encoding is inherited from the scheme used by XTerm, for
  107. compatibility, but a sane, rigorously specified subset is chosen.
  108. RGB colors are encoded in one of three forms:
  109. rgb:<red>/<green>/<blue>
  110. <red>, <green>, <blue> := h | hh | hhh | hhhh
  111. h := single hexadecimal digits (case insignificant)
  112. Note that h indicates the value scaled in 4 bits, hh the value scaled in 8 bits, hhh the value scaled in 12 bits, and hhhh the value scaled
  113. in 16 bits, respectively.
  114. #<h...>
  115. h := single hexadecimal digits (case insignificant)
  116. #RGB (4 bits each)
  117. #RRGGBB (8 bits each)
  118. #RRRGGGBBB (12 bits each)
  119. #RRRRGGGGBBBB (16 bits each)
  120. The R, G, and B represent single hexadecimal digits. When fewer than 16 bits each are specified, they represent the most significant bits
  121. of the value (unlike the “rgb:” syntax, in which values are scaled). For example, the string ``#3a7`` is the same as ``#3000a0007000``.
  122. rgbi:<red>/<green>/<blue>
  123. red, green, and blue are floating-point values between 0.0 and 1.0, inclusive. The input format for these values is an optional
  124. sign, a string of numbers possibly containing a decimal point, and an optional exponent field containing an E or e followed by a possibly
  125. signed integer string.
  126. In addition, the following color names are accepted (case-insensitively) corresponding to the
  127. specified RGB values.
  128. .. include:: generated/color-names.rst