Skip to content

Commit 211eaa6

Browse files
committed
Optional color coded magnitude scale
2 parents 7ca6559 + e73c90b commit 211eaa6

File tree

5 files changed

+88
-8
lines changed

5 files changed

+88
-8
lines changed

src/Main.svelte

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts">
2-
import { feedData } from "./store"
2+
import { feedData, magnitudeColorScale } from "./store"
33
import { tooltip } from "./tooltip"
4-
import { round1, timestampToLocalString } from "./utils"
4+
import { round1, timestampToLocalString, magBgColor } from "./utils"
55
import Header from "./Header.svelte"
66
</script>
77

@@ -12,13 +12,19 @@
1212
<div class="w-full h-full min-h-screen bg-white dark:bg-gray-800">
1313
{#each $feedData.features as feature (feature.properties.code)}
1414
<div
15-
class="flex items-center justify-between border-b dark:border-gray-600 hover:bg-gray-100 dark:hover:bg-gray-900 py-1"
15+
class={`flex items-center justify-between border-b dark:border-gray-600 hover:bg-gray-100 dark:hover:bg-gray-900 py-1 ${
16+
$magnitudeColorScale ? `${magBgColor(feature.properties.mag)} bg-opacity-20` : ""
17+
}`}
1618
>
1719
<!-- Magnitude -->
18-
<div class="px-4">
19-
<span class:font-bold={feature.properties.mag >= 4.5} class:text-red-600={feature.properties.mag >= 7}>
20-
{round1(feature.properties.mag)}
21-
</span>
20+
<div
21+
class={`mx-2 w-8 h-8 flex items-center justify-center rounded-lg text-sm ${
22+
$magnitudeColorScale ? `${magBgColor(feature.properties.mag)} text-white` : ""
23+
}`}
24+
class:font-bold={feature.properties.mag >= 4.5 && !$magnitudeColorScale}
25+
class:text-red-600={feature.properties.mag >= 7 && !$magnitudeColorScale}
26+
>
27+
{round1(feature.properties.mag)}
2228
</div>
2329

2430
<!-- Location -->

src/Settings.svelte

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,23 @@
99
refreshIntervalTimer,
1010
DEFAULT_MAGNITUDE_NOTIFICATION_THRESHOLD,
1111
magnitudeNotificationThreshold,
12+
magnitudeColorScale,
1213
} from "./store"
1314
import { startFeedRefreshInterval } from "./feed"
1415
import { tooltip } from "./tooltip"
1516
import { setTheme } from "./utils"
17+
import { MagnitudeColor } from "./types"
1618
1719
let selectedTheme = $theme
1820
1921
$: newRefreshInterval = $refreshInterval
2022
$: newMagnitudeNotificationThreshold = $magnitudeNotificationThreshold
23+
$: newMagnitudeColorScale = $magnitudeColorScale
2124
2225
$: refreshIntervalChanged = newRefreshInterval !== $refreshInterval
2326
$: magnitudeNotificationThresholdChanged = newMagnitudeNotificationThreshold !== $magnitudeNotificationThreshold
24-
$: disabled = !refreshIntervalChanged && !magnitudeNotificationThresholdChanged
27+
$: magnitudeColorScaleChanged = newMagnitudeColorScale !== $magnitudeColorScale
28+
$: disabled = !refreshIntervalChanged && !magnitudeNotificationThresholdChanged && !magnitudeColorScaleChanged
2529
2630
const preferences = {
2731
themes: [
@@ -48,6 +52,10 @@
4852
if (magnitudeNotificationThresholdChanged) {
4953
magnitudeNotificationThreshold.set(newMagnitudeNotificationThreshold || DEFAULT_MAGNITUDE_NOTIFICATION_THRESHOLD)
5054
}
55+
56+
if (magnitudeColorScaleChanged) {
57+
magnitudeColorScale.set(newMagnitudeColorScale)
58+
}
5159
}
5260
5361
function closeSettings() {
@@ -125,6 +133,35 @@
125133
</div>
126134
</div>
127135

136+
<!-- Color scale -->
137+
<div class="flex items-center gap-4 p-2 bg-white dark:bg-gray-800">
138+
<div class="w-1/2">
139+
<h2 class="text-right">Magnitude color scale</h2>
140+
</div>
141+
<div class="w-1/2">
142+
<input
143+
type="checkbox"
144+
value="1"
145+
bind:checked={newMagnitudeColorScale}
146+
class="appearance-none w-6 h-6 rounded bg-white dark:bg-gray-800 border dark:border-gray-600 checked:bg-blue-600 dark:checked:bg-blue-600 checked:border-transparent"
147+
/>
148+
</div>
149+
</div>
150+
151+
<div class="flex items-center justify-center pb-4 bg-white dark:bg-gray-800">
152+
{#each Object.keys(MagnitudeColor) as magnitude, i (magnitude)}
153+
{@const totalMagnitudes = Object.keys(MagnitudeColor).length}
154+
<div
155+
class={`flex items-center justify-center w-6 h-6 text-xs text-white ${MagnitudeColor[magnitude]}`}
156+
class:rounded-l={i === 0}
157+
class:rounded-r={i + 1 === totalMagnitudes}
158+
>
159+
{magnitude.replace("m-", "")}
160+
</div>
161+
{/each}
162+
</div>
163+
164+
<!-- Save settings -->
128165
<div class="text-right p-2 bg-white dark:bg-gray-800 border-t dark:border-gray-600 rounded-b-lg">
129166
<button
130167
on:click={saveSettings}

src/store.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ magnitudeNotificationThreshold.subscribe((value: number) => {
3131
localStorage.setItem('magnitudeNotificationThreshold', value.toString())
3232
})
3333

34+
// Magnitude color scale
35+
const storedMagnitudeColorScale = localStorage.getItem('magnitudeColorScale') === '1' ? '1' : '0'
36+
export const magnitudeColorScale = writable<boolean>(storedMagnitudeColorScale === '1' ? true : false)
37+
magnitudeColorScale.subscribe((value: boolean) => {
38+
localStorage.setItem('magnitudeColorScale', value ? '1' : '0')
39+
})
40+
3441
// Refresh interval timer
3542
export const refreshIntervalTimer = writable<NodeJS.Timeout>(undefined)
3643

src/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,14 @@ export interface Geometry {
5353
type: 'Point';
5454
coordinates: number[]; // [36.4567, 38.0154, 10]
5555
}
56+
57+
export enum MagnitudeColor {
58+
'm-2.5' = 'bg-teal-600',
59+
'm-3' = 'bg-green-600',
60+
'm-4' = 'bg-yellow-500',
61+
'm-5' = 'bg-orange-400',
62+
'm-6' = 'bg-orange-600',
63+
'm-7' = 'bg-red-600',
64+
'm-8' = 'bg-red-700',
65+
'm-9' = 'bg-red-800',
66+
}

src/utils.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { MagnitudeColor } from "./types"
12

23
// Round a number to 1 decimal
34
export const round1 = function (num: number): string {
@@ -42,6 +43,24 @@ export const timestampToLocalString = function (timestamp: number): string {
4243
}
4344

4445

46+
// Color-coded background based on magnitude
47+
export const magBgColor = function (magnitude: number) {
48+
let mag: number
49+
50+
if (magnitude >= 9) mag = 9
51+
else if (magnitude >= 8) mag = 8
52+
else if (magnitude >= 7) mag = 7
53+
else if (magnitude >= 6) mag = 6
54+
else if (magnitude >= 5) mag = 5
55+
else if (magnitude >= 4) mag = 4
56+
else if (magnitude >= 3) mag = 3
57+
else mag = 2.5
58+
59+
return MagnitudeColor[`m-${mag}`]
60+
}
61+
62+
63+
4564
// Theming functions
4665
export function setIsDark(dark: boolean) {
4766
const htmlNode = document.querySelector("html")

0 commit comments

Comments
 (0)