Skip to content

Commit 67a2d3f

Browse files
author
Oskar Widmark
committed
feat: add color presets
1 parent 67086f2 commit 67a2d3f

File tree

6 files changed

+176
-10
lines changed

6 files changed

+176
-10
lines changed

src/App.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
}
99

1010
.App-header {
11-
background-color: #282c34;
1211
height: 100vh;
1312
color: white;
1413
display: flex;

src/App.tsx

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Operator,
1010
SortType,
1111
AlgorithmOptions,
12+
ColorPreset,
1213
} from './types';
1314
import { SortingAlgorithms } from './sorting-algorithms';
1415
import {
@@ -24,6 +25,10 @@ import {
2425
INIT_SWAP_TIME,
2526
INIT_COMPARE_TIME,
2627
DEFAULT_ALGORITHM_OPTIONS,
28+
DEFAULT_COLUMN_COLOR,
29+
DEFAULT_BACKGROUND_COLOR,
30+
DEFAULT_HIGHLIGHT_COLOR,
31+
RAINBOW_BACKGROUND_COLOR,
2732
} from './constants';
2833
import { SortAppBar } from './AppBar';
2934
import { CanvasController } from './canvas-controller';
@@ -55,6 +60,10 @@ class App extends React.Component<Props> {
5560
shouldHighlightComparisons: boolean;
5661
shouldPlaySound: boolean;
5762
algorithmOptions: AlgorithmOptions;
63+
colorPreset: ColorPreset;
64+
columnColor: string;
65+
backgroundColor: string;
66+
highlightColor: string;
5867
};
5968
canvasController: CanvasController;
6069

@@ -84,6 +93,10 @@ class App extends React.Component<Props> {
8493
shouldHighlightComparisons: false,
8594
shouldPlaySound: false,
8695
algorithmOptions: DEFAULT_ALGORITHM_OPTIONS,
96+
colorPreset: ColorPreset.Rainbow,
97+
columnColor: DEFAULT_COLUMN_COLOR,
98+
backgroundColor: DEFAULT_BACKGROUND_COLOR,
99+
highlightColor: DEFAULT_HIGHLIGHT_COLOR,
87100
};
88101

89102
this.resetPresets = {
@@ -97,6 +110,9 @@ class App extends React.Component<Props> {
97110
this.canvasController = new CanvasController(
98111
ref as React.RefObject<HTMLCanvasElement>,
99112
INIT_COLUMN_NUMBER,
113+
this.state.colorPreset,
114+
this.state.columnColor,
115+
this.state.highlightColor,
100116
);
101117
}
102118

@@ -326,9 +342,46 @@ class App extends React.Component<Props> {
326342
}));
327343
};
328344

345+
setColorPreset = (colorPreset: ColorPreset) => {
346+
this.setState({ colorPreset });
347+
this.canvasController.colorPreset = colorPreset;
348+
this.stopSorting();
349+
this.canvasController.redraw(this.arr);
350+
};
351+
352+
setColumnColor = (columnColor: string) => {
353+
this.setState({ columnColor });
354+
this.canvasController.columnColor = columnColor;
355+
this.stopSorting();
356+
this.canvasController.redraw(this.arr);
357+
};
358+
359+
setBackgroundColor = (backgroundColor: string) => {
360+
this.setState({ backgroundColor });
361+
};
362+
363+
setHighlightColor = (highlightColor: string) => {
364+
this.setState({ highlightColor });
365+
this.canvasController.highlightColor = highlightColor;
366+
this.stopSorting();
367+
this.canvasController.redraw(this.arr);
368+
};
369+
370+
getBackgroundColor = () => {
371+
switch (this.state.colorPreset) {
372+
case ColorPreset.Rainbow:
373+
return RAINBOW_BACKGROUND_COLOR;
374+
case ColorPreset.Custom:
375+
return this.state.backgroundColor;
376+
}
377+
};
378+
329379
render() {
330380
return (
331-
<div className="App">
381+
<div
382+
className="App"
383+
style={{ backgroundColor: this.getBackgroundColor() }}
384+
>
332385
<div className="App-header">
333386
<SortAppBar
334387
arr={this.arr}
@@ -373,6 +426,14 @@ class App extends React.Component<Props> {
373426
columnNbr={this.state.columnNbr}
374427
algorithmOptions={this.state.algorithmOptions}
375428
setAlgorithmOption={this.setAlgorithmOption}
429+
colorPreset={this.state.colorPreset}
430+
columnColor={this.state.columnColor}
431+
backgroundColor={this.state.backgroundColor}
432+
highlightColor={this.state.highlightColor}
433+
setColorPreset={this.setColorPreset}
434+
setColumnColor={this.setColumnColor}
435+
setBackgroundColor={this.setBackgroundColor}
436+
setHighlightColor={this.setHighlightColor}
376437
/>
377438
</div>
378439
</div>

src/SideDrawer.tsx

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ import {
77
MenuItem,
88
Slider,
99
SelectChangeEvent,
10+
TextField,
11+
Grid2,
12+
Stack,
1013
} from '@mui/material';
11-
import { SortName, ResetPreset, AlgorithmOptions } from './types';
14+
import { SortName, ResetPreset, AlgorithmOptions, ColorPreset } from './types';
1215
import { timeScale } from './utils';
1316
import ChevronRightIcon from '@mui/icons-material/ChevronRight';
1417
import {
@@ -36,6 +39,14 @@ interface SideDrawerProps {
3639
key: keyof AlgorithmOptions,
3740
value: AlgorithmOptions[typeof key],
3841
) => void;
42+
colorPreset: ColorPreset;
43+
columnColor: string;
44+
backgroundColor: string;
45+
highlightColor: string;
46+
setColorPreset: (string: ColorPreset) => void;
47+
setColumnColor: (string: string) => void;
48+
setBackgroundColor: (string: string) => void;
49+
setHighlightColor: (string: string) => void;
3950
}
4051

4152
export function SideDrawer({
@@ -51,6 +62,14 @@ export function SideDrawer({
5162
areSettingsOpen,
5263
algorithmOptions,
5364
setAlgorithmOption,
65+
colorPreset,
66+
columnColor,
67+
backgroundColor,
68+
highlightColor,
69+
setColorPreset,
70+
setColumnColor,
71+
setBackgroundColor,
72+
setHighlightColor,
5473
}: SideDrawerProps) {
5574
const isBitonicSort = chosenSortAlg === SortName.BitonicSort;
5675

@@ -195,6 +214,67 @@ export function SideDrawer({
195214
</Select>
196215
</FormControl>
197216
</div>
217+
<div className="select-wrapper">
218+
<FormControl component="fieldset">
219+
<Typography
220+
align="left"
221+
variant="h6"
222+
color="textSecondary"
223+
gutterBottom
224+
>
225+
Colors
226+
</Typography>
227+
<Stack direction="column" spacing={2} alignItems="left">
228+
<TextField
229+
value={colorPreset}
230+
label="Preset"
231+
size="small"
232+
select
233+
onChange={(e) => setColorPreset(e.target.value as ColorPreset)}
234+
>
235+
{Object.values(ColorPreset).map((v) => (
236+
<MenuItem value={v} key={v}>
237+
<Typography
238+
align="left"
239+
variant="body1"
240+
color="textSecondary"
241+
>
242+
{v}
243+
</Typography>
244+
</MenuItem>
245+
))}
246+
</TextField>
247+
{colorPreset === ColorPreset.Custom && (
248+
<Grid2 container spacing={2}>
249+
<TextField
250+
label="Columns"
251+
value={columnColor}
252+
type="color"
253+
size="small"
254+
sx={{ width: 100 }}
255+
onChange={(e) => setColumnColor(e.target.value)}
256+
/>
257+
<TextField
258+
label="Background"
259+
value={backgroundColor}
260+
type="color"
261+
size="small"
262+
sx={{ width: 100 }}
263+
onChange={(e) => setBackgroundColor(e.target.value)}
264+
/>
265+
<TextField
266+
label="Highlight"
267+
value={highlightColor}
268+
type="color"
269+
size="small"
270+
sx={{ width: 100 }}
271+
onChange={(e) => setHighlightColor(e.target.value)}
272+
/>
273+
</Grid2>
274+
)}
275+
</Stack>
276+
</FormControl>
277+
</div>
198278
</Drawer>
199279
);
200280
}

src/canvas-controller.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { MouseEvent } from 'react';
2-
import { HIGHLIGHT_COLOR } from './constants';
3-
import { DrawData, SortValue } from './types';
2+
import { ColorPreset, DrawData, SortValue } from './types';
43
import { hsvToRgbHex } from './utils';
54

65
export class CanvasController {
@@ -14,6 +13,9 @@ export class CanvasController {
1413
constructor(
1514
public canvasRef: React.RefObject<HTMLCanvasElement>,
1615
public columnNbr: number,
16+
public colorPreset: ColorPreset,
17+
public columnColor: string,
18+
public highlightColor: string,
1719
) {}
1820

1921
get refCurrent() {
@@ -41,6 +43,24 @@ export class CanvasController {
4143
return context;
4244
}
4345

46+
getColumnColor(value: number) {
47+
switch (this.colorPreset) {
48+
case ColorPreset.Custom:
49+
return this.columnColor;
50+
case ColorPreset.Rainbow:
51+
return hsvToRgbHex((360 * value) / this.columnNbr, 1, 1);
52+
}
53+
}
54+
55+
getHighlightColor() {
56+
switch (this.colorPreset) {
57+
case ColorPreset.Custom:
58+
return this.highlightColor;
59+
case ColorPreset.Rainbow:
60+
return '#FFFFFF';
61+
}
62+
}
63+
4464
resizeCanvas = (arr: SortValue[]) => {
4565
const parent = document.getElementById('canvas-wrapper');
4666
if (parent === null) {
@@ -66,7 +86,7 @@ export class CanvasController {
6686
this.prevHighlightIndices = indices;
6787

6888
for (const idx of indices) {
69-
this.redrawColumn(arr, idx, HIGHLIGHT_COLOR);
89+
this.redrawColumn(arr, idx, this.getHighlightColor());
7090
}
7191
};
7292

@@ -167,14 +187,12 @@ export class CanvasController {
167187
};
168188

169189
private drawColumn = (arr: SortValue[], i: number, color?: string) => {
170-
const arrLength = arr.length;
171190
const width = this.canvas2dCtx.canvas.width / this.columnNbr;
172191
const height =
173192
(this.canvas2dCtx.canvas.height / this.columnNbr) * (arr[i].value + 1);
174193
const startX = width * i;
175194

176-
this.canvas2dCtx.fillStyle =
177-
color || hsvToRgbHex((360 * arr[i].value) / arrLength, 1, 1);
195+
this.canvas2dCtx.fillStyle = color || this.getColumnColor(arr[i].value);
178196
this.fillRect(startX, 0, width, height);
179197
};
180198

src/constants.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import { AlgorithmOptions } from './types';
33
export const INIT_SWAP_TIME = 1;
44
export const INIT_COMPARE_TIME = 1;
55
export const INIT_COLUMN_NUMBER = 100;
6-
export const HIGHLIGHT_COLOR = '#FFFFFF';
76
export const POWERS_OF_TWO = [8, 16, 32, 64, 128, 256, 512, 1024];
7+
export const DEFAULT_COLUMN_COLOR = '#ffffff';
8+
export const DEFAULT_BACKGROUND_COLOR = '#000000';
9+
export const DEFAULT_HIGHLIGHT_COLOR = '#ff0000';
10+
export const RAINBOW_BACKGROUND_COLOR = '#282c34';
811
export const DEFAULT_ALGORITHM_OPTIONS: AlgorithmOptions = {
912
type: 'iterative',
1013
base: 4,

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,8 @@ export enum SortType {
4646
Comparison = 'Comparison',
4747
Distribution = 'Distribution',
4848
}
49+
50+
export enum ColorPreset {
51+
Rainbow = 'Rainbow',
52+
Custom = 'Custom',
53+
}

0 commit comments

Comments
 (0)