0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-04-13 15:31:26 -05:00

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"
(: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

View file

@ -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");