-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Basic CSS Color Parsing #24
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6244dcb
Add CSS color parsing (and also write a few bonus tests)
cedrickcooke 1d3d186
Fix codecov path fixes
cedrickcooke bb395cf
Use string prefix to determine parsing technique
cedrickcooke c0bca12
Remove accidental double blank line
cedrickcooke b140fbc
Add tests for the Random extensions
cedrickcooke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.juul.krayon.color | ||
|
||
private const val HEX = "[a-f0-9]" | ||
|
||
private val RGB = """($HEX)($HEX)($HEX)""".toRegex(RegexOption.IGNORE_CASE) | ||
private val RGBA = """($HEX)($HEX)($HEX)($HEX)""".toRegex(RegexOption.IGNORE_CASE) | ||
private val RRGGBB = """($HEX{2})($HEX{2})($HEX{2})""".toRegex(RegexOption.IGNORE_CASE) | ||
private val RRGGBBAA = """($HEX{2})($HEX{2})($HEX{2})($HEX{2})""".toRegex(RegexOption.IGNORE_CASE) | ||
|
||
public fun String.toColor(): Color = | ||
keywordMap[this.toLowerCase()] ?: parseHexNotation(this) | ||
cedrickcooke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public fun String.toColorOrNull(): Color? = | ||
try { | ||
toColor() | ||
} catch (e: IllegalArgumentException) { | ||
null | ||
} | ||
|
||
private fun parseHexNotation(text: String): Color { | ||
require(text.startsWith("#")) { "Hex notation must start with a pound sign (#)." } | ||
val hexText = text.substring(1) | ||
val match = when (hexText.length) { | ||
3 -> RGB.matchEntire(hexText) | ||
4 -> RGBA.matchEntire(hexText) | ||
6 -> RRGGBB.matchEntire(hexText) | ||
8 -> RRGGBBAA.matchEntire(hexText) | ||
else -> throw IllegalArgumentException("Incorrect length. Hex notation must contain exactly 3, 4, 6, or 8 hexadecimal characters.") | ||
} ?: throw IllegalArgumentException("Text is not a hexadecimal string. Each character must match the regex [a-fA-F0-9].") | ||
val red = match.groupValues[1] | ||
val green = match.groupValues[2] | ||
val blue = match.groupValues[3] | ||
val alpha = match.groupValues.getOrNull(4) ?: "ff" | ||
return Color(parseHexComponent(alpha), parseHexComponent(red), parseHexComponent(green), parseHexComponent(blue)) | ||
} | ||
|
||
private fun parseHexComponent(component: String): Int = | ||
if (component.length == 1) { | ||
// Per MDN, "The three-digit notation (#RGB) is a shorter version of the six-digit form (#RRGGBB)." | ||
// Achieve that by just calling this function again with the component repeated. | ||
parseHexComponent(component + component) | ||
} else { | ||
component.toInt(HEX_BASE) | ||
} |
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,12 @@ | ||
package com.juul.krayon.color | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class OperationsTests { | ||
|
||
@Test | ||
fun lerp_fromWhiteToBlack_isGray() { | ||
assertEquals(gray, lerp(white, black, 0.5f)) | ||
} | ||
} |
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,68 @@ | ||
package com.juul.krayon.color | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
import kotlin.test.assertNull | ||
|
||
class ToColorTests { | ||
|
||
@Test | ||
fun toColor_withKeyword_succeeds() { | ||
assertEquals(cornflowerBlue, "cornflowerBlue".toColor()) | ||
} | ||
|
||
@Test | ||
fun toColor_withRGBInput_succeeds() { | ||
assertEquals(red, "#f00".toColor()) | ||
assertEquals(lime, "#0f0".toColor()) | ||
assertEquals(blue, "#00f".toColor()) | ||
} | ||
|
||
@Test | ||
fun toColor_withRGBAInput_succeeds() { | ||
assertEquals(red.copy(alpha = 0x00), "#f000".toColor()) | ||
assertEquals(lime.copy(alpha = 0x88), "#0f08".toColor()) | ||
assertEquals(blue.copy(alpha = 0xFF), "#00ff".toColor()) | ||
} | ||
|
||
@Test | ||
fun toColor_withRRGGBBInput_succeeds() { | ||
assertEquals(cornflowerBlue, "#6495ed".toColor()) | ||
} | ||
|
||
@Test | ||
fun toColor_withRRGGBBAAInput_succeeds() { | ||
assertEquals(cornflowerBlue.copy(alpha = 0x48), "#6495ed48".toColor()) | ||
} | ||
|
||
@Test | ||
fun toColor_onMissingCrunch_throwsException() { | ||
assertFailsWith<IllegalArgumentException> { "fff".toColor() } | ||
} | ||
|
||
@Test | ||
fun toColorOrNull_onMissingCrunch_returnsNull() { | ||
assertNull("fff".toColorOrNull()) | ||
} | ||
|
||
@Test | ||
fun toColor_onIncorrectLength_throwsException() { | ||
assertFailsWith<IllegalArgumentException> { "#fffff".toColor() } | ||
} | ||
|
||
@Test | ||
fun toColorOrNull_onIncorrectLength_returnsNull() { | ||
assertNull("#fffff".toColorOrNull()) | ||
} | ||
|
||
@Test | ||
fun toColor_onInvalidCharacter_throwsException() { | ||
assertFailsWith<IllegalArgumentException> { "#fgf".toColor() } | ||
} | ||
|
||
@Test | ||
fun toColorOrNull_onInvalidCharacter_returnsNull() { | ||
assertNull("#fgf".toColorOrNull()) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect that regex approach is slower than processing by simple iteration.
But we can definitely postpone that optimization (and should benchmark to verify that regex is even slower).
Created #25 (super low priority though) to potentially address this later.