Skip to content

Commit

Permalink
Updated formatCssString method to manage NaN when percentUnits is set…
Browse files Browse the repository at this point in the history
… to true (#57)

Edits to CssRender.kt to manage NaN with percentUnits = true; added formatCssString tests to CssRenderTest.kt for HSV and Oklch
  • Loading branch information
danielemaddaluno authored Mar 24, 2024
1 parent 5f3745c commit 1ac764c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,12 @@ private fun Color.renderAlpha(commas: Boolean, renderAlpha: RenderCondition, alp
}

private fun Float.render(percent: Boolean = false): String = when (percent) {
true -> "${(this * 100).roundToInt()}%"
true -> when {
this.isNaN() -> "NaN"
else -> {
"${(this * 100).roundToInt()}%"
}
}
false -> when (this) {
0f -> "0"
1f -> "1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import com.github.ajalt.colormath.AngleUnit.*
import com.github.ajalt.colormath.RenderCondition.*
import com.github.ajalt.colormath.RenderCondition.AUTO
import com.github.ajalt.colormath.model.HSL
import com.github.ajalt.colormath.model.HSV
import com.github.ajalt.colormath.model.HWB
import com.github.ajalt.colormath.model.JzAzBz
import com.github.ajalt.colormath.model.LABColorSpaces.LAB50
import com.github.ajalt.colormath.model.LCHabColorSpaces.LCHab50
import com.github.ajalt.colormath.model.Oklch
import com.github.ajalt.colormath.model.RGB
import com.github.ajalt.colormath.model.RGBColorSpaces.ACES
import com.github.ajalt.colormath.model.RGBColorSpaces.ACEScc
Expand Down Expand Up @@ -46,6 +48,32 @@ class CssRenderTest {
val renderAlpha: RenderCondition = AUTO,
)

private data class H2(
val h: Number,
val s: Number,
val v: Number,
val a: Float = 1f,
val commas: Boolean = false,
val namedHsla: Boolean = false,
val hueUnit: AngleUnit = AngleUnit.AUTO,
val alphaPercent: Boolean = false,
val renderAlpha: RenderCondition = AUTO,
val unitsPercent: Boolean = false
)

private data class O(
val l: Number,
val c: Number,
val h: Number,
val a: Float = 1f,
val commas: Boolean = false,
val namedHsla: Boolean = false,
val hueUnit: AngleUnit = AngleUnit.AUTO,
val alphaPercent: Boolean = false,
val renderAlpha: RenderCondition = AUTO,
val unitsPercent: Boolean = false
)

@Test
fun formatCssRgb() = forAll(
row(R(0, 0, 0), "rgb(0 0 0)"),
Expand Down Expand Up @@ -122,4 +150,36 @@ class CssRenderTest {
) { color, expected ->
color.formatCssStringOrNull() shouldBe expected
}

@Test
fun formatCssHsv() = forAll(
row(H2(0, 1, 1, unitsPercent = false), "color(--hsv 0 1 1)"),
row(H2(0, 1, 1, unitsPercent = true), "color(--hsv 0% 100% 100%)"),
row(H2(Float.NaN, 0, 1, unitsPercent = false), "color(--hsv NaN 0 1)"),
row(H2(Float.NaN, 0, 1, unitsPercent = true), "color(--hsv NaN 0% 100%)"),
) { (h, s, v, a, commas, namedHsla, hueUnit, alphaPercent, renderAlpha, unitsPercent), expected ->
HSV(h, s, v, a).formatCssString(
hueUnit,
renderAlpha,
unitsPercent,
alphaPercent,
namedHsla,
commas
) shouldBe expected
}

@Test
fun formatCssOklch() = forAll(
row(O(0, 0, Float.NaN, unitsPercent = false), "color(--oklch 0 0 NaN)"),
row(O(0, 0, Float.NaN, unitsPercent = true), "color(--oklch 0% 0% NaN)"),
) { (l, c, h, a, commas, namedHsla, hueUnit, alphaPercent, renderAlpha, unitsPercent), expected ->
Oklch(l, c, h, a).formatCssString(
hueUnit,
renderAlpha,
unitsPercent,
alphaPercent,
namedHsla,
commas
) shouldBe expected
}
}

0 comments on commit 1ac764c

Please sign in to comment.