From 4246d04e5a8e4356ce6c76bbf97d56382bb411d8 Mon Sep 17 00:00:00 2001 From: MacDue Date: Sun, 17 Jul 2022 19:45:38 +0100 Subject: [PATCH] LibWeb: Calculate to angles for linear-gradients This also renames LinearGradientStyleValue::angle() to LinearGradientStyleValue::angle_degrees() to make the unit more obvious. --- Userland/Libraries/LibWeb/CSS/StyleValue.cpp | 17 +++++++++++++---- Userland/Libraries/LibWeb/CSS/StyleValue.h | 2 +- .../LibWeb/Painting/BackgroundPainting.cpp | 2 +- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp index e4fda14f4e..e049dbd65f 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp @@ -1502,9 +1502,11 @@ bool LinearGradientStyleValue::equals(StyleValue const& other_) const return true; } -float LinearGradientStyleValue::angle(Gfx::FloatRect const& background_box) const +float LinearGradientStyleValue::angle_degrees(Gfx::FloatRect const& gradient_rect) const { - (void)background_box; + auto corner_angle_degrees = [&] { + return static_cast(atan2(gradient_rect.height(), gradient_rect.width())) * 180 / AK::Pi; + }; return m_direction.visit( [&](SideOrCorner side_or_corner) { switch (side_or_corner) { @@ -1516,9 +1518,16 @@ float LinearGradientStyleValue::angle(Gfx::FloatRect const& background_box) cons return 270.0f; case SideOrCorner::Right: return 90.0f; + case SideOrCorner::TopRight: + return corner_angle_degrees(); + case SideOrCorner::BottomLeft: + return corner_angle_degrees() + 180.0f; + case SideOrCorner::TopLeft: + return -corner_angle_degrees(); + case SideOrCorner::BottomRight: + return -(corner_angle_degrees() + 180.0f); default: - // FIXME: Angle gradients towards corners - return 0.0f; + VERIFY_NOT_REACHED(); } }, [&](Angle const& angle) { diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValue.h index a9c987099e..2d2b90390e 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.h @@ -936,7 +936,7 @@ public: return m_color_stop_list; } - float angle(Gfx::FloatRect const& background_box) const; + float angle_degrees(Gfx::FloatRect const& gradient_rect) const; private: LinearGradientStyleValue(GradientDirection direction, Vector color_stop_list) diff --git a/Userland/Libraries/LibWeb/Painting/BackgroundPainting.cpp b/Userland/Libraries/LibWeb/Painting/BackgroundPainting.cpp index 77ab00bb31..9d4b1791c5 100644 --- a/Userland/Libraries/LibWeb/Painting/BackgroundPainting.cpp +++ b/Userland/Libraries/LibWeb/Painting/BackgroundPainting.cpp @@ -27,7 +27,7 @@ static Optional linear_gradient_to_gfx_gradient(CSS::LinearGradient if (linear_gradient.color_stop_list().size() != 2) return {}; - auto angle = round_to(linear_gradient.angle(background_rect)); + auto angle = round_to(linear_gradient.angle_degrees(background_rect)); auto color_a = linear_gradient.color_stop_list()[0].color_stop.color; auto color_b = linear_gradient.color_stop_list()[1].color_stop.color; auto orientation = [&]() -> Optional {