增加Swatch Scheme中必要结构体的导出函数。

This commit is contained in:
徐涛
2025-02-07 13:06:50 +08:00
parent e9c2d4cb16
commit 8e71d3c555
6 changed files with 464 additions and 363 deletions

View File

@@ -1,4 +1,5 @@
use palette::FromColor;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::str::FromStr;
@@ -19,13 +20,29 @@ pub struct SwatchScheme {
dark: HashMap<String, Swatch>,
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[wasm_bindgen(getter_with_clone)]
pub struct SwatchEntry {
pub name: String,
pub color: String,
}
#[wasm_bindgen]
impl SwatchEntry {
#[wasm_bindgen(constructor)]
pub fn new(name: &str, color: &str) -> Self {
Self {
name: name.to_string(),
color: color.to_string(),
}
}
#[wasm_bindgen(js_name = toJsValue)]
pub fn to_js_value(&self) -> Result<JsValue, JsError> {
Ok(serde_wasm_bindgen::to_value(self)?)
}
}
impl SwatchScheme {
pub fn new(
colors: Vec<SwatchEntry>,

View File

@@ -1,5 +1,5 @@
use serde::Serialize;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::{prelude::wasm_bindgen, JsError, JsValue};
use crate::schemes::q_style::ColorShifting;
@@ -27,3 +27,28 @@ impl Default for SwatchSchemeSetting {
}
}
}
#[wasm_bindgen]
impl SwatchSchemeSetting {
#[wasm_bindgen(constructor)]
pub fn new(
amount: usize,
min_lightness: f32,
max_lightness: f32,
include_primary: bool,
dark_convert: ColorShifting,
) -> Self {
Self {
amount,
min_lightness,
max_lightness,
include_primary,
dark_convert,
}
}
#[wasm_bindgen(js_name = toJsValue)]
pub fn to_js_value(&self) -> Result<JsValue, JsError> {
Ok(serde_wasm_bindgen::to_value(self)?)
}
}