summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGfx/Gradients.h
blob: 62b848712e2400d8d3ec3706ef031e0b22474a49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
 * Copyright (c) 2023, MacDue <macdue@dueutil.tech>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Math.h>
#include <LibGfx/Color.h>
#include <LibGfx/Size.h>

namespace Gfx {

struct ColorStop {
    Color color;
    float position = AK::NaN<float>;
    Optional<float> transition_hint = {};
};

class GradientLine;

inline float normalized_gradient_angle_radians(float gradient_angle)
{
    // Adjust angle so 0 degrees is bottom
    float real_angle = 90 - gradient_angle;
    return real_angle * (AK::Pi<float> / 180);
}

template<typename T>
inline float calculate_gradient_length(Size<T> gradient_size, float sin_angle, float cos_angle)
{
    return AK::fabs(static_cast<float>(gradient_size.height()) * sin_angle) + AK::fabs(static_cast<float>(gradient_size.width()) * cos_angle);
}

template<typename T>
inline float calculate_gradient_length(Size<T> gradient_size, float gradient_angle)
{
    float angle = normalized_gradient_angle_radians(gradient_angle);
    float sin_angle, cos_angle;
    AK::sincos(angle, sin_angle, cos_angle);
    return calculate_gradient_length(gradient_size, sin_angle, cos_angle);
}

}