ladybird/Userland/Libraries/LibGfx/Gradients.h
MacDue c8c065b6b0 LibWeb+LibGfx: Migrate (most of) the CSS gradient painting to LibGfx
This moves the CSS gradient painting to the painter creating:

 - Painter::fill_rect_with_linear_gradient()
 - Painter::fill_rect_with_conic_gradient()
 - Painter::fill_rect_with_radial_gradient()

This has a few benefits:
 - The gradients can now easily respect the painter scale
 - The Painter::fill_pixels() escape hatch can be removed
 - We can remove the old fixed color stop gradient code
    - The old functions are  now just a shim
 - Anywhere can now easily use this gradient painting code!

This only leaves the color stop resolution in LibWeb (which is fine).
Just means in LibGfx you have to actually specify color stop positions.

(Also while here add a small optimization to avoid generating
excessively long gradient lines)
2023-01-10 10:25:58 +01:00

45 lines
1.1 KiB
C++

/*
* 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);
}
}