AUI Framework  master
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
Gradient.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 Alex2772 and Contributors
    4 *
    5 * SPDX-License-Identifier: MPL-2.0
    6 *
    7 * This Source Code Form is subject to the terms of the Mozilla Public
    8 * License, v. 2.0. If a copy of the MPL was not distributed with this
    9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   10 */
   11
   12#pragma once
   13
   14#include "AUI/Image/AImage.h"
   15#include "AUI/Render/ABrush.h"
   16
   17namespace aui::render::brush::gradient {
   18    struct Helper {
   19    public:
   20        ASmallVector<glm::u8vec4, 8> colors;
   21        glm::mat3 matrix;
   22
   23        Helper(const ALinearGradientBrush& brush) {
   24            for (const auto& c : brush.colors) {
   25                colors.push_back(glm::uvec4(c.color * 255.f));
   26            }
   27            //float actualEdgeUvPosition = (0.5f - 0.05f /* bias to make ideal color edge */) / float(colors.size());
   28            float actualEdgeUvPosition = 0.f; // temporary
   29
   30            float rotationRadians = brush.rotation.radians();
   31
   32            float s = glm::sin(rotationRadians);
   33            float c = -glm::cos(rotationRadians);
   34            float bias = glm::max(-s, -c, 0.f);
   35
   36            auto probe = [&](glm::vec2 uv) {
   37                return uv.x * s + uv.y * c + bias;
   38            };
   39            auto adjust = [&](glm::vec2 uv) {
   40                float p = probe(uv);
   41                if (p > 1.f) {
   42                    s /= p;
   43                    c /= p;
   44                    bias /= p;
   45                }
   46            };
   47
   48            if (auto p = probe({1, 1}); p < 0.f) {
   49                bias += -p;
   50            }
   51
   52            adjust({0, 0});
   53            adjust({1, 0});
   54            adjust({0, 1});
   55            adjust({1, 1});
   56
   57            matrix = {
   58                /* uv.x */ s * (1.f - actualEdgeUvPosition * 2.f), 0.f, 0.f,
   59                /* uv.y */ c * (1.f - actualEdgeUvPosition * 2.f), 0.f, 0.f,
   60                /* 1    */ bias * (1.f - actualEdgeUvPosition * 2.f) + actualEdgeUvPosition, 0.f, 0.f,
   61            };
   62        }
   63
   64        [[nodiscard]]
   65        AImageView gradientMap() const noexcept {
   66            return AImageView({(const char*)colors.data(), colors.sizeInBytes()}, {colors.size(), 1}, APixelFormat::RGBA_BYTE);
   67        }
   68    };
   69}
Non-owning read-only image representation of some format.
Definition AImageView.h:58
Vector-like container consisting of few elements on stack and switches to dynamic allocation vector i...
Definition ASmallVector.h:34
Definition ABrush.h:48