-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45dfd0f
commit c01c0d9
Showing
20 changed files
with
463 additions
and
785 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,18 @@ | ||
import { isDark } from "../lib/darkThemeHelper"; | ||
import type { OsmiContextInstance } from "../types/osmi.types"; | ||
|
||
const getColor = (themeContext: OsmiContextInstance | null, colorKey: string, opacity: string) => { | ||
const predefinedColor = themeContext?.theme[colorKey]; | ||
|
||
if (typeof predefinedColor === "string") { | ||
return predefinedColor.replace( | ||
"--osmi-opacity", | ||
opacity !== undefined ? (Number(opacity) / 100).toFixed(2) : "1" | ||
); | ||
} else { | ||
const getColor = (themeContext: OsmiContextInstance, colorKey: string, opacity?: string) => { | ||
const color = themeContext.theme[colorKey]; | ||
if (typeof color !== "string") { | ||
throw Error("Invalid color syntax"); | ||
} | ||
const alpha = opacity ? (Number(opacity) / 100).toFixed(2) : "1"; | ||
return color.replace("--osmi-opacity", alpha); | ||
}; | ||
|
||
export const colorHelper = | ||
(syntax: string | boolean | undefined) => (themeContext: OsmiContextInstance | null) => { | ||
const splitSyntax = (syntax as string).split(" "); | ||
|
||
if (splitSyntax.length === 1) { | ||
const [colorKey, opacity] = splitSyntax[0].split("/"); | ||
return getColor(themeContext, colorKey, opacity); | ||
} | ||
|
||
if (splitSyntax.length === 2 && splitSyntax.some((s) => s.includes("dark:"))) { | ||
const defaultSyntax = splitSyntax.find((s) => !s.includes("dark:")) ?? ""; | ||
const darkSyntax = splitSyntax.find((s) => s.includes("dark:"))?.replace("dark:", "") ?? ""; | ||
|
||
const [defaultColor, defaultOpacity] = defaultSyntax.split("/"); | ||
const [darkColor, darkOpacity] = darkSyntax.split("/"); | ||
export const colorHelper = (syntax: string) => (themeContext: OsmiContextInstance) => { | ||
const [baseSyntax, darkSyntax] = syntax.split(" ").map(s => s.replace("dark:", "")); | ||
const [colorKey, opacity] = (isDark(darkSyntax, themeContext.mode) ? darkSyntax : baseSyntax).split("/"); | ||
|
||
if (isDark(darkSyntax, themeContext?.mode ?? "")) { | ||
return getColor(themeContext, darkColor, darkOpacity); | ||
} else { | ||
return getColor(themeContext, defaultColor, defaultOpacity); | ||
} | ||
} | ||
|
||
throw Error("Invalid color syntax"); | ||
}; | ||
return getColor(themeContext, colorKey, opacity); | ||
}; |
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
Oops, something went wrong.