2023-03-24 16:17:50 +00:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2023-03-24 16:17:50 +00:00
|
|
|
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
|
|
|
|
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
|
|
|
|
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Vector.h>
|
2023-03-30 17:13:37 +01:00
|
|
|
#include <LibWeb/CSS/Angle.h>
|
|
|
|
#include <LibWeb/CSS/Percentage.h>
|
2023-03-24 16:42:50 +00:00
|
|
|
#include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h>
|
2023-03-25 14:11:11 +00:00
|
|
|
#include <LibWeb/Painting/GradientPainting.h>
|
2023-03-24 16:17:50 +00:00
|
|
|
|
|
|
|
namespace Web::CSS {
|
|
|
|
|
|
|
|
// Note: The sides must be before the corners in this enum (as this order is used in parsing).
|
|
|
|
enum class SideOrCorner {
|
|
|
|
Top,
|
|
|
|
Bottom,
|
|
|
|
Left,
|
|
|
|
Right,
|
|
|
|
TopLeft,
|
|
|
|
TopRight,
|
|
|
|
BottomLeft,
|
|
|
|
BottomRight
|
|
|
|
};
|
|
|
|
|
|
|
|
class LinearGradientStyleValue final : public AbstractImageStyleValue {
|
|
|
|
public:
|
|
|
|
using GradientDirection = Variant<Angle, SideOrCorner>;
|
|
|
|
|
|
|
|
enum class GradientType {
|
|
|
|
Standard,
|
|
|
|
WebKit
|
|
|
|
};
|
|
|
|
|
2023-08-19 14:00:10 +01:00
|
|
|
static ValueComparingNonnullRefPtr<LinearGradientStyleValue> create(GradientDirection direction, Vector<LinearColorStopListElement> color_stop_list, GradientType type, GradientRepeating repeating)
|
2023-03-24 16:17:50 +00:00
|
|
|
{
|
2025-01-30 15:15:33 +05:00
|
|
|
VERIFY(!color_stop_list.is_empty());
|
2023-08-19 14:00:10 +01:00
|
|
|
return adopt_ref(*new (nothrow) LinearGradientStyleValue(direction, move(color_stop_list), type, repeating));
|
2023-03-24 16:17:50 +00:00
|
|
|
}
|
|
|
|
|
2024-12-07 00:59:49 +01:00
|
|
|
virtual String to_string(SerializationMode) const override;
|
2023-03-24 16:17:50 +00:00
|
|
|
virtual ~LinearGradientStyleValue() override = default;
|
2024-08-14 11:10:54 +01:00
|
|
|
virtual bool equals(CSSStyleValue const& other) const override;
|
2023-03-24 16:17:50 +00:00
|
|
|
|
|
|
|
Vector<LinearColorStopListElement> const& color_stop_list() const
|
|
|
|
{
|
|
|
|
return m_properties.color_stop_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_repeating() const { return m_properties.repeating == GradientRepeating::Yes; }
|
|
|
|
|
|
|
|
float angle_degrees(CSSPixelSize gradient_size) const;
|
|
|
|
|
2023-08-16 12:45:23 +01:00
|
|
|
void resolve_for_size(Layout::NodeWithStyleAndBoxModelMetrics const&, CSSPixelSize) const override;
|
2023-03-24 16:17:50 +00:00
|
|
|
|
|
|
|
bool is_paintable() const override { return true; }
|
2024-08-06 15:26:47 +03:00
|
|
|
void paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering image_rendering) const override;
|
2023-03-24 16:17:50 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
LinearGradientStyleValue(GradientDirection direction, Vector<LinearColorStopListElement> color_stop_list, GradientType type, GradientRepeating repeating)
|
|
|
|
: AbstractImageStyleValue(Type::LinearGradient)
|
|
|
|
, m_properties { .direction = direction, .color_stop_list = move(color_stop_list), .gradient_type = type, .repeating = repeating }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Properties {
|
|
|
|
GradientDirection direction;
|
|
|
|
Vector<LinearColorStopListElement> color_stop_list;
|
|
|
|
GradientType gradient_type;
|
|
|
|
GradientRepeating repeating;
|
|
|
|
bool operator==(Properties const&) const = default;
|
|
|
|
} m_properties;
|
|
|
|
|
|
|
|
struct ResolvedData {
|
|
|
|
Painting::LinearGradientData data;
|
|
|
|
CSSPixelSize size;
|
|
|
|
};
|
|
|
|
|
|
|
|
mutable Optional<ResolvedData> m_resolved;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|