diff options
author | Ricki Hirner <hirner@bitfire.at> | 2022-03-16 13:01:28 +0300 |
---|---|---|
committer | Ricki Hirner <hirner@bitfire.at> | 2022-03-16 13:01:28 +0300 |
commit | 9bca5faccdd7095f1ded5fcdb7bffd571584a34c (patch) | |
tree | d868deca4b4299a9d15b155b8fbcba09e24f1b76 | |
parent | 73058b1ca618a465559be47ecd1be398b4ea3505 (diff) |
Css3Color: treat color names case-insensitive and parse hex values if possible
3 files changed, 67 insertions, 25 deletions
diff --git a/src/androidTest/java/at/bitfire/ical4android/Css3ColorTest.kt b/src/androidTest/java/at/bitfire/ical4android/Css3ColorTest.kt new file mode 100644 index 0000000..b89dad8 --- /dev/null +++ b/src/androidTest/java/at/bitfire/ical4android/Css3ColorTest.kt @@ -0,0 +1,48 @@ +/*************************************************************************************************** + * Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details. + **************************************************************************************************/ + +package at.bitfire.ical4android + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Test + +class Css3ColorTest { + + @Test + fun testColorFromString() { + // color name + assertEquals(0xffffff00.toInt(), Css3Color.colorFromString("yellow")) + + // RGB value + assertEquals(0xffffff00.toInt(), Css3Color.colorFromString("#ffff00")) + + // ARGB value + assertEquals(0xffffff00.toInt(), Css3Color.colorFromString("#ffffff00")) + + // invalid value + assertNull(Css3Color.colorFromString("DoesNotExist")) + } + + @Test + fun testFromString() { + // lower case + assertEquals(0xffffff00.toInt(), Css3Color.fromString("yellow")?.argb) + + // capitalized + assertEquals(0xffffff00.toInt(), Css3Color.fromString("Yellow")?.argb) + + // not-existing color + assertNull(Css3Color.fromString("DoesNotExist")) + } + + @Test + fun testNearestMatch() { + // every color is its own nearest match + Css3Color.values().forEach { + assertEquals(it.argb, Css3Color.nearestMatch(it.argb).argb) + } + } + +}
\ No newline at end of file diff --git a/src/main/java/at/bitfire/ical4android/Css3Color.kt b/src/main/java/at/bitfire/ical4android/Css3Color.kt index 57df5a2..f6190a0 100644 --- a/src/main/java/at/bitfire/ical4android/Css3Color.kt +++ b/src/main/java/at/bitfire/ical4android/Css3Color.kt @@ -4,6 +4,7 @@ package at.bitfire.ical4android +import android.graphics.Color import kotlin.math.sqrt /** @@ -166,16 +167,29 @@ enum class Css3Color(val argb: Int) { companion object { /** - * Returns the CSS3 color property of the given name. + * Parses the given color either as CSS3 color name or as (A)RGB hex value. * - * @param name color name - * @return [Css3Color] object or null if no match was found + * @param color CSS3 color name like "blue" or (A)RGB hex value like #0000FF + * @return ARGB color value or *null* if the color couldn't be parsed + */ + fun colorFromString(color: String): Int? = + fromString(color)?.argb ?: + try { + Color.parseColor(color) + } catch(e: IllegalArgumentException) { + null + } + + /** + * Returns the Css3Color object of the given CSS3 color name. + * + * @param name CSS3 color name like "blue" + * @return [Css3Color] object or *null* if no match was found */ fun fromString(name: String) = try { - valueOf(name) + valueOf(name.lowercase()) } catch (e: IllegalArgumentException) { - Ical4Android.log.warning("Unknown color: $name") null } diff --git a/src/test/java/at/bitfire/ical4android/Css3ColorTest.kt b/src/test/java/at/bitfire/ical4android/Css3ColorTest.kt deleted file mode 100644 index 9828752..0000000 --- a/src/test/java/at/bitfire/ical4android/Css3ColorTest.kt +++ /dev/null @@ -1,20 +0,0 @@ -/*************************************************************************************************** - * Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details. - **************************************************************************************************/ - -package at.bitfire.ical4android - -import org.junit.Assert.assertEquals -import org.junit.Test - -class Css3ColorTest { - - @Test - fun testNearestMatch() { - // every color is its own nearest match - Css3Color.values().forEach { - assertEquals(it.argb, Css3Color.nearestMatch(it.argb).argb) - } - } - -}
\ No newline at end of file |