terminal/src/buffer/out
James Holderness b604117421
Standardize the color table order (#11602)
## Summary of the Pull Request

In the original implementation, we used two different orderings for the color tables. The WT color table used ANSI order, while the conhost color table used a Windows-specific order. This PR standardizes on the ANSI color order everywhere, so the usage of indexed colors is consistent across both parts of the code base, which will hopefully allow more of the code to be shared one day.

## References

This is another small step towards de-duplicating `AdaptDispatch` and `TerminalDispatch` for issue #3849, and is essentially a followup to the SGR dispatch refactoring in PR #6728.

## PR Checklist
* [x] Closes #11461
* [x] CLA signed.
* [x] Tests added/passed
* [ ] Documentation updated.
* [ ] Schema updated.
* [x] I've discussed this with core contributors already. Issue number where discussion took place: #11461

## Detailed Description of the Pull Request / Additional comments

Conhost still needs to deal with legacy attributes using Windows color order, so those values now need to be transposed to ANSI colors order when creating a `TextAttribute` object. This is done with a simple mapping table, which also handles the translation of the default color entries, so it's actually slightly faster than the original code.

And when converting `TextAttribute` values back to legacy console attributes, we were already using a mapping table to handle the narrowing of 256-color values down to 16 colors, so we just needed to adjust that table to account for the translation from ANSI to Windows, and then could make use of the same table for both 256-color and 16-color values.

There are also a few places in conhost that read from or write to the color tables, and those now need to transpose the index values. I've addressed this by creating separate `SetLegacyColorTableEntry` and `GetLegacyColorTableEntry` methods in the `Settings` class which take care of the mapping, so it's now clearer in which cases the code is dealing with legacy values, and which are ANSI values.

These methods are used in the `SetConsoleScreenBufferInfoEx` and `GetConsoleScreenBufferInfoEx` APIs, as well as a few place where color preferences are handled (the registry, shortcut links, and the properties dialog), none of which are particularly sensitive to performance. However, we also use the legacy table when looking up the default colors for rendering (which happens a lot), so I've refactored that code so the default color calculations now only occur once per frame.

The plus side of all of this is that the VT code doesn't need to do the index translation anymore, so we can finally get rid of all the calls to `XTermToWindowsIndex`, and we no longer need a separate color table initialization method for conhost, so I was able to merge a number of color initialization methods into one. We also no longer need to translate from legacy values to ANSI when generating VT sequences for conpty.

The one exception to that is the 16-color VT renderer, which uses the `TextColor::GetLegacyIndex` method to approximate 16-color equivalents for RGB and 256-color values. Since that method returns a legacy index, it still needs to be translated to ANSI before it can be used in a VT sequence. But this should be no worse than it was before.

One more special case is conhost's secret _Color Selection_ feature. That uses `Ctrl`+Number and `Alt`+Number key sequences to highlight parts of the buffer, and the mapping from number to color is based on the Windows color order. So that mapping now needs to be transposed, but that's also not performance sensitive.

The only thing that I haven't bothered to update is the trace logging code in the `Telemetry` class, which logs the first 16 entries in the color table. Those entries are now going to be in a different order, but I didn't think that would be of great concern to anyone.

## Validation Steps Performed

A lot of unit tests needed to be updated to use ANSI color constants when setting indexed colors, where before they might have been expecting values in Windows order. But this replaced a wild mix of different constants, sometimes having to use bit shifting, as well as values mapped with `XTermToWindowsIndex`, so I think the tests are a whole lot clearer now. Only a few cases have been left with literal numbers where that seemed more appropriate.

In addition to getting the unit tests working, I've also manually tested the behaviour of all the console APIs which I thought could be affected by these changes, and confirmed that they produced the same results in the new code as they did in the original implementation.

This includes:
- `WriteConsoleOutput`
- `ReadConsoleOutput`
- `SetConsoleTextAttribute` with `WriteConsoleOutputCharacter`
- `FillConsoleOutputAttribute` and `FillConsoleOutputCharacter` 
- `ScrollConsoleScreenBuffer`
- `GetConsoleScreenBufferInfo`
- `GetConsoleScreenBufferInfoEx`
- `SetConsoleScreenBufferInfoEx`

I've also manually tested changing colors via the console properties menu, the registry, and shortcut links, including setting default colors and popup colors. And I've tested that the "Quirks Mode" is still working as expected in PowerShell.

In terms of performance, I wrote a little test app that filled a 80x9999 buffer with random color combinations using `WriteConsoleOutput`, which I figured was likely to be the most performance sensitive call, and I think it now actually performs slightly better than the original implementation.

I've also tested similar code - just filling the visible window - with SGR VT sequences of various types, and the performance seems about the same as it was before.
2021-11-04 22:13:22 +00:00
..
lib Vectorize TextColor::GetColor (#10779) 2021-08-02 19:02:59 +00:00
ut_textbuffer Standardize the color table order (#11602) 2021-11-04 22:13:22 +00:00
AttrRow.cpp Introduce til::rle - a run length encoded vector (#10099) 2021-05-20 17:27:50 +00:00
AttrRow.hpp Introduce til::rle - a run length encoded vector (#10099) 2021-05-20 17:27:50 +00:00
CharRow.cpp ROW: clean up in preparation to hide CharRow & AttrRow (#8446) 2021-01-20 21:16:56 +00:00
CharRow.hpp ROW: clean up in preparation to hide CharRow & AttrRow (#8446) 2021-01-20 21:16:56 +00:00
CharRowCell.cpp Greatly reduce allocations in the conhost/OpenConsole startup path (#8489) 2020-12-16 10:40:30 -08:00
CharRowCell.hpp Greatly reduce allocations in the conhost/OpenConsole startup path (#8489) 2020-12-16 10:40:30 -08:00
CharRowCellReference.cpp Add GH issue IDs to all the suppress/disables that I left behind as they were a bit too challenging to solve with this giant PR 2019-09-05 11:14:43 -07:00
CharRowCellReference.hpp Greatly reduce allocations in the conhost/OpenConsole startup path (#8489) 2020-12-16 10:40:30 -08:00
cursor.cpp Defer cursor winrt event triggering (#10685) 2021-07-20 14:05:45 +00:00
cursor.h Defer cursor winrt event triggering (#10685) 2021-07-20 14:05:45 +00:00
DbcsAttribute.hpp C26447,C26440 - is noexcept but can throw or doesn't throw but not noexcept 2019-08-29 15:23:07 -07:00
dirs Initial release of the Windows Terminal source code 2019-05-02 15:29:04 -07:00
LineRendition.hpp Add support for double-width/double-height lines in conhost (#8664) 2021-02-18 05:44:50 +00:00
OutputCell.cpp Refactor the SGR implementation in AdaptDispatch (#5758) 2020-05-08 16:04:16 -07:00
OutputCell.hpp Refactor the SGR implementation in AdaptDispatch (#5758) 2020-05-08 16:04:16 -07:00
OutputCellIterator.cpp Replace gsl::at with a new til::at(span) for pre-checked bounds (#6925) 2020-07-15 10:29:36 -07:00
OutputCellIterator.hpp Replace basic_string_view<T> with span<const T> (#6921) 2020-07-15 16:40:42 +00:00
OutputCellRect.cpp Replace basic_string_view<T> with span<const T> (#6921) 2020-07-15 16:40:42 +00:00
OutputCellRect.hpp C26455, default constructor may not throw. Mark noexcept. (Trivial cases.) 2019-09-03 14:57:14 -07:00
OutputCellView.cpp Some of the PR feedback. 2019-09-05 17:21:54 -07:00
OutputCellView.hpp Optimize hot path in textBufferCellIterator (#10621) 2021-07-27 15:09:56 +00:00
precomp.cpp Initial release of the Windows Terminal source code 2019-05-02 15:29:04 -07:00
precomp.h Exclude more rarely-used stuff from Windows headers (#8513) 2020-12-11 19:35:23 +00:00
Row.cpp Introduce til::rle - a run length encoded vector (#10099) 2021-05-20 17:27:50 +00:00
Row.hpp Introduce til::rle - a run length encoded vector (#10099) 2021-05-20 17:27:50 +00:00
search.cpp Add support for double-width/double-height lines in conhost (#8664) 2021-02-18 05:44:50 +00:00
search.h Cache the size viewport structure inside TextBuffer (#6841) 2020-07-09 11:18:25 +00:00
sources.inc Merged PR 6142815: OS build fixes on top of 94d39b758 2021-06-10 15:52:14 +00:00
TextAttribute.cpp Standardize the color table order (#11602) 2021-11-04 22:13:22 +00:00
TextAttribute.hpp Standardize the color table order (#11602) 2021-11-04 22:13:22 +00:00
textBuffer.cpp Implement Keyboard Selection (#10824) 2021-09-23 12:24:32 -07:00
textBuffer.hpp Implement Keyboard Selection (#10824) 2021-09-23 12:24:32 -07:00
textBufferCellIterator.cpp Optimize hot path in textBufferCellIterator (#10621) 2021-07-27 15:09:56 +00:00
textBufferCellIterator.hpp Expose Text Attributes to UI Automation (#10336) 2021-07-09 23:21:35 +00:00
textBufferTextIterator.cpp Some of the PR feedback. 2019-09-05 17:21:54 -07:00
textBufferTextIterator.hpp C26447,C26440 - is noexcept but can throw or doesn't throw but not noexcept 2019-08-29 15:23:07 -07:00
TextColor.cpp Standardize the color table order (#11602) 2021-11-04 22:13:22 +00:00
TextColor.h Standardize the color table order (#11602) 2021-11-04 22:13:22 +00:00
UnicodeStorage.cpp Compensate for VS 16.7, part 2 (#7383) 2020-08-24 21:21:53 +00:00
UnicodeStorage.hpp Compensate for VS 16.7, part 2 (#7383) 2020-08-24 21:21:53 +00:00