lab.rs
2.2 kB · rust · 71 lines
1use crate::{checked, code_of, Fault, Pixels};2use mrlycore::Colorizer;3use mrlylab::moire::{pairs, presets, render};4use mrlylab::press;5use wasm_bindgen::prelude::*;67/// Lists the first members of a design in the sequence press, each as a decimal string.8#[wasm_bindgen]9pub fn press_members(10 code: &str,11 dimension: usize,12 base: usize,13 count: usize,14) -> Result<Vec<String>, Fault> {15 let code = checked(code, dimension, base)?;16 Ok(press::members(code, dimension, base, count)17 .iter()18 .map(|m| m.to_string())19 .collect())20}2122/// Counts the members of a design below the limit, as a decimal string.23#[wasm_bindgen]24pub fn press_count_below(25 code: &str,26 dimension: usize,27 base: usize,28 limit: &str,29) -> Result<String, Fault> {30 let code = checked(code, dimension, base)?;31 Ok(press::count_below(code, dimension, base, code_of(limit)?).to_string())32}3334/// Names the moire presets.35#[wasm_bindgen]36pub fn moire_names() -> Vec<String> {37 presets::all(1).iter().map(|p| p.name.to_string()).collect()38}3940/// Samples a named moire preset up to the scale limit into a square of the size and renders it through the fire, heat or diverge ramp into pixels.41#[wasm_bindgen]42pub fn moire(43 name: &str,44 limit: usize,45 size: usize,46 ramp: &str,47 levels: usize,48 invert: bool,49) -> Result<Pixels, Fault> {50 let field = presets::named(name, limit)?.field(size)?;51 let colorizer = match ramp {52 "heat" => Colorizer::heat(),53 "diverge" => Colorizer::diverge(),54 _ => Colorizer::fire(),55 };56 let png = render(&field, &colorizer, levels, false, invert, 1)?;57 let (width, height, colors) = mrlycore::unpng(&png)?;58 Ok(Pixels::of(width, height, colors))59}6061/// Returns the exact Pearson correlation of the flat carpet layers at two scales, area-weighted on their lcm grid, which is zero exactly when the two odd scales are coprime.62#[wasm_bindgen]63pub fn moire_correlation(m: usize, n: usize) -> f64 {64 pairs::correlation(m, n)65}6667/// Lists the odd scales a moire stack samples up to the limit.68#[wasm_bindgen]69pub fn odd_scales(limit: usize) -> Vec<u32> {70 (1..=limit.max(1)).step_by(2).map(|n| n as u32).collect()71}