Skip to content

Commit 19497e8

Browse files
committed
Change hardcoded colors usage to use Geist UI's palette
1 parent 6655b0c commit 19497e8

File tree

7 files changed

+61
-51
lines changed

7 files changed

+61
-51
lines changed

algorithms-helper/bubble-sort.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ACTIVE_BAR_COLOR, INACTIVE_BAR_COLOR } from "../constants"
1+
import { AnimateFunctionParams } from "../types"
22
import {
33
changeBarsColor,
44
getAllBars,
@@ -42,11 +42,8 @@ const bubbleSort = (array: number[]) => {
4242
return animaSeq
4343
}
4444

45-
export const animateBubbleSort = (
46-
barHeights: number[],
47-
sortingSpeed: number,
48-
callback?: () => void
49-
) => {
45+
export const animateBubbleSort = (params: AnimateFunctionParams) => {
46+
const { barHeights, palette, sortingSpeed, callback } = params
5047
const animationSequence = bubbleSort(barHeights)
5148
const bars = getAllBars()
5249

@@ -57,25 +54,25 @@ export const animateBubbleSort = (
5754
if (!isFirstIteration) {
5855
const [prevFirstBarIdx, prevSecondBarIdx] = animationSequence[iteration - 1]
5956

60-
changeBarsColor(
61-
[bars[prevFirstBarIdx], bars[prevSecondBarIdx]],
62-
INACTIVE_BAR_COLOR
63-
)
57+
changeBarsColor([bars[prevFirstBarIdx], bars[prevSecondBarIdx]], palette.inactive)
6458
}
6559

6660
const activeBarHeights = [
6761
getNumberFromHeightString(bars[firstBarIdx].style.height),
6862
getNumberFromHeightString(bars[secondBarIdx].style.height),
6963
]
7064

71-
makeBarsActive([
72-
{ element: bars[firstBarIdx], height: activeBarHeights[1] },
73-
{ element: bars[secondBarIdx], height: activeBarHeights[0] },
74-
])
65+
makeBarsActive(
66+
[
67+
{ element: bars[firstBarIdx], height: activeBarHeights[1] },
68+
{ element: bars[secondBarIdx], height: activeBarHeights[0] },
69+
],
70+
palette.active
71+
)
7572

7673
const isLastIteration = iteration === animationSequence.length - 1
7774
if (isLastIteration && callback) {
78-
postSortAnimation(bars, ACTIVE_BAR_COLOR)
75+
postSortAnimation(bars, palette.active)
7976
callback()
8077
}
8178
}, iteration * sortingSpeed)

algorithms-helper/insertion-sort.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { INACTIVE_BAR_COLOR, ACTIVE_BAR_COLOR } from "../constants"
1+
import { AnimateFunctionParams } from "../types"
22
import { getAllBars, changeBarsColor, makeBarsActive, postSortAnimation } from "../utils"
33

44
interface InsertionAnimationSequence {
@@ -52,11 +52,8 @@ const insertionSort = (arrayToSort: number[]): InsertionAnimationSequence[] => {
5252
return animaSeq
5353
}
5454

55-
export const animateInsertionSort = (
56-
barHeights: number[],
57-
sortingSpeed: number,
58-
callback?: () => void
59-
) => {
55+
export const animateInsertionSort = (params: AnimateFunctionParams) => {
56+
const { barHeights, palette, sortingSpeed, callback } = params
6057
const animationSequence = insertionSort(barHeights)
6158
const bars = getAllBars()
6259

@@ -73,11 +70,14 @@ export const animateInsertionSort = (
7370

7471
setTimeout(() => {
7572
if (!isFirstIteration) {
76-
changeBarsColor(bars[animationSequence[i - 1].idx], INACTIVE_BAR_COLOR)
73+
changeBarsColor(bars[animationSequence[i - 1].idx], palette.inactive)
7774
}
7875

7976
prevIdxBarHeights.push(bars[idxToInsertTo].style.height)
80-
makeBarsActive([{ element: bars[idxToInsertTo], height: barHeights[barToMove] }])
77+
makeBarsActive(
78+
[{ element: bars[idxToInsertTo], height: barHeights[barToMove] }],
79+
palette.active
80+
)
8181
}, i * sortingSpeed)
8282

8383
for (let x = 1; x <= rightShift; x++) {
@@ -87,8 +87,8 @@ export const animateInsertionSort = (
8787

8888
const isLastInnerIteration = x === rightShift
8989
if (isLastInnerIteration && isLastIteration) {
90-
changeBarsColor(bars[idxToInsertTo], INACTIVE_BAR_COLOR)
91-
postSortAnimation(bars, ACTIVE_BAR_COLOR)
90+
changeBarsColor(bars[idxToInsertTo], palette.inactive)
91+
postSortAnimation(bars, palette.active)
9292
if (callback) callback()
9393
}
9494
}, i * sortingSpeed)

algorithms-helper/selection-sort.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { INACTIVE_BAR_COLOR, ACTIVE_BAR_COLOR } from "../constants"
2-
import { ActiveBar } from "../types"
1+
import { ActiveBar, AnimateFunctionParams } from "../types"
32
import { changeBarsColor, getAllBars, makeBarsActive, postSortAnimation } from "../utils"
43

54
const selectionSort = (inputArray: number[]) => {
@@ -44,11 +43,8 @@ const selectionSort = (inputArray: number[]) => {
4443
return [arrayState, animationSequence]
4544
}
4645

47-
export const animateSelectionSort = (
48-
barHeights: number[],
49-
sortingSpeed: number,
50-
callback?: () => void
51-
) => {
46+
export const animateSelectionSort = (params: AnimateFunctionParams) => {
47+
const { barHeights, palette, sortingSpeed, callback } = params
5248
const [arrayStates, animationSequence] = selectionSort(barHeights)
5349
const bars = getAllBars()
5450

@@ -81,23 +77,23 @@ export const animateSelectionSort = (
8177
let prevAnimationSequence = animationSequence[i - 1]
8278
let [prevBar1Idx, prevBar2Idx] = prevAnimationSequence
8379
let previousActiveBars = [bars[prevBar1Idx], bars[prevBar2Idx]]
84-
changeBarsColor(previousActiveBars, INACTIVE_BAR_COLOR)
80+
changeBarsColor(previousActiveBars, palette.inactive)
8581
}
8682

87-
makeBarsActive(barsToActiveBars)
83+
makeBarsActive(barsToActiveBars, palette.active)
8884

8985
if (lastIteration) {
9086
// Revert the last bar to an inactive bar
9187
setTimeout(() => {
9288
let lastBar = activeBar1
93-
changeBarsColor(lastBar, INACTIVE_BAR_COLOR)
89+
changeBarsColor(lastBar, palette.inactive)
9490

9591
// Because this animate function executes asynchronous function (setTimeout)
9692
// if we wanted to do something right after this function is done running,
9793
// we have to put the code inside the last setTimeout.
9894
// Otherwise, the code will get executed without waiting for the setTimeouts
9995
// to get executed.
100-
postSortAnimation(bars, ACTIVE_BAR_COLOR)
96+
postSortAnimation(bars, palette.active)
10197
if (callback) callback()
10298
}, sortingSpeed)
10399
}

constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { SortingAlgorithms, SortingSpeeds } from "./types"
22

3-
export const ACTIVE_BAR_COLOR = "#111"
4-
export const INACTIVE_BAR_COLOR = "#ff1a1a"
53
export const ALGORITHMS_LIST: SortingAlgorithms[] = ["Selection", "Insertion", "Bubble"]
64
export const sortingSpeedTable: SortingSpeeds = {
75
slow: 160,

pages/index.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import {
1414
SortButton,
1515
SpeedControl,
1616
} from "../components"
17-
import { Spacer } from "@geist-ui/react"
18-
import { INACTIVE_BAR_COLOR, sortingSpeedTable } from "../constants"
17+
import { Spacer, useTheme } from "@geist-ui/react"
18+
import { sortingSpeedTable } from "../constants"
1919
import { SortingAlgorithms, SortingSpeeds, SortingState } from "../types"
2020
import { default as Head } from "next/head"
2121
import { AppStateContext } from "../contexts/app-state"
@@ -33,6 +33,12 @@ const getBarElementsFromBarHeights = (barHeights: number[]) => {
3333
}
3434

3535
const Home: React.FC = () => {
36+
const { palette: geistUIPalette } = useTheme()
37+
const palette = {
38+
active: geistUIPalette.foreground,
39+
inactive: geistUIPalette.errorLight,
40+
}
41+
3642
const [barHeights, setBarHeights] = useState<number[]>([])
3743
const [sortingState, setSortingState] = useState<SortingState>("Sort")
3844
const [arrayLength, setArrayLength] = useState(70)
@@ -50,17 +56,18 @@ const Home: React.FC = () => {
5056
setSortingState("Sort")
5157
requestAnimationFrame(() => {
5258
const barsDomEl = getAllBars()
53-
if (barsDomEl[5].style.backgroundColor === "rgb(17, 17, 17)") {
54-
changeBarsColor(barsDomEl, INACTIVE_BAR_COLOR)
59+
if (barsDomEl[5].style.backgroundColor === "rgb(0, 0, 0)") {
60+
changeBarsColor(barsDomEl, palette.inactive)
5561
}
5662
})
5763
}
5864

5965
function triggerAnimation() {
6066
setSortingState("Sorting")
6167
startAnimation({
62-
sortingAlgorithm: selectedAlgorithm,
6368
barHeights,
69+
palette,
70+
sortingAlgorithm: selectedAlgorithm,
6471
sortingSpeed: sortingSpeedTable[sortingSpeed],
6572
callback: () => setSortingState("Sorted"),
6673
})

types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ export interface SortingSpeeds {
66
normal: number
77
fast: number
88
}
9+
export interface AnimateFunctionParams {
10+
barHeights: number[]
11+
palette: { active: string; inactive: string }
12+
sortingSpeed: number
13+
callback?: () => void
14+
}

utils/index.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { animateBubbleSort } from "../algorithms-helper/bubble-sort"
22
import { animateInsertionSort } from "../algorithms-helper/insertion-sort"
33
import { animateSelectionSort } from "../algorithms-helper/selection-sort"
4-
import { ACTIVE_BAR_COLOR } from "../constants"
5-
import { ActiveBar, SortingAlgorithms, SortingSpeeds } from "../types"
4+
import { ActiveBar, SortingAlgorithms } from "../types"
65

76
export const generateARandomNumber = (min: number, max: number) => {
87
min = Math.ceil(min)
@@ -47,9 +46,9 @@ export const changeBarsColor = (
4746
}
4847
}
4948

50-
export const makeBarsActive = (bars: ActiveBar[]) => {
49+
export const makeBarsActive = (bars: ActiveBar[], activeBarColor: string) => {
5150
for (const bar of bars) {
52-
changeBarsColor(bar.element, ACTIVE_BAR_COLOR)
51+
changeBarsColor(bar.element, activeBarColor)
5352
bar.element.style.height = `${bar.height}%`
5453
}
5554
}
@@ -82,23 +81,30 @@ export const getNumberFromHeightString = (height: string) => {
8281

8382
interface StartAnimationParams {
8483
barHeights: number[]
84+
palette: { active: string; inactive: string }
8585
sortingAlgorithm: SortingAlgorithms
8686
sortingSpeed: number
8787
callback?: () => void
8888
}
8989

9090
export const startAnimation = (params: StartAnimationParams) => {
91-
const { barHeights, sortingAlgorithm, sortingSpeed, callback } = params
91+
const { barHeights, palette, sortingAlgorithm, sortingSpeed, callback } = params
92+
const animationParams = {
93+
barHeights,
94+
palette,
95+
sortingSpeed,
96+
callback,
97+
}
9298

9399
switch (sortingAlgorithm) {
94100
case "Selection":
95-
animateSelectionSort(barHeights, sortingSpeed, callback)
101+
animateSelectionSort(animationParams)
96102
break
97103
case "Insertion":
98-
animateInsertionSort(barHeights, sortingSpeed, callback)
104+
animateInsertionSort(animationParams)
99105
break
100106
case "Bubble":
101-
animateBubbleSort(barHeights, sortingSpeed, callback)
107+
animateBubbleSort(animationParams)
102108
break
103109
}
104110
}

0 commit comments

Comments
 (0)