Skip to content

Commit 0a47995

Browse files
author
Oskar Widmark
committed
feat: highlight color based on action type
1 parent a2aa805 commit 0a47995

File tree

6 files changed

+54
-21
lines changed

6 files changed

+54
-21
lines changed

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Features:
55
- [ ] Merge sort
66
- [x] 2D color matrix visualization
77
- [ ] Spiral visualization
8-
- [ ] Highlight color based on action type
8+
- [x] Highlight color based on action type
99
- [ ] Mobile browser support
1010

1111
Fixes:

src/App.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ class App extends React.Component<Props> {
210210
this.setState((prevState: AppState) => ({
211211
nbrOfSwaps: prevState.nbrOfSwaps + 1,
212212
}));
213-
this.canvasController.highlight(arr, [i1, i2], drawIteration);
213+
this.canvasController.highlight(arr, [i1, i2], 'swap', drawIteration);
214214
await sleep(this.state.settings.swapTime, this.swapCounter++);
215215
}
216216
};
@@ -268,7 +268,12 @@ class App extends React.Component<Props> {
268268
nbrOfComparisons: prevState.nbrOfComparisons + 1,
269269
}));
270270
const indexes = 'value' in params ? [i1] : [i1, params.i2];
271-
this.canvasController.highlight(arr, indexes, drawIteration);
271+
this.canvasController.highlight(
272+
arr,
273+
indexes,
274+
'comparison',
275+
drawIteration,
276+
);
272277
await sleep(this.state.settings.compareTime, this.comparisonCounter++);
273278
}
274279

@@ -313,7 +318,7 @@ class App extends React.Component<Props> {
313318
this.setState((prevState: AppState) => ({
314319
nbrOfAuxWrites: prevState.nbrOfAuxWrites + 1,
315320
}));
316-
this.canvasController.highlight(arr, [i], drawIteration);
321+
this.canvasController.highlight(arr, [i], 'auxWrite', drawIteration);
317322
await sleep(this.state.settings.auxWriteTime, this.auxWriteCounter++);
318323
}
319324
};

src/ColorTab.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { TitledSelect } from './components/TitledSelect';
99
import { ColorField } from './components/ColorField';
1010
import { TitledSlider } from './components/TitledSlider';
11+
import { HIGHLIGHT_TYPES } from './constants';
1112

1213
export function ColorTab(props: {
1314
settings: ColorSettings;
@@ -18,7 +19,7 @@ export function ColorTab(props: {
1819
colorPreset,
1920
columnColor1,
2021
columnColor2,
21-
highlightColor,
22+
highlightColors,
2223
backgroundColor,
2324
visualizationType,
2425
displayType,
@@ -64,13 +65,17 @@ export function ColorTab(props: {
6465
setColorSettings({ backgroundColor: value })
6566
}
6667
/>
67-
<ColorField
68-
label="Highlight"
69-
color={highlightColor}
70-
setColor={(value) =>
71-
setColorSettings({ highlightColor: value })
72-
}
73-
/>
68+
{HIGHLIGHT_TYPES.map((type) => (
69+
<ColorField
70+
label={`Highlight (${type[0].toUpperCase()})`}
71+
color={highlightColors[type]}
72+
setColor={(value) =>
73+
setColorSettings({
74+
highlightColors: { ...highlightColors, [type]: value },
75+
})
76+
}
77+
/>
78+
))}
7479
</Grid2>
7580
)}
7681
</Stack>

src/canvas-controller.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
DrawData,
66
SortValue,
77
VisualizationType,
8+
HighlightType,
89
} from './types';
910
import { hsvToRgbHex, rgbHexToHsv } from './utils';
1011

@@ -139,11 +140,11 @@ export class CanvasController {
139140
}
140141
}
141142

142-
getHighlightColor() {
143+
getHighlightColor(type: HighlightType) {
143144
switch (this.context.colorPreset) {
144145
case ColorPreset.Custom:
145146
case ColorPreset.CustomGradient:
146-
return this.context.highlightColor;
147+
return this.context.highlightColors[type];
147148
case ColorPreset.Rainbow:
148149
return '#FFFFFF';
149150
}
@@ -173,7 +174,12 @@ export class CanvasController {
173174
this.drawAll(arr);
174175
};
175176

176-
highlight = (arr: SortValue[], indices: number[], drawIteration?: number) => {
177+
highlight = (
178+
arr: SortValue[],
179+
indices: number[],
180+
type: HighlightType = 'comparison',
181+
drawIteration?: number,
182+
) => {
177183
if (drawIteration == null || drawIteration !== this.currentDrawIteration) {
178184
if (this.highlightIndices.length) {
179185
for (const idx of this.highlightIndices) {
@@ -192,11 +198,11 @@ export class CanvasController {
192198

193199
for (const idx of indices) {
194200
if (this.context.visualizationType === VisualizationType.Matrix) {
195-
this.redrawCellRow(arr, idx, this.getHighlightColor());
196-
this.redrawCellColumn(arr, idx, this.getHighlightColor());
201+
this.redrawCellRow(arr, idx, this.getHighlightColor(type));
202+
this.redrawCellColumn(arr, idx, this.getHighlightColor(type));
197203
continue;
198204
}
199-
this.redrawColumn(arr, idx, this.getHighlightColor());
205+
this.redrawColumn(arr, idx, this.getHighlightColor(type));
200206
}
201207
};
202208

@@ -278,7 +284,7 @@ export class CanvasController {
278284
};
279285

280286
private removeHighlight = (arr: SortValue[]) => {
281-
this.highlight(arr, [], -1);
287+
this.highlight(arr, []);
282288
};
283289

284290
private redrawColumn = (arr: SortValue[], i: number, color?: string) => {

src/constants.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Settings,
77
SortName,
88
VisualizationType,
9+
HighlightType,
910
} from './types';
1011

1112
export const RAINBOW_BACKGROUND_COLOR = '#282c34';
@@ -41,7 +42,11 @@ export const INIT_SETTINGS: Settings = {
4142
columnColor1: '#ffffff',
4243
columnColor2: '#ffffff',
4344
backgroundColor: '#000000',
44-
highlightColor: '#ff0000',
45+
highlightColors: {
46+
comparison: '#ff0000',
47+
swap: '#00ff00',
48+
auxWrite: '#ffff00',
49+
},
4550
visualizationType: VisualizationType.Bars,
4651
displayType: DisplayType.Full,
4752
gapSize: 0,
@@ -52,3 +57,9 @@ export const INIT_SETTINGS: Settings = {
5257
playSoundOnComparison: true,
5358
playSoundOnAuxWrite: false,
5459
};
60+
61+
export const HIGHLIGHT_TYPES: HighlightType[] = [
62+
'swap',
63+
'comparison',
64+
'auxWrite',
65+
];

src/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,19 @@ export type ColorSettings = {
9595
colorPreset: ColorPreset;
9696
columnColor1: string;
9797
columnColor2: string;
98-
highlightColor: string;
98+
highlightColors: HighlightColors;
9999
backgroundColor: string;
100100
visualizationType: VisualizationType;
101101
displayType: DisplayType;
102102
gapSize: number;
103103
};
104104

105+
export type HighlightType = 'comparison' | 'swap' | 'auxWrite';
106+
107+
type HighlightColors = {
108+
[key in HighlightType]: string;
109+
};
110+
105111
export type SoundSettings = {
106112
soundType: NonCustomOscillatorType;
107113
soundVolume: number;

0 commit comments

Comments
 (0)