Skip to content

Commit

Permalink
Renamed ColorMode to ColorPalette
Browse files Browse the repository at this point in the history
  • Loading branch information
HHogg committed Dec 15, 2024
1 parent e613a58 commit 78a6646
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 84 deletions.
2 changes: 1 addition & 1 deletion workspaces/gap-validation/src/Article/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
59 changes: 10 additions & 49 deletions workspaces/tilings/src-rust/renderer/src/draw/layers/draw_shapes.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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());
Expand All @@ -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)?)
}
}
}
};

Expand Down
7 changes: 3 additions & 4 deletions workspaces/tilings/src-rust/renderer/src/draw/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand All @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions workspaces/tilings/src-rust/tiling/src/build/plane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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> = {
Expand All @@ -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,
Expand Down
18 changes: 9 additions & 9 deletions workspaces/tilings/src/Presentation/Settings/Settings.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ColorMode, Layer, ScaleMode } from '@hogg/wasm';
import { ColorPalette, Layer, ScaleMode } from '@hogg/wasm';
import {
GaugeIcon,
LayersIcon,
Expand All @@ -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,
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -120,7 +120,7 @@ export default function Settings() {
<ConfigMenu
config={[
autoRotateConfig,
colorModeConfig,
colorPaletteConfig,
expansionPhasesConfig,
showLayersConfig,
scaleModeConfig,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ColorMode, ScaleMode } from '@hogg/wasm';
import { ColorPalette, ScaleMode } from '@hogg/wasm';
import { PropsWithChildren, useState } from 'react';
import {
Settings,
Expand All @@ -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
);
Expand All @@ -32,12 +34,12 @@ export default function SettingsProvider({

const value = {
autoRotate,
colorMode,
colorPalette,
expansionPhases,
scaleMode,
showLayers,
setAutoRotate,
setColorMode,
setColorPalette,
setExpansionPhases,
setScaleMode,
setShowLayers,
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -7,15 +7,15 @@ import {

export type Settings = {
autoRotate: boolean;
colorMode: ColorMode;
colorPalette: ColorPalette;
expansionPhases: number;
scaleMode: ScaleMode;
showLayers: Record<Layer, boolean>;
};

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;
Expand All @@ -38,7 +38,7 @@ export const SettingsContext = createContext<SettingsContextValue>({
...defaultSettings,
showSettings: false,
setAutoRotate: noop,
setColorMode: noop,
setColorPalette: noop,
setExpansionPhases: noop,
setScaleMode: noop,
setShowLayers: noop,
Expand Down
6 changes: 3 additions & 3 deletions workspaces/tilings/src/Presentation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function PresentationInner(props: RendererProps) {
setShowSettings,
autoRotate,
expansionPhases,
colorMode,
colorPalette,
scaleMode,
showLayers,
} = useSettingsContext();
Expand All @@ -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 (
Expand Down
5 changes: 2 additions & 3 deletions workspaces/wasm/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand Down Expand Up @@ -221,7 +220,7 @@ export interface Styles {

export interface Options {
autoRotate?: boolean;
colorMode?: ColorMode;
colorPalette?: ColorPalette;
isValid?: boolean;
maxStage?: number;
padding?: number;
Expand Down
4 changes: 2 additions & 2 deletions workspaces/website/src/pages/TilingGenerationPage.tsx
Original file line number Diff line number Diff line change
@@ -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: {},
};
Expand Down

0 comments on commit 78a6646

Please sign in to comment.