color-stack.rst 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. transparent_background_color1..8 A background color that is rendered Unset
  60. with the specified opacity in cells that have
  61. the specified background color. An opacity
  62. value less than zero means, use the
  63. :opt:`background_opacity` value.
  64. ================================= =============================================== ===============================
  65. In this table the third column shows what effect setting the color to *dynamic*
  66. has in kitty and many other terminal emulators. It is advisory only, terminal
  67. emulators may not support dynamic colors for these or they may have other
  68. effects. Setting the ANSI color table colors to dynamic is not allowed.
  69. Querying current color values
  70. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  71. To query colors values, the client program sends this escape code with the
  72. ``value`` field set to ``?`` (the byte ``0x3f``). The terminal then responds
  73. with the same escape code, but with the ``?`` replaced by the :ref:`encoded
  74. color value <color_control_color_encoding>`. If the queried color is one that
  75. does not have a defined value, for example, if the terminal is using a reverse
  76. video effect or a gradient or similar, then the value must be empty, that is
  77. the response contains only the key and ``=``, no value. For example, if the
  78. client sends::
  79. <OSC> 21 ; foreground=? ; cursor=? <ST>
  80. The terminal responds::
  81. <OSC> 21 ; foreground=rgb:ff/00/00 ; cursor= <ST>
  82. This indicates that the foreground color is red and the cursor color is
  83. undefined (typically the cursor takes the color of the text under it and the
  84. text takes the color of the background).
  85. If the terminal does not know a field that a client send to it for a query it
  86. must respond back with the ``field=?``, that is, it must send back a question
  87. mark as the value.
  88. Setting color values
  89. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  90. To set a color value, the client program sends this escape code with the
  91. ``value`` field set to either an :ref:`encoded color value
  92. <color_control_color_encoding>` or the empty value. The empty value means
  93. the terminal should use a dynamic color for example reverse video for
  94. selections or similar. To reset a color to its default value (i.e. the value it
  95. would have if it was never set) the client program should send just the key
  96. name with no ``=`` and no value. For example::
  97. <OSC> 21 ; foreground=green ; cursor= ; background <ST>
  98. This sets the foreground to the color green, sets the cursor color to dynamic
  99. (usually meaning the cursor takes the color of the text under it) and resets
  100. the background color to its default value.
  101. To check if setting succeeded, the client can simply query the color, in fact
  102. the two can be combined into a single escape code, for example::
  103. <OSC> 21 ; foreground=white ; foreground=? <ST>
  104. The terminal will change the foreground color and reply with the new foreground
  105. color.
  106. .. _color_control_color_encoding:
  107. Color value encoding
  108. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  109. The color encoding is inherited from the scheme used by XTerm, for
  110. compatibility, but a sane, rigorously specified subset is chosen.
  111. RGB colors are encoded in one of three forms:
  112. ``rgb:<red>/<green>/<blue>``
  113. | <red>, <green>, <blue> := h | hh | hhh | hhhh
  114. | h := single hexadecimal digits (case insignificant)
  115. | Note that h indicates the value scaled in 4 bits, hh the value scaled in 8 bits,
  116. hhh the value scaled in 12 bits, and hhhh the value scaled in 16 bits, respectively.
  117. ``#<h...>``
  118. | h := single hexadecimal digits (case insignificant)
  119. | #RGB (4 bits each)
  120. | #RRGGBB (8 bits each)
  121. | #RRRGGGBBB (12 bits each)
  122. | #RRRRGGGGBBBB (16 bits each)
  123. | The R, G, and B represent single hexadecimal digits. When fewer than 16 bits
  124. each are specified, they represent the most significant bits of the value
  125. (unlike the “rgb:” syntax, in which values are scaled). For example,
  126. the string ``#3a7`` is the same as ``#3000a0007000``.
  127. ``rgbi:<red>/<green>/<blue>``
  128. red, green, and blue are floating-point values between 0.0 and 1.0, inclusive. The input format for these values is an optional
  129. sign, a string of numbers possibly containing a decimal point, and an optional exponent field containing an E or e followed by a possibly
  130. signed integer string. Values outside the ``0 - 1`` range must be clipped to be within the range.
  131. If a color should have an alpha component, it must be suffixed to the color
  132. specification in the form :code:`@number between zero and one`. For example::
  133. red@0.5 rgb:ff0000@0.1 #ff0000@0.3
  134. The syntax for the floating point alpha component is the same as used for the
  135. components of ``rgbi`` defined above. When not specified, the default alpha
  136. value is ``1.0``. Values outside the range ``0 - 1`` must be clipped
  137. to be within the range, negative values may have special context dependent
  138. meaning.
  139. In addition, the following color names are accepted (case-insensitively) corresponding to the
  140. specified RGB values.
  141. .. include:: generated/color-names.rst