Safer lottiefiles color parsing
diff --git a/LottieSample/src/main/kotlin/com/airbnb/lottie/samples/model/AnimationData.kt b/LottieSample/src/main/kotlin/com/airbnb/lottie/samples/model/AnimationData.kt
index 02116ae..5ccb954 100644
--- a/LottieSample/src/main/kotlin/com/airbnb/lottie/samples/model/AnimationData.kt
+++ b/LottieSample/src/main/kotlin/com/airbnb/lottie/samples/model/AnimationData.kt
@@ -1,8 +1,11 @@
package com.airbnb.lottie.samples.model
+import android.graphics.Color
import android.os.Parcelable
import android.support.annotation.ColorInt
+import android.util.Log
import androidx.graphics.toColorInt
+import com.airbnb.lottie.L
import kotlinx.android.parcel.Parcelize
// This is a lint bug
@@ -23,15 +26,28 @@
) : Parcelable {
@ColorInt
fun bgColorInt(): Int {
- val bgColor = this.bgColor ?: ""
- return when(bgColor.length) {
- 0 -> "#ffffff"
- 4 -> "#%c%c%c%c%c%c".format(
- bgColor[1], bgColor[1],
- bgColor[2], bgColor[2],
- bgColor[3], bgColor[3]
- )
- else -> bgColor
- } .toColorInt()
+ var bgColor = this.bgColor ?: "#ffffff"
+ bgColor = if (bgColor.startsWith("#")) bgColor else "#$bgColor"
+
+ return try {
+ when (bgColor.length) {
+ 0 -> "#ffffff"
+ 4 -> "#%c%c%c%c%c%c".format(
+ bgColor[1], bgColor[1],
+ bgColor[2], bgColor[2],
+ bgColor[3], bgColor[3]
+ )
+ 5 -> "#%c%c%c%c%c%c%c%c".format(
+ bgColor[1], bgColor[1],
+ bgColor[2], bgColor[2],
+ bgColor[3], bgColor[3],
+ bgColor[4], bgColor[4]
+ )
+ else -> bgColor
+ }.toColorInt()
+ } catch (e: IllegalArgumentException) {
+ Log.w(L.TAG, "Unable to parse $bgColor.")
+ Color.WHITE
+ }
}
}
\ No newline at end of file