Repository: microsoft/terminal
File: src/cascadia/TerminalApp/TerminalTab.cpp (C++)

Code before:
    // - <none>
    void TerminalTab::_RecalculateAndApplyTabColor()
    {
        auto weakThis{ get_weak() };

        TabViewItem().Dispatcher().RunAsync(CoreDispatcherPriority::Normal, [weakThis]() {
            auto ptrTab = weakThis.get();
            if (!ptrTab)
                return;

            auto tab{ ptrTab };

            // GetTabColor will return the color set by the color picker, or the
            // color specified in the profile. If neither of those were set,
            // then look to _themeColor to see if there's a value there.
            // Otherwise, clear our color, falling back to the TabView defaults.
            auto currentColor = tab->GetTabColor();
            if (currentColor.has_value())
            {
                tab->_ApplyTabColor(currentColor.value());
            }
            else if (tab->_themeColor != nullptr)
            {
                // One-liner to safely get the active control's brush.
                const auto terminalBrush =
                    [tab]() -> Media::Brush { if (const auto& c{ tab->GetActiveTerminalControl() }){ return c.BackgroundBrush(); } return nullptr; }();

                if (const auto themeBrush{ tab->_themeColor.Evaluate(Application::Current().Resources(), terminalBrush, false) })
                {
                    // ThemeColor.Evaluate will get us a Brush (because the
                    // TermControl could have an acrylic BG, for example). Take
                    // that brush, and get the color out of it. We don't really
                    // want to have the tab items themselves be acrylic.
                    tab->_ApplyTabColor(til::color{ ThemeColor::ColorFromBrush(themeBrush) });
                }
                else
                {
                    tab->_ClearTabBackgroundColor();
                }
            }
            else
            {
                tab->_ClearTabBackgroundColor();
            }
        });
    }

    // Method Description:
    // - Applies the given color to the background of this tab's TabViewItem.
    // - Sets the tab foreground color depending on the luminance of
    // the background color

Review comment (bug):
```cpp
Media::Brush terminalBrush{ nullptr };
if (const auto& c{ tab->GetActiveTerminalControl() }){
    terminalBrush = c.BackgroundBrush();
}
```
would be more consistent.

Code after:
    // - <none>
    void TerminalTab::_RecalculateAndApplyTabColor()
    {
        auto weakThis{ get_weak() };

        TabViewItem().Dispatcher().RunAsync(CoreDispatcherPriority::Normal, [weakThis]() {
            auto ptrTab = weakThis.get();
            if (!ptrTab)
                return;

            auto tab{ ptrTab };

            // GetTabColor will return the color set by the color picker, or the
            // color specified in the profile. If neither of those were set,
            // then look to _themeColor to see if there's a value there.
            // Otherwise, clear our color, falling back to the TabView defaults.
            auto currentColor = tab->GetTabColor();
            if (currentColor.has_value())
            {
                tab->_ApplyTabColor(currentColor.value());
            }
            else if (tab->_themeColor != nullptr)
            {
                // One-liner to safely get the active control's brush.
                Media::Brush terminalBrush{ nullptr };
                if (const auto& c{ tab->GetActiveTerminalControl() })
                {
                    terminalBrush = c.BackgroundBrush();
                }

                if (const auto themeBrush{ tab->_themeColor.Evaluate(Application::Current().Resources(), terminalBrush, false) })
                {
                    // ThemeColor.Evaluate will get us a Brush (because the
                    // TermControl could have an acrylic BG, for example). Take
                    // that brush, and get the color out of it. We don't really
                    // want to have the tab items themselves be acrylic.
                    tab->_ApplyTabColor(til::color{ ThemeColor::ColorFromBrush(themeBrush) });
                }
                else
                {
                    tab->_ClearTabBackgroundColor();
                }
            }
            else
            {
                tab->_ClearTabBackgroundColor();
            }
        });
    }

    // Method Description: