-
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.
- Loading branch information
Showing
5 changed files
with
28 additions
and
25 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,18 @@ | ||
package color | ||
|
||
import kotlinx.serialization.decodeFromString | ||
import kotlinx.serialization.json.Json | ||
import utils.ResourcePaths.CONSTANT_COLORS | ||
import java.io.File | ||
import java.util.regex.Pattern | ||
import java.util.regex.Pattern.CASE_INSENSITIVE | ||
|
||
|
||
object ConstantColorsService { | ||
private val colors: Map<String, CssColor> = | ||
Json.decodeFromString<Map<String, String>>(CONSTANT_COLORS.getResource()) | ||
.mapValues { CssColor.fromHEX(it.value) } | ||
|
||
fun getNamesRegex() = Pattern.compile(colors.keys.joinToString(separator = "|"), CASE_INSENSITIVE)!!.toRegex() | ||
operator fun get(name: String): CssColor = colors[name]!! | ||
} |
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 |
---|---|---|
@@ -1,9 +1,15 @@ | ||
package utils | ||
|
||
import java.io.InputStream | ||
|
||
|
||
class ResourceException(path: String) : RuntimeException("Cannot find the resource: $path") | ||
|
||
private fun InputStream.getStringContent() = this.bufferedReader().use { it.readText() } | ||
|
||
enum class ResourcePaths(private val path: String) { | ||
CONSTANT_COLORS("/colors/constant_color_names.json"); | ||
|
||
fun getResourceUri() = this::class.java.getResource(path)?.toURI() ?: throw ResourceException(path) | ||
fun getResource() = | ||
object {}.javaClass.getResourceAsStream(path)?.getStringContent() ?: throw ResourceException(path) | ||
} |