Merge pull request #5392 from penpot/ladybenko-9266-linear-gradients

🎉 Render linear gradient fills
This commit is contained in:
Alejandro 2024-12-02 12:33:56 +01:00 committed by GitHub
commit 61bf4e145e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 184 additions and 76 deletions

View file

@ -8,6 +8,7 @@
"A WASM based render API"
(:require
[app.common.data.macros :as dm]
[app.common.math :as mth]
[app.common.uuid :as uuid]
[app.config :as cf]
[app.render-wasm.helpers :as h]
@ -34,6 +35,14 @@
(h/call internal-module "_render_without_cache")
(set! internal-frame-id nil))
(defn- rgba-from-hex
"Takes a hex color in #rrggbb format, and an opacity value from 0 to 1 and returns its 32-bit rgba representation"
[hex opacity]
(let [rgb (js/parseInt (subs hex 1) 16)
a (mth/floor (* (or opacity 1) 0xff))]
;; rgba >>> 0 so we have an unsigned representation
(unsigned-bit-shift-right (bit-or (bit-shift-left a 24) rgb) 0)))
(defn cancel-render
[]
(when internal-frame-id
@ -92,14 +101,22 @@
[fills]
(h/call internal-module "_clear_shape_fills")
(run! (fn [fill]
(let [opacity (:fill-opacity fill)
color (:fill-color fill)]
(let [opacity (or (:fill-opacity fill) 1.0)
color (:fill-color fill)
gradient (:fill-color-gradient fill)]
(when ^boolean color
(let [rgb (js/parseInt (subs color 1) 16)
r (bit-shift-right rgb 16)
g (bit-and (bit-shift-right rgb 8) 255)
b (bit-and rgb 255)]
(h/call internal-module "_add_shape_solid_fill" r g b opacity)))))
(let [rgba (rgba-from-hex color opacity)]
(h/call internal-module "_add_shape_solid_fill" rgba)))
(when (and (some? gradient) (= (:type gradient) :linear))
(h/call internal-module "_add_shape_linear_fill"
(:start-x gradient)
(:start-y gradient)
(:end-x gradient)
(:end-y gradient)
opacity)
(run! (fn [stop]
(let [rgba (rgba-from-hex (:color stop) (:opacity stop))]
(h/call internal-module "_add_shape_fill_stop" rgba (:offset stop)))) (:stops gradient)))))
fills))
(defn- translate-blend-mode