@@ -78,8 +78,8 @@ class Theme private constructor(isDarkMode: Boolean) {
78
78
79
79
private val defaultThemeName = " trime"
80
80
81
- fun isImageString (str : String? ): Boolean {
82
- return str?.contains(Regex (" .(png|jpg) " )) == true
81
+ fun isFileString (str : String? ): Boolean {
82
+ return str?.contains(Regex (""" \.[a-z]{3,4}$ "" " )) == true
83
83
}
84
84
}
85
85
@@ -179,8 +179,10 @@ class Theme private constructor(isDarkMode: Boolean) {
179
179
m : Map <String ?, Any ?>,
180
180
key : String? ,
181
181
): Int? {
182
- if (m[key] == null ) return null
183
- return ColorUtils .parseColor(m[key] as String? ) ? : getColor(m[key] as String? )
182
+ var value = theme.getColorValue(m[key] as String? )
183
+ // 回退到配色
184
+ if (value == null ) value = theme.currentColors[key]
185
+ return if (value is Int ) value else null
184
186
}
185
187
186
188
// 返回drawable。 Config 2.0
@@ -200,29 +202,15 @@ class Theme private constructor(isDarkMode: Boolean) {
200
202
m : Map <String ?, Any ?>,
201
203
key : String ,
202
204
): Drawable ? {
203
- m[key] ? : return null
204
- val value = m[key] as String?
205
- val override = ColorUtils .parseColor(value)
206
- if (override != null ) {
207
- return GradientDrawable ().apply { setColor(override ) }
208
- }
209
-
210
- // value maybe a color label or a image path
211
- return if (isImageString(value)) {
212
- val path = theme.getImagePath(value!! )
213
- if (path.isNotEmpty()) {
214
- bitmapDrawable(path)
215
- } else {
216
- // fallback if image not found
217
- getDrawable(key)
218
- }
219
- } else if (theme.currentColors.containsKey(value)) {
220
- // use custom color label
221
- getDrawable(value!! )
222
- } else {
223
- // fallback color
224
- getDrawable(key)
205
+ var value = theme.getColorValue(m[key] as String? )
206
+ // 回退到配色
207
+ if (value == null ) value = theme.currentColors[key]
208
+ if (value is Int ) {
209
+ return GradientDrawable ().apply { setColor(value) }
210
+ } else if (value is String ) {
211
+ return bitmapDrawable(value)
225
212
}
213
+ return null
226
214
}
227
215
228
216
// 返回图片或背景的drawable,支持null参数。 Config 2.0
@@ -233,26 +221,19 @@ class Theme private constructor(isDarkMode: Boolean) {
233
221
roundCornerKey : String ,
234
222
alphaKey : String? ,
235
223
): Drawable ? {
236
- if (key == null ) return null
237
- val o = theme.currentColors[key]
238
- var color = o
239
- if (o is String ) {
240
- if (isImageString(o)) {
241
- val bitmap = bitmapDrawable(o)
242
- if (bitmap != null ) {
243
- if (! alphaKey.isNullOrEmpty() && theme.style.getObject(alphaKey) != null ) {
244
- bitmap.alpha = MathUtils .clamp(theme.style.getInt(alphaKey), 0 , 255 )
245
- }
246
- return bitmap
224
+ val value = theme.getColorValue(key)
225
+ if (value is String ) {
226
+ val bitmap = bitmapDrawable(value)
227
+ if (bitmap != null ) {
228
+ if (! alphaKey.isNullOrEmpty() && theme.style.getObject(alphaKey) != null ) {
229
+ bitmap.alpha = MathUtils .clamp(theme.style.getInt(alphaKey), 0 , 255 )
247
230
}
248
- } else {
249
- // it is html hex color string (e.g. #ff0000)
250
- color = ColorUtils .parseColor(o)
231
+ return bitmap
251
232
}
252
233
}
253
234
254
- if (color is Int ) {
255
- val gradient = GradientDrawable ().apply { setColor(color ) }
235
+ if (value is Int ) {
236
+ val gradient = GradientDrawable ().apply { setColor(value ) }
256
237
if (roundCornerKey.isNotEmpty()) {
257
238
gradient.cornerRadius = theme.style.getFloat(roundCornerKey)
258
239
}
@@ -374,20 +355,6 @@ class Theme private constructor(isDarkMode: Boolean) {
374
355
return keyboardPadding
375
356
}
376
357
377
- // 获取当前配色方案的key的value,或者从fallback获取值。
378
- private fun getColorValue (key : String? ): Any? {
379
- val map = presetColorSchemes!! [currentColorSchemeId] ? : return null
380
- var value: Any?
381
- var newKey = key
382
- val limit = fallbackColors!! .size * 2
383
- for (i in 0 until limit) {
384
- value = map[newKey]
385
- if (value != null || ! fallbackColors!! .containsKey(newKey)) return value
386
- newKey = fallbackColors!! [newKey]
387
- }
388
- return null
389
- }
390
-
391
358
var hasDarkLight = false
392
359
private set
393
360
@@ -442,54 +409,75 @@ class Theme private constructor(isDarkMode: Boolean) {
442
409
currentColorSchemeId,
443
410
darkMode,
444
411
)
445
- cacheColorValues ()
412
+ refreshColorValues ()
446
413
}
447
414
448
- private fun cacheColorValues () {
415
+ private fun refreshColorValues () {
449
416
currentColors.clear()
450
417
val colorMap = presetColorSchemes!! [currentColorSchemeId]
451
418
if (colorMap == null ) {
452
419
Timber .w(" Color scheme id not found: %s" , currentColorSchemeId)
453
420
return
454
421
}
455
422
456
- for ((key, value1 ) in colorMap) {
423
+ for ((key, value ) in colorMap) {
457
424
if (key == " name" || key == " author" || key == " light_scheme" || key == " dark_scheme" ) continue
458
- val value = parseColorValue(value1)
459
- if (value != null ) currentColors[key] = value
425
+ currentColors[key] = value
460
426
}
461
- for ((key) in fallbackColors!! ) {
427
+ for ((key, value ) in fallbackColors!! ) {
462
428
if (! currentColors.containsKey(key)) {
463
- val value = parseColorValue(getColorValue(key))
464
- if (value != null ) currentColors[key] = value
429
+ currentColors[key] = value
430
+ }
431
+ }
432
+
433
+ for ((key, value) in currentColors) {
434
+ val parsedValue = getColorValue(value)
435
+ if (parsedValue != null ) {
436
+ currentColors[key] = parsedValue
437
+ } else {
438
+ Timber .w(" Cannot parse color key: %s, value: %s" , key, value)
465
439
}
466
440
}
467
441
}
468
442
443
+ // 处理 value 值,转为颜色(Int)或图片path string 或者 fallback
444
+ // 处理失败返回 null
445
+ private fun getColorValue (value : Any? ): Any? {
446
+ val parsedValue = parseColorValue(value)
447
+ if (parsedValue != null ) {
448
+ return parsedValue
449
+ }
450
+ var newKey = value
451
+ var newValue: Any?
452
+ val limit = currentColors.size
453
+ for (i in 0 until limit) {
454
+ newValue = currentColors[newKey]
455
+ if (newValue == null ) return null
456
+ if (newValue is Int ) return newValue
457
+ if (isFileString(newValue as String )) return newValue
458
+
459
+ val parsedNewValue = parseColorValue(newValue)
460
+ if (parsedNewValue != null ) {
461
+ return parsedNewValue
462
+ }
463
+ newKey = newValue
464
+ }
465
+ return null
466
+ }
467
+
469
468
// 获取参数的真实value,Config 2.0
470
469
// 如果是色彩返回int,如果是背景图返回path string,如果处理失败返回null
471
470
private fun parseColorValue (value : Any? ): Any? {
472
- value ? : return null
473
471
if (value is String ) {
474
- if (value.matches(" .*[.\\\\ /].*" .toRegex())) {
475
- return getImagePath(value)
476
- } else {
477
- runCatching {
478
- return ColorUtils .parseColor(value)
479
- }.getOrElse {
480
- Timber .e(" Unknown color value $value : ${it.message} " )
472
+ if (isFileString(value)) {
473
+ // 获取图片的真实地址
474
+ val fullPath = joinToFullImagePath(value)
475
+ if (File (fullPath).exists()) {
476
+ return fullPath
481
477
}
482
478
}
479
+ return ColorUtils .parseColor(value)
483
480
}
484
481
return null
485
482
}
486
-
487
- private fun getImagePath (value : String ): String {
488
- return if (value.matches(" .*[.\\\\ /].*" .toRegex())) {
489
- val fullPath = joinToFullImagePath(value)
490
- if (File (fullPath).exists()) fullPath else " "
491
- } else {
492
- " "
493
- }
494
- }
495
483
}
0 commit comments