-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(color): improve contrast calculation
- Loading branch information
Showing
18 changed files
with
276 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export interface Scores { | ||
best: number; | ||
last: number; | ||
} | ||
|
||
/** | ||
* Get the last score and best score from previous games. If no scores were | ||
* found, set their values to 0 | ||
* | ||
* @returns the scores | ||
*/ | ||
export function getScores(): Scores { | ||
const last = parseInt(localStorage.getItem('last')) || 0; | ||
const best = parseInt(localStorage.getItem('best')) || last; | ||
|
||
return { best, last }; | ||
} | ||
|
||
/** | ||
* Save the score into the local storage | ||
* | ||
* @param score the score | ||
*/ | ||
export function saveScore(score: number): void { | ||
const localBest = parseInt(localStorage.getItem('best')) || 0; | ||
const best = Math.max(score, localBest); | ||
|
||
localStorage.setItem('best', best.toString()); | ||
localStorage.setItem('last', score.toString()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import Color from 'color'; | ||
import { getLocaleFromNavigator, locale } from 'svelte-i18n'; | ||
import settings, { Settings } from '../store/settings'; | ||
import { defaultColors, Lang, LANGS } from '../ts/constants'; | ||
|
||
/** | ||
* Get the correct language | ||
* @param lang the initial language, that may be valid or not | ||
* @returns the correct language | ||
*/ | ||
const getLanguage = (lang?: string): Lang => { | ||
if (!lang) { | ||
lang = getLocaleFromNavigator(); | ||
} | ||
|
||
if (lang in LANGS) { | ||
return lang as Lang; | ||
} | ||
|
||
return 'en'; | ||
}; | ||
|
||
/** | ||
* Load settings from local storage | ||
* @returns the settings | ||
*/ | ||
export const loadSettings = (): Settings => { | ||
const settingsRaw = localStorage.getItem('settings'); | ||
|
||
let settings: any; | ||
|
||
try { | ||
settings = JSON.parse(settingsRaw); | ||
} catch { | ||
settings = {}; | ||
} | ||
|
||
let colorTheme: Color; | ||
|
||
if (settings?.colorTheme) { | ||
try { | ||
colorTheme = Color(settings?.colorTheme); | ||
} catch { | ||
colorTheme = Color(defaultColors[0]); | ||
} | ||
} else { | ||
colorTheme = defaultColors[0]; | ||
} | ||
|
||
return { | ||
colorTheme, | ||
showRules: Boolean(settings?.showRules ?? true), | ||
language: getLanguage(settings?.language), | ||
soundsEnabled: Boolean(settings?.soundsEnabled ?? true), | ||
}; | ||
}; | ||
|
||
/** | ||
* Save settings into local storage | ||
* @param settings the settings to save | ||
*/ | ||
export const saveSettings = (settings: Settings): void => { | ||
const jsonSettings = { ...settings, colorTheme: settings.colorTheme.hex() }; | ||
localStorage.setItem('settings', JSON.stringify(jsonSettings)); | ||
}; | ||
|
||
// Setters | ||
|
||
export const setLanguage = (language: Lang) => { | ||
settings.update(s => ({ ...s, language })); | ||
locale.set(language); | ||
}; | ||
|
||
export const setColorTheme = (colorTheme: Color) => { | ||
settings.update(s => ({ ...s, colorTheme })); | ||
}; | ||
|
||
export const setSoundsEnabled = (soundsEnabled: boolean) => { | ||
settings.update(s => ({ ...s, soundsEnabled })); | ||
}; |
Oops, something went wrong.