From 2d4281bdf21c0e0f8b4a375bec9ced4f4d615504 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bel=C3=A9n=20Albeza?= Date: Fri, 29 Nov 2024 11:33:57 +0100 Subject: [PATCH] :sparkles: Use u32 as color args for wasm --- frontend/src/app/render_wasm/api.cljs | 24 +++++++++++++----------- render-wasm/src/main.rs | 10 ++++------ 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/frontend/src/app/render_wasm/api.cljs b/frontend/src/app/render_wasm/api.cljs index 59a072c5e..52ee0e08a 100644 --- a/frontend/src/app/render_wasm/api.cljs +++ b/frontend/src/app/render_wasm/api.cljs @@ -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 @@ -96,11 +105,8 @@ 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) @@ -109,12 +115,8 @@ (:end-y gradient) opacity) (run! (fn [stop] - (let [rgb (js/parseInt (subs (:color stop) 1) 16) - r (bit-shift-right rgb 16) - 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))))) + (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 diff --git a/render-wasm/src/main.rs b/render-wasm/src/main.rs index 43d21992e..4d1e8805d 100644 --- a/render-wasm/src/main.rs +++ b/render-wasm/src/main.rs @@ -150,11 +150,10 @@ pub extern "C" fn clear_shape_children() { } #[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"); if let Some(shape) = state.current_shape() { - let alpha: u8 = (a * 0xff as f32).floor() as u8; - let color = skia::Color::from_argb(alpha, r, g, b); + let color = skia::Color::new(raw_color); shape.add_fill(shapes::Fill::from(color)); } } @@ -178,11 +177,10 @@ pub extern "C" fn add_shape_linear_fill( } #[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"); if let Some(shape) = state.current_shape() { - let alpha: u8 = (a * 0xff as f32).floor() as u8; - let color = skia::Color::from_argb(alpha, r, g, b); + let color = skia::Color::new(raw_color); shape .add_gradient_stop(color, offset) .expect("got no fill or an invalid one");