-
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.
Implement HSV/HSL color decoding and use Colormath lib for hex decoding
- Loading branch information
Showing
9 changed files
with
128 additions
and
33 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
28 changes: 28 additions & 0 deletions
28
core/src/main/kotlin/eu/iamgio/quarkdown/misc/color/decoder/ColorDecoderUtils.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,28 @@ | ||
package eu.iamgio.quarkdown.misc.color.decoder | ||
|
||
import eu.iamgio.quarkdown.misc.color.Color | ||
|
||
/** | ||
* @param awtColor Java AWT color | ||
* @return a [Color] from a Java AWT color | ||
*/ | ||
fun Color.Companion.from(awtColor: java.awt.Color): Color = | ||
Color( | ||
red = awtColor.red, | ||
green = awtColor.green, | ||
blue = awtColor.blue, | ||
alpha = awtColor.alpha.toDouble() / 255, | ||
) | ||
|
||
/** | ||
* @param colormathColor Colormath color | ||
* @return a [Color] from a Colormath color | ||
*/ | ||
fun Color.Companion.from(colormathColor: com.github.ajalt.colormath.Color): Color = | ||
with(colormathColor.toSRGB()) { | ||
Color( | ||
red = (r * MAX_RGB).toInt(), | ||
green = (g * MAX_RGB).toInt(), | ||
blue = (b * MAX_RGB).toInt(), | ||
) | ||
} |
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
59 changes: 59 additions & 0 deletions
59
core/src/main/kotlin/eu/iamgio/quarkdown/misc/color/decoder/HsvHslColorDecoder.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,59 @@ | ||
package eu.iamgio.quarkdown.misc.color.decoder | ||
|
||
import com.github.ajalt.colormath.model.HSL | ||
import com.github.ajalt.colormath.model.HSV | ||
import eu.iamgio.quarkdown.misc.color.Color | ||
|
||
/** | ||
* Maximum value for the hue component. | ||
*/ | ||
private const val MAX_HUE = 360 | ||
|
||
/** | ||
* Maximum value for saturation and lightness/value components. | ||
*/ | ||
private const val MAX_SVL = 100 | ||
|
||
/** | ||
* Decodes a [Color] from an HSV or HSL string (e.g. `hsv(208, 70, 66)` or `hsl(208, 54, 43)`). | ||
*/ | ||
object HsvHslColorDecoder : ColorDecoder { | ||
override fun decode(raw: String): Color? { | ||
if (!raw.startsWith("hsl(") && !raw.startsWith("hsv(")) return null | ||
|
||
// HSL and HSV have the same structure (a degree (0-360) and two percentages (0-100)). | ||
for (method in arrayOf('l', 'v')) { | ||
val match = Regex("hs$method\\((\\d{1,3}), ?(\\d{1,3}), ?(\\d{1,3})\\)").find(raw) ?: continue | ||
val (h, s, lv) = | ||
match.destructured.let { (h, s, lv) -> | ||
Triple( | ||
// Hue (0-360). | ||
h.toFloatOrNull() | ||
?.rem(MAX_HUE), // e.g. 520 % 360 = 160 | ||
// Normalized saturation (0-1). | ||
s.toFloatOrNull() | ||
?.takeIf { it <= MAX_SVL } | ||
?.div(MAX_SVL), // [0, 100] -> [0, 1] | ||
// Normalized lightness/value (0-1). | ||
lv.toFloatOrNull() | ||
?.takeIf { it <= MAX_SVL } | ||
?.div(MAX_SVL), // [0, 100] -> [0, 1] | ||
) | ||
} | ||
|
||
if (h == null || s == null || lv == null) continue | ||
|
||
// Colormath color to be converted. | ||
val color = | ||
when (method) { | ||
'l' -> HSL(h, s, lv) | ||
'v' -> HSV(h, s, lv) | ||
else -> return null // Impossible | ||
} | ||
|
||
return Color.from(color) | ||
} | ||
|
||
return 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