Repository: microsoft/terminal
File: src/cascadia/WindowsTerminal/AppHost.cpp (C++)

Code before:
{
    // Instantiate the frame color timer, if we haven't already. We'll only ever
    // create one instance of this. We'll set up the callback for the timers as
    // _updateFrameColor, which will actually handle setting the colors. If we
    // already have a timer, just start that one.

    if (_frameTimer == nullptr)
    {
        _frameTimer = winrt::Windows::UI::Xaml::DispatcherTimer();
        _frameTimer.Interval(FrameUpdateInterval);
        _frameTimerToken = _frameTimer.Tick({ this, &AppHost::_updateFrameColor });
    }
    _frameTimer.Start();
}

void AppHost::_stopFrameTimer()
{
    if (_frameTimer)
    {
        _frameTimer.Stop();
    }
}

// Method Description:
// - Updates the color of the window frame to cycle through all the colors. This
//   is called as the `_frameTimer` Tick callback, roughly 60 times per second.
void AppHost::_updateFrameColor(const winrt::Windows::Foundation::IInspectable&, const winrt::Windows::Foundation::IInspectable&)
{
    // First, a couple helper functions:
    static const auto fmod_1 = [](const float x) -> float {
        float integer = floor(x);
        return x - integer;
    };

    static const auto saturateAndToColor = [](const float a, const float b, const float c) -> til::color {
        return til::color{
            base::saturated_cast<uint8_t>(255.f * std::clamp(a, 0.f, 1.f)),
            base::saturated_cast<uint8_t>(255.f * std::clamp(b, 0.f, 1.f)),
            base::saturated_cast<uint8_t>(255.f * std::clamp(c, 0.f, 1.f))
        };
    };

    // Helper for converting a hue [0, 1) to an RGB value.
    // Credit to https://www.chilliant.com/rgb2hsv.html
    static const auto hueToRGB = [&](const float H) -> til::color {
        float R = abs(H * 6 - 3) - 1;
        float G = 2 - abs(H * 6 - 2);
        float B = 2 - abs(H * 6 - 4);
        return saturateAndToColor(R, G, B);
    };


Review comment (performance):
BTW This is one of the things we should probably put into the renderer and not the UI parts. Over RDP it'll run too fast (<= 30Hz) and on modern PCs too slow (>= 120Hz). It's similar to our scrollbar updates, etc.

Code after:
{
    // Instantiate the frame color timer, if we haven't already. We'll only ever
    // create one instance of this. We'll set up the callback for the timers as
    // _updateFrameColor, which will actually handle setting the colors. If we
    // already have a timer, just start that one.

    if (_frameTimer == nullptr)
    {
        _frameTimer = winrt::Windows::UI::Xaml::DispatcherTimer();
        _frameTimer.Interval(FrameUpdateInterval);
        _frameTimerToken = _frameTimer.Tick({ this, &AppHost::_updateFrameColor });
    }
    _frameTimer.Start();
}

void AppHost::_stopFrameTimer()
{
    if (_frameTimer)
    {
        _frameTimer.Stop();
    }
}

// Method Description:
// - Updates the color of the window frame to cycle through all the colors. This
//   is called as the `_frameTimer` Tick callback, roughly 60 times per second.
void AppHost::_updateFrameColor(const winrt::Windows::Foundation::IInspectable&, const winrt::Windows::Foundation::IInspectable&)
{
    // First, a couple helper functions:
    static const auto saturateAndToColor = [](const float a, const float b, const float c) -> til::color {
        return til::color{
            base::saturated_cast<uint8_t>(255.f * std::clamp(a, 0.f, 1.f)),
            base::saturated_cast<uint8_t>(255.f * std::clamp(b, 0.f, 1.f)),
            base::saturated_cast<uint8_t>(255.f * std::clamp(c, 0.f, 1.f))
        };
    };

    // Helper for converting a hue [0, 1) to an RGB value.
    // Credit to https://www.chilliant.com/rgb2hsv.html
    static const auto hueToRGB = [&](const float H) -> til::color {
        float R = abs(H * 6 - 3) - 1;
        float G = 2 - abs(H * 6 - 2);
        float B = 2 - abs(H * 6 - 4);
        return saturateAndToColor(R, G, B);
    };

    // Now, the main body of work.
    // - Convert the time delta between when we were started and now, to a hue. This will cycle us through all the colors.
    // - Convert that hue to an RGB value.
    // - Set the frame's color to that RGB color.
    const auto now = std::chrono::high_resolution_clock::now();