-
Notifications
You must be signed in to change notification settings - Fork 7
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
7 changed files
with
81 additions
and
17 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
50 changes: 50 additions & 0 deletions
50
core/src/main/kotlin/eu/iamgio/quarkdown/misc/color/decoder/RgbaColorDecoder.kt
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,50 @@ | ||
package eu.iamgio.quarkdown.misc.color.decoder | ||
|
||
import eu.iamgio.quarkdown.misc.color.Color | ||
|
||
/** | ||
* Given a regex match result, extracts RGB components from it. | ||
* @return a list of red, green and blue components. Any can be `null` if the component is invalid. | ||
*/ | ||
private fun extractRGBComponents(match: MatchResult): List<Int?> = | ||
match.destructured.toList().map { component -> | ||
component.toIntOrNull()?.takeIf { it <= Color.MAX_RGB } | ||
} | ||
|
||
/** | ||
* Decodes a [Color] from an RGB string (e.g. `rgb(255, 100, 25)`). | ||
*/ | ||
object RgbColorDecoder : ColorDecoder { | ||
override fun decode(raw: String): Color? { | ||
if (!raw.startsWith("rgb(")) return null | ||
|
||
val match = Regex("rgb\\((\\d{1,3}), ?(\\d{1,3}), ?(\\d{1,3})\\)").find(raw) ?: return null | ||
val (r, g, b) = extractRGBComponents(match) | ||
|
||
return if (r != null && g != null && b != null) { | ||
Color(r, g, b) | ||
} else { | ||
null | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Decodes a [Color] from an RGBA string (e.g. `rgba(255, 100, 25, 0.5)`). | ||
*/ | ||
object RgbaColorDecoder : ColorDecoder { | ||
override fun decode(raw: String): Color? { | ||
if (!raw.startsWith("rgba(")) return null | ||
|
||
val match = Regex("rgba\\((\\d{1,3}), ?(\\d{1,3}), ?(\\d{1,3}), ?([0-9.]+)\\)").find(raw) ?: return null | ||
val (r, g, b) = extractRGBComponents(match) | ||
|
||
val a = match.destructured.component4().toDoubleOrNull()?.takeIf { it <= Color.MAX_ALPHA } | ||
|
||
return if (r != null && g != null && b != null && a != null) { | ||
Color(r, g, b, a) | ||
} else { | ||
null | ||
} | ||
} | ||
} |
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