From 78a6646b75cf83fbfa7d77556d98cc51ff8b5c02 Mon Sep 17 00:00:00 2001
From: Harry Hogg <harry@hogg.io>
Date: Sun, 15 Dec 2024 09:00:11 +0000
Subject: [PATCH] Renamed ColorMode to ColorPalette

---
 .../gap-validation/src/Article/index.tsx      |  2 +-
 .../renderer/src/draw/layers/draw_shapes.rs   | 59 ++++---------------
 .../src-rust/renderer/src/draw/options.rs     |  7 +--
 .../src-rust/tiling/src/build/plane.rs        |  5 ++
 .../ArrangementCard.tsx                       |  4 +-
 .../Presentation/Renderer/defaultOptions.ts   |  6 +-
 .../src/Presentation/Settings/Settings.tsx    | 18 +++---
 .../Settings/SettingsProvider.tsx             | 10 ++--
 .../Settings/useSettingsContext.ts            |  8 +--
 workspaces/tilings/src/Presentation/index.tsx |  6 +-
 workspaces/wasm/src/types.ts                  |  5 +-
 .../src/pages/TilingGenerationPage.tsx        |  4 +-
 12 files changed, 50 insertions(+), 84 deletions(-)

diff --git a/workspaces/gap-validation/src/Article/index.tsx b/workspaces/gap-validation/src/Article/index.tsx
index c1aba8f..6f5d92c 100644
--- a/workspaces/gap-validation/src/Article/index.tsx
+++ b/workspaces/gap-validation/src/Article/index.tsx
@@ -32,7 +32,7 @@ const Article = () => {
               height="200px"
               notation="12-3,4,6,4,3,4,6,4,3,4,6,4"
               options={{
-                colorMode: ColorMode.None,
+                colorPalette: colorPalette.None,
               }}
             />
           </ArticleFig>
diff --git a/workspaces/tilings/src-rust/renderer/src/draw/layers/draw_shapes.rs b/workspaces/tilings/src-rust/renderer/src/draw/layers/draw_shapes.rs
index bc93da5..f8d7aa1 100644
--- a/workspaces/tilings/src-rust/renderer/src/draw/layers/draw_shapes.rs
+++ b/workspaces/tilings/src-rust/renderer/src/draw/layers/draw_shapes.rs
@@ -1,14 +1,14 @@
 use std::collections::HashMap;
 
 use anyhow::Result;
-use colorgrad::{Gradient, GradientBuilder, GradientBuilderError};
+use colorgrad::{Gradient, GradientBuilder};
 use rand::seq::SliceRandom;
 use rand::thread_rng;
-use tiling::{Tiling, TilingError};
+use tiling::Tiling;
 
 use super::Layer;
 use crate::canvas::{Canvas, Polygon};
-use crate::draw::options::ColorMode;
+use crate::draw::options::ColorPalette;
 use crate::draw::Options;
 use crate::Error;
 
@@ -17,59 +17,33 @@ pub const VAPOR_WAVE_COLOR_PALETTE: [&str; 12] = [
   "#0DFDF9", "#F52E97", "#94157F", "#F9AB53",
 ];
 
-pub fn create_custom_gradient(
-  domain_min: usize,
-  domain_max: usize,
-  colors: [&str; 2],
-) -> Result<colorgrad::LinearGradient, GradientBuilderError> {
-  GradientBuilder::new()
-    .html_colors(&colors)
-    .domain(&[domain_min as f32, domain_max as f32])
-    .mode(colorgrad::BlendMode::Rgb)
-    .build::<colorgrad::LinearGradient>()
-}
-
-pub fn create_invalid_mode_gradient(
-  domain_min: usize,
-  domain_max: usize,
-) -> Result<colorgrad::LinearGradient, GradientBuilderError> {
-  create_custom_gradient(domain_min, domain_max, ["#e64980", "#a61e4d"])
-}
-
-pub fn create_valid_mode_gradient(
-  domain_min: usize,
-  domain_max: usize,
-) -> Result<colorgrad::LinearGradient, GradientBuilderError> {
-  create_custom_gradient(domain_min, domain_max, ["#12b886", "#087f5b"])
-}
-
 pub fn draw_shapes(canvas: &mut Canvas, options: &Options, tiling: &Tiling) -> Result<(), Error> {
   let path_shape_count = tiling.notation.path.get_shape_count() as f32;
-  let color_mode = options.color_mode.clone().unwrap_or_default();
+  let color_palette = options.color_palette.clone().unwrap_or_default();
   let shape_style = options.styles.shape.clone().unwrap_or_default();
 
   if path_shape_count == 0.0 {
     return Ok(());
   }
 
-  let gradient = {
-    match color_mode {
-      ColorMode::None => None,
-      ColorMode::BlackAndWhite => Some(
+  let gradient: Option<colorgrad::LinearGradient> = {
+    match color_palette {
+      ColorPalette::None => None,
+      ColorPalette::BlackAndWhite => Some(
         GradientBuilder::new()
           .html_colors(&["#000000", "#ffffff"])
           .domain(&[0.0, path_shape_count])
           .mode(colorgrad::BlendMode::Rgb)
           .build()?,
       ),
-      ColorMode::VaporWave => Some(
+      ColorPalette::VaporWave => Some(
         GradientBuilder::new()
           .html_colors(&VAPOR_WAVE_COLOR_PALETTE)
           .domain(&[0.0, path_shape_count])
           .mode(colorgrad::BlendMode::Rgb)
           .build()?,
       ),
-      ColorMode::VaporWaveRandom => {
+      ColorPalette::VaporWaveRandom => {
         let mut colors = VAPOR_WAVE_COLOR_PALETTE.to_vec();
 
         colors.shuffle(&mut thread_rng());
@@ -82,19 +56,6 @@ pub fn draw_shapes(canvas: &mut Canvas, options: &Options, tiling: &Tiling) -> R
             .build()?,
         )
       }
-      ColorMode::Validity => {
-        if let Some(is_valid) = options.is_valid {
-          if is_valid {
-            Some(create_valid_mode_gradient(0, path_shape_count as usize)?)
-          } else {
-            Some(create_invalid_mode_gradient(0, path_shape_count as usize)?)
-          }
-        } else if !matches!(tiling.error, TilingError::Noop) {
-          Some(create_invalid_mode_gradient(0, path_shape_count as usize)?)
-        } else {
-          Some(create_valid_mode_gradient(0, path_shape_count as usize)?)
-        }
-      }
     }
   };
 
diff --git a/workspaces/tilings/src-rust/renderer/src/draw/options.rs b/workspaces/tilings/src-rust/renderer/src/draw/options.rs
index 478d07f..e758036 100644
--- a/workspaces/tilings/src-rust/renderer/src/draw/options.rs
+++ b/workspaces/tilings/src-rust/renderer/src/draw/options.rs
@@ -12,7 +12,7 @@ use super::layers::Layer;
 #[typeshare]
 pub struct Options {
   pub auto_rotate: Option<bool>,
-  pub color_mode: Option<ColorMode>,
+  pub color_palette: Option<ColorPalette>,
   pub is_valid: Option<bool>,
   pub max_stage: Option<u16>,
   pub padding: Option<f64>,
@@ -38,15 +38,14 @@ pub struct Styles {
 
 #[derive(Clone, Deserialize, Serialize)]
 #[typeshare]
-pub enum ColorMode {
+pub enum ColorPalette {
   BlackAndWhite,
   None,
-  Validity,
   VaporWave,
   VaporWaveRandom,
 }
 
-impl Default for ColorMode {
+impl Default for ColorPalette {
   fn default() -> Self {
     Self::VaporWaveRandom
   }
diff --git a/workspaces/tilings/src-rust/tiling/src/build/plane.rs b/workspaces/tilings/src-rust/tiling/src/build/plane.rs
index 4e3be64..831e168 100644
--- a/workspaces/tilings/src-rust/tiling/src/build/plane.rs
+++ b/workspaces/tilings/src-rust/tiling/src/build/plane.rs
@@ -42,6 +42,8 @@ pub struct Plane {
   #[serde(skip)]
   pub line_segments_by_shape_group: Vec<BTreeSet<LineSegment>>,
   #[serde(skip)]
+  pub polygons_to_transform: Vec<Polygon>,
+  #[serde(skip)]
   pub stage_added_polygon: bool,
   #[serde(skip)]
   pub stage_started_transforms: bool,
@@ -73,6 +75,7 @@ impl Plane {
     self.points_mid = SpatialGridMap::default().with_resize_method(ResizeMethod::First);
     self.polygons = SpatialGridMap::default().with_resize_method(ResizeMethod::Maximum);
     self.polygons_placement = SpatialGridMap::default().with_resize_method(ResizeMethod::Maximum);
+    self.polygons_to_transform = Vec::new();
     self.seed_polygon = None;
     self.stage_added_polygon = false;
     self.stages = Vec::new();
@@ -586,6 +589,8 @@ impl Plane {
       let p2 = Point::at((value - PI * 0.5).cos(), (value - PI * 0.5).sin());
       let line_segment = LineSegment::default().with_start(p1).with_end(p2);
 
+      // TODO: We don't need to iterate over all of the polygons.
+
       self
         .polygons
         .to_owned()
diff --git a/workspaces/tilings/src/Presentation/ArrangementInformation/ArrangementCard.tsx b/workspaces/tilings/src/Presentation/ArrangementInformation/ArrangementCard.tsx
index 295d25c..bb3626a 100644
--- a/workspaces/tilings/src/Presentation/ArrangementInformation/ArrangementCard.tsx
+++ b/workspaces/tilings/src/Presentation/ArrangementInformation/ArrangementCard.tsx
@@ -1,5 +1,5 @@
 import { DeepPartial, PatternBackground } from '@hogg/common';
-import { Options, ColorMode, ScaleMode, Layer } from '@hogg/wasm';
+import { Options, ColorPalette, ScaleMode, Layer } from '@hogg/wasm';
 import { Box, Text } from 'preshape';
 import TilingRenderer, { TilingRendererProps } from '../../TilingRenderer';
 
@@ -9,7 +9,7 @@ export type ArrangementCardProps = Omit<TilingRendererProps, 'notation'> & {
 };
 
 const options: DeepPartial<Options> = {
-  colorMode: ColorMode.None,
+  colorPalette: ColorPalette.None,
   scaleMode: ScaleMode.Contain,
   showLayers: {
     [Layer.ConvexHull]: false,
diff --git a/workspaces/tilings/src/Presentation/Renderer/defaultOptions.ts b/workspaces/tilings/src/Presentation/Renderer/defaultOptions.ts
index 51b8c3b..cf95a92 100644
--- a/workspaces/tilings/src/Presentation/Renderer/defaultOptions.ts
+++ b/workspaces/tilings/src/Presentation/Renderer/defaultOptions.ts
@@ -1,4 +1,4 @@
-import { Options, ColorMode, ScaleMode, Layer } from '@hogg/wasm';
+import { Options, ColorPalette, ScaleMode, Layer } from '@hogg/wasm';
 import { ThemeColorMap, colorNegativeShade4 } from 'preshape';
 
 type NoUndefinedField<T> = {
@@ -9,10 +9,10 @@ export const defaultExpansionPhases = 10;
 
 export const defaultOptions: Pick<
   NoUndefinedField<Options>,
-  'autoRotate' | 'colorMode' | 'scaleMode' | 'showLayers'
+  'autoRotate' | 'colorPalette' | 'scaleMode' | 'showLayers'
 > = {
   autoRotate: true,
-  colorMode: ColorMode.VaporWave,
+  colorPalette: ColorPalette.VaporWave,
   scaleMode: ScaleMode.Cover,
   showLayers: {
     [Layer.Axis]: false,
diff --git a/workspaces/tilings/src/Presentation/Settings/Settings.tsx b/workspaces/tilings/src/Presentation/Settings/Settings.tsx
index c91051c..9689111 100644
--- a/workspaces/tilings/src/Presentation/Settings/Settings.tsx
+++ b/workspaces/tilings/src/Presentation/Settings/Settings.tsx
@@ -1,4 +1,4 @@
-import { ColorMode, Layer, ScaleMode } from '@hogg/wasm';
+import { ColorPalette, Layer, ScaleMode } from '@hogg/wasm';
 import {
   GaugeIcon,
   LayersIcon,
@@ -24,13 +24,13 @@ const layersOptions = Object.values(Layer).sort((a, b) => a.localeCompare(b));
 export default function Settings() {
   const {
     autoRotate,
-    colorMode,
+    colorPalette,
     expansionPhases,
     scaleMode,
     showLayers,
     showSettings,
     setAutoRotate,
-    setColorMode,
+    setColorPalette,
     setExpansionPhases,
     setScaleMode,
     setShowLayers,
@@ -70,13 +70,13 @@ export default function Settings() {
     onChange: setSpeed,
   };
 
-  const colorModeConfig: MenuConfigEntryOneOf<ColorMode> = {
-    label: 'Color mode',
+  const colorPaletteConfig: MenuConfigEntryOneOf<ColorPalette> = {
+    label: 'Color palette',
     icon: PaletteIcon,
     type: 'oneOf',
-    value: colorMode,
-    options: Object.values(ColorMode),
-    onChange: setColorMode,
+    value: colorPalette,
+    options: Object.values(ColorPalette),
+    onChange: setColorPalette,
   };
 
   const autoRotateConfig: MenuConfigEntryBoolean = {
@@ -120,7 +120,7 @@ export default function Settings() {
       <ConfigMenu
         config={[
           autoRotateConfig,
-          colorModeConfig,
+          colorPaletteConfig,
           expansionPhasesConfig,
           showLayersConfig,
           scaleModeConfig,
diff --git a/workspaces/tilings/src/Presentation/Settings/SettingsProvider.tsx b/workspaces/tilings/src/Presentation/Settings/SettingsProvider.tsx
index 06f1f8b..5abff51 100644
--- a/workspaces/tilings/src/Presentation/Settings/SettingsProvider.tsx
+++ b/workspaces/tilings/src/Presentation/Settings/SettingsProvider.tsx
@@ -1,4 +1,4 @@
-import { ColorMode, ScaleMode } from '@hogg/wasm';
+import { ColorPalette, ScaleMode } from '@hogg/wasm';
 import { PropsWithChildren, useState } from 'react';
 import {
   Settings,
@@ -20,7 +20,9 @@ export default function SettingsProvider({
   };
 
   const [autoRotate, setAutoRotate] = useState(initialState.autoRotate);
-  const [colorMode, setColorMode] = useState<ColorMode>(initialState.colorMode);
+  const [colorPalette, setColorPalette] = useState<ColorPalette>(
+    initialState.colorPalette
+  );
   const [expansionPhases, setExpansionPhases] = useState(
     initialState.expansionPhases
   );
@@ -32,12 +34,12 @@ export default function SettingsProvider({
 
   const value = {
     autoRotate,
-    colorMode,
+    colorPalette,
     expansionPhases,
     scaleMode,
     showLayers,
     setAutoRotate,
-    setColorMode,
+    setColorPalette,
     setExpansionPhases,
     setScaleMode,
     setShowLayers,
diff --git a/workspaces/tilings/src/Presentation/Settings/useSettingsContext.ts b/workspaces/tilings/src/Presentation/Settings/useSettingsContext.ts
index b6aa160..4c386b2 100644
--- a/workspaces/tilings/src/Presentation/Settings/useSettingsContext.ts
+++ b/workspaces/tilings/src/Presentation/Settings/useSettingsContext.ts
@@ -1,4 +1,4 @@
-import { ColorMode, ScaleMode, Layer } from '@hogg/wasm';
+import { ColorPalette, ScaleMode, Layer } from '@hogg/wasm';
 import { createContext, useContext } from 'react';
 import {
   defaultExpansionPhases,
@@ -7,7 +7,7 @@ import {
 
 export type Settings = {
   autoRotate: boolean;
-  colorMode: ColorMode;
+  colorPalette: ColorPalette;
   expansionPhases: number;
   scaleMode: ScaleMode;
   showLayers: Record<Layer, boolean>;
@@ -15,7 +15,7 @@ export type Settings = {
 
 type SettingsContextValue = Settings & {
   setAutoRotate: (autoRotate: boolean) => void;
-  setColorMode: (colorMode: ColorMode) => void;
+  setColorPalette: (colorPalette: ColorPalette) => void;
   setExpansionPhases: (count: number) => void;
   setScaleMode: (scaleMode: ScaleMode) => void;
   setShowLayers: (layers: Record<Layer, boolean>) => void;
@@ -38,7 +38,7 @@ export const SettingsContext = createContext<SettingsContextValue>({
   ...defaultSettings,
   showSettings: false,
   setAutoRotate: noop,
-  setColorMode: noop,
+  setColorPalette: noop,
   setExpansionPhases: noop,
   setScaleMode: noop,
   setShowLayers: noop,
diff --git a/workspaces/tilings/src/Presentation/index.tsx b/workspaces/tilings/src/Presentation/index.tsx
index abcd087..058e505 100644
--- a/workspaces/tilings/src/Presentation/index.tsx
+++ b/workspaces/tilings/src/Presentation/index.tsx
@@ -24,7 +24,7 @@ function PresentationInner(props: RendererProps) {
     setShowSettings,
     autoRotate,
     expansionPhases,
-    colorMode,
+    colorPalette,
     scaleMode,
     showLayers,
   } = useSettingsContext();
@@ -33,12 +33,12 @@ function PresentationInner(props: RendererProps) {
   const options = useMemo(
     () => ({
       autoRotate,
-      colorMode,
+      colorPalette,
       maxStage,
       scaleMode,
       showLayers,
     }),
-    [autoRotate, colorMode, maxStage, scaleMode, showLayers]
+    [autoRotate, colorPalette, maxStage, scaleMode, showLayers]
   );
 
   return (
diff --git a/workspaces/wasm/src/types.ts b/workspaces/wasm/src/types.ts
index 5d4f39d..90c3813 100644
--- a/workspaces/wasm/src/types.ts
+++ b/workspaces/wasm/src/types.ts
@@ -178,10 +178,9 @@ export interface Style {
 	opacity?: number;
 }
 
-export enum ColorMode {
+export enum ColorPalette {
 	BlackAndWhite = "BlackAndWhite",
 	None = "None",
-	Validity = "Validity",
 	VaporWave = "VaporWave",
 	VaporWaveRandom = "VaporWaveRandom",
 }
@@ -221,7 +220,7 @@ export interface Styles {
 
 export interface Options {
 	autoRotate?: boolean;
-	colorMode?: ColorMode;
+	colorPalette?: ColorPalette;
 	isValid?: boolean;
 	maxStage?: number;
 	padding?: number;
diff --git a/workspaces/website/src/pages/TilingGenerationPage.tsx b/workspaces/website/src/pages/TilingGenerationPage.tsx
index 76f4e85..81ad6f0 100644
--- a/workspaces/website/src/pages/TilingGenerationPage.tsx
+++ b/workspaces/website/src/pages/TilingGenerationPage.tsx
@@ -1,10 +1,10 @@
 import { TilingRenderer } from '@hogg/tilings';
-import { ColorMode, Options, ScaleMode } from '@hogg/wasm';
+import { ColorPalette, Options, ScaleMode } from '@hogg/wasm';
 import { useSearchParams } from 'react-router-dom';
 
 const options: Options = {
   autoRotate: true,
-  colorMode: ColorMode.VaporWaveRandom,
+  colorPalette: ColorPalette.VaporWaveRandom,
   scaleMode: ScaleMode.Cover,
   styles: {},
 };