Use u32 as color args for wasm

This commit is contained in:
Belén Albeza 2024-11-29 11:33:57 +01:00
parent c8e322cd58
commit 2d4281bdf2
2 changed files with 17 additions and 17 deletions

View file

@ -8,6 +8,7 @@
"A WASM based render API" "A WASM based render API"
(:require (:require
[app.common.data.macros :as dm] [app.common.data.macros :as dm]
[app.common.math :as mth]
[app.common.uuid :as uuid] [app.common.uuid :as uuid]
[app.config :as cf] [app.config :as cf]
[app.render-wasm.helpers :as h] [app.render-wasm.helpers :as h]
@ -34,6 +35,14 @@
(h/call internal-module "_render_without_cache") (h/call internal-module "_render_without_cache")
(set! internal-frame-id nil)) (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 (defn cancel-render
[] []
(when internal-frame-id (when internal-frame-id
@ -96,11 +105,8 @@
color (:fill-color fill) color (:fill-color fill)
gradient (:fill-color-gradient fill)] gradient (:fill-color-gradient fill)]
(when ^boolean color (when ^boolean color
(let [rgb (js/parseInt (subs color 1) 16) (let [rgba (rgba-from-hex color opacity)]
r (bit-shift-right rgb 16) (h/call internal-module "_add_shape_solid_fill" rgba)))
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)))
(when (and (some? gradient) (= (:type gradient) :linear)) (when (and (some? gradient) (= (:type gradient) :linear))
(h/call internal-module "_add_shape_linear_fill" (h/call internal-module "_add_shape_linear_fill"
(:start-x gradient) (:start-x gradient)
@ -109,12 +115,8 @@
(:end-y gradient) (:end-y gradient)
opacity) opacity)
(run! (fn [stop] (run! (fn [stop]
(let [rgb (js/parseInt (subs (:color stop) 1) 16) (let [rgba (rgba-from-hex (:color stop) (:opacity stop))]
r (bit-shift-right rgb 16) (h/call internal-module "_add_shape_fill_stop" rgba (:offset stop)))) (:stops gradient)))))
g (bit-and (bit-shift-right rgb 8) 255)
b (bit-and rgb 255)
a (:opacity stop)]
(h/call internal-module "_add_shape_fill_stop" r g b a (:offset stop)))) (:stops gradient)))))
fills)) fills))
(defn- translate-blend-mode (defn- translate-blend-mode

View file

@ -150,11 +150,10 @@ pub extern "C" fn clear_shape_children() {
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn add_shape_solid_fill(r: u8, g: u8, b: u8, a: f32) { pub extern "C" fn add_shape_solid_fill(raw_color: u32) {
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer"); let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
if let Some(shape) = state.current_shape() { if let Some(shape) = state.current_shape() {
let alpha: u8 = (a * 0xff as f32).floor() as u8; let color = skia::Color::new(raw_color);
let color = skia::Color::from_argb(alpha, r, g, b);
shape.add_fill(shapes::Fill::from(color)); shape.add_fill(shapes::Fill::from(color));
} }
} }
@ -178,11 +177,10 @@ pub extern "C" fn add_shape_linear_fill(
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn add_shape_fill_stop(r: u8, g: u8, b: u8, a: f32, offset: f32) { pub extern "C" fn add_shape_fill_stop(raw_color: u32, offset: f32) {
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer"); let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
if let Some(shape) = state.current_shape() { if let Some(shape) = state.current_shape() {
let alpha: u8 = (a * 0xff as f32).floor() as u8; let color = skia::Color::new(raw_color);
let color = skia::Color::from_argb(alpha, r, g, b);
shape shape
.add_gradient_stop(color, offset) .add_gradient_stop(color, offset)
.expect("got no fill or an invalid one"); .expect("got no fill or an invalid one");