Repository: microsoft/terminal
File: src/inc/til/color.h (C/C++)

Code before:
            b{ static_cast<uint8_t>(other.b * 255.0f) },
            a{ static_cast<uint8_t>(other.a * 255.0f) }
        {
        }

        constexpr color with_alpha(uint8_t alpha) const
        {
            return color{
                r,
                g,
                b,
                alpha
            };
        }

        // source-over alpha blending/composition.
        // `this` (source/top) will be blended "over" `destination` (bottom).
        // `this` and `destination` are expected to be in straight alpha.
        // See https://en.wikipedia.org/wiki/Alpha_compositing#Description
        constexpr color layer_over(const color& destination) const
        {
            const auto sourceAlpha = a / 255.0f;
            const auto destinationAlpha = destination.a / 255.0f;
            const auto aInverse = 1.0f - sourceAlpha;

            const auto resultA = a + destination.a * aInverse;
            const auto resultR = (r * sourceAlpha + destination.r * destinationAlpha * aInverse) / resultA;
            const auto resultG = (g * sourceAlpha + destination.g * destinationAlpha * aInverse) / resultA;
            const auto resultB = (b * sourceAlpha + destination.b * destinationAlpha * aInverse) / resultA;

            return {
                static_cast<uint8_t>(resultR + 0.5f),
                static_cast<uint8_t>(resultG + 0.5f),
                static_cast<uint8_t>(resultB + 0.5f),
                static_cast<uint8_t>(resultA + 0.5f),
            };
        }

#ifdef D3DCOLORVALUE_DEFINED
        constexpr operator D3DCOLORVALUE() const
        {
            return D3DCOLORVALUE{ r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f };
        }
#endif

#ifdef WINRT_Windows_UI_H
        constexpr color(const winrt::Windows::UI::Color& winUIColor) :
            color(winUIColor.R, winUIColor.G, winUIColor.B, winUIColor.A)
        {
        }


Review comment (suggestion):
We don't need to multiply by 255 if we simply make sure that the part in front of the division is in the range `[0, 255^2]`. After dividing it by `resultA` (`[0, 255]`) we get a result in the range `[0, 255]`.

Code after:
            b{ static_cast<uint8_t>(other.b * 255.0f) },
            a{ static_cast<uint8_t>(other.a * 255.0f) }
        {
        }

        constexpr color with_alpha(uint8_t alpha) const
        {
            return color{
                r,
                g,
                b,
                alpha
            };
        }

        // source-over alpha blending/composition.
        // `this` (source/top) will be blended "over" `destination` (bottom).
        // `this` and `destination` are expected to be in straight alpha.
        // See https://en.wikipedia.org/wiki/Alpha_compositing#Description
        constexpr color layer_over(const color& destination) const
        {
            const auto aInverse = (255 - a) / 255.0f;
            const auto resultA = a + destination.a * aInverse;
            const auto resultR = (r * a + destination.r * destination.a * aInverse) / resultA;
            const auto resultG = (g * a + destination.g * destination.a * aInverse) / resultA;
            const auto resultB = (b * a + destination.b * destination.a * aInverse) / resultA;

            return {
                static_cast<uint8_t>(resultR + 0.5f),
                static_cast<uint8_t>(resultG + 0.5f),
                static_cast<uint8_t>(resultB + 0.5f),
                static_cast<uint8_t>(resultA + 0.5f),
            };
        }

#ifdef D3DCOLORVALUE_DEFINED
        constexpr operator D3DCOLORVALUE() const
        {
            return D3DCOLORVALUE{ r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f };
        }
#endif

#ifdef WINRT_Windows_UI_H
        constexpr color(const winrt::Windows::UI::Color& winUIColor) :
            color(winUIColor.R, winUIColor.G, winUIColor.B, winUIColor.A)
        {
        }

        constexpr operator winrt::Windows::UI::Color() const
        {
            return { a, r, g, b };