• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.airbnb.lottie.sample.compose.ui
2 
3 import android.util.Log
4 import androidx.annotation.ColorInt
5 import androidx.compose.ui.graphics.Color
6 import androidx.core.graphics.toColorInt
7 import com.airbnb.lottie.L
8 
9 val purple200 = Color(0xFFBB86FC)
10 val TealDark = Color(0xFF484848)
11 val Teal = Color(0xFF009587)
12 
13 @ColorInt
toColorSafenull14 fun String?.toColorSafe(): Color {
15     var bgColor = this ?: "#ffffff"
16     bgColor = if (bgColor.startsWith("#")) bgColor else "#$bgColor"
17 
18     val colorInt = try {
19         when (bgColor.length) {
20             0 -> "#ffffff"
21             4 -> "#%c%c%c%c%c%c".format(
22                 bgColor[1], bgColor[1],
23                 bgColor[2], bgColor[2],
24                 bgColor[3], bgColor[3]
25             )
26             5 -> "#%c%c%c%c%c%c%c%c".format(
27                 bgColor[1], bgColor[1],
28                 bgColor[2], bgColor[2],
29                 bgColor[3], bgColor[3],
30                 bgColor[4], bgColor[4]
31             )
32             else -> bgColor
33         }.toColorInt()
34     } catch (e: IllegalArgumentException) {
35         Log.w(L.TAG, "Unable to parse $bgColor.")
36         android.graphics.Color.WHITE
37     }
38     return Color(colorInt)
39 }
40