Ver Fonte

LibWeb: Calculate to <corner> angles for linear-gradients

This also renames LinearGradientStyleValue::angle() to
LinearGradientStyleValue::angle_degrees() to make the unit more
obvious.
MacDue há 3 anos atrás
pai
commit
4246d04e5a

+ 13 - 4
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<float>(atan2(gradient_rect.height(), gradient_rect.width())) * 180 / AK::Pi<float>;
+    };
     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) {

+ 1 - 1
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<ColorStopListElement> color_stop_list)

+ 1 - 1
Userland/Libraries/LibWeb/Painting/BackgroundPainting.cpp

@@ -27,7 +27,7 @@ static Optional<GfxGradient> linear_gradient_to_gfx_gradient(CSS::LinearGradient
     if (linear_gradient.color_stop_list().size() != 2)
         return {};
 
-    auto angle = round_to<int>(linear_gradient.angle(background_rect));
+    auto angle = round_to<int>(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<Gfx::Orientation> {