mirror of
https://github.com/nextcloud/talk-android
synced 2025-03-06 14:27:24 +00:00
safeguard theme values in case of null values
Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
parent
d9c59b6f87
commit
d4c07f1278
70
app/src/main/java/com/nextcloud/talk/ui/theme/ColorUtil.kt
Normal file
70
app/src/main/java/com/nextcloud/talk/ui/theme/ColorUtil.kt
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* Nextcloud Talk application
|
||||||
|
*
|
||||||
|
* @author Andy Scherzinger
|
||||||
|
* Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package com.nextcloud.talk.ui.theme
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.graphics.Color
|
||||||
|
import androidx.annotation.ColorInt
|
||||||
|
import androidx.annotation.ColorRes
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
import androidx.core.graphics.ColorUtils
|
||||||
|
import com.nextcloud.talk.R
|
||||||
|
|
||||||
|
object ColorUtil {
|
||||||
|
private const val HSL_SIZE: Int = 3
|
||||||
|
private const val INDEX_LUMINATION: Int = 2
|
||||||
|
private const val LUMINATION_DARK_THRESHOLD: Float = 0.6f
|
||||||
|
|
||||||
|
fun getPrimaryColor(context: Context, primaryColor: String?, @ColorRes fallbackColor: Int): Int {
|
||||||
|
return if (primaryColor != null) {
|
||||||
|
Color.parseColor(primaryColor)
|
||||||
|
} else {
|
||||||
|
ContextCompat.getColor(context, fallbackColor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getNullsafeColor(color: String?, @ColorInt fallbackColor: Int): Int {
|
||||||
|
return if (color != null) {
|
||||||
|
Color.parseColor(color)
|
||||||
|
} else {
|
||||||
|
fallbackColor
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getTextColor(context: Context, colorText: String?, @ColorInt fallBackPrimaryColor: Int): Int {
|
||||||
|
return if (colorText != null) {
|
||||||
|
Color.parseColor(colorText)
|
||||||
|
} else {
|
||||||
|
getForegroundColorForBackgroundColor(context, fallBackPrimaryColor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ColorInt
|
||||||
|
public fun getForegroundColorForBackgroundColor(context: Context, @ColorInt color: Int): Int {
|
||||||
|
val hsl = FloatArray(HSL_SIZE)
|
||||||
|
ColorUtils.RGBToHSL(Color.red(color), Color.green(color), Color.blue(color), hsl)
|
||||||
|
|
||||||
|
return if (hsl[INDEX_LUMINATION] < LUMINATION_DARK_THRESHOLD) {
|
||||||
|
Color.WHITE
|
||||||
|
} else {
|
||||||
|
ContextCompat.getColor(context, R.color.grey_900)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,9 @@
|
|||||||
* Nextcloud Talk application
|
* Nextcloud Talk application
|
||||||
*
|
*
|
||||||
* @author Álvaro Brey
|
* @author Álvaro Brey
|
||||||
|
* @author Andy Scherzinger
|
||||||
* Copyright (C) 2022 Álvaro Brey
|
* Copyright (C) 2022 Álvaro Brey
|
||||||
|
* Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
|
||||||
* Copyright (C) 2022 Nextcloud GmbH
|
* Copyright (C) 2022 Nextcloud GmbH
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
@ -22,7 +24,7 @@
|
|||||||
package com.nextcloud.talk.ui.theme
|
package com.nextcloud.talk.ui.theme
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.graphics.Color
|
import com.nextcloud.talk.R
|
||||||
import com.nextcloud.talk.models.json.capabilities.ThemingCapability
|
import com.nextcloud.talk.models.json.capabilities.ThemingCapability
|
||||||
|
|
||||||
internal class ServerThemeImpl(context: Context, themingCapability: ThemingCapability) :
|
internal class ServerThemeImpl(context: Context, themingCapability: ThemingCapability) :
|
||||||
@ -34,12 +36,13 @@ internal class ServerThemeImpl(context: Context, themingCapability: ThemingCapab
|
|||||||
override val colorElementDark: Int
|
override val colorElementDark: Int
|
||||||
override val colorText: Int
|
override val colorText: Int
|
||||||
|
|
||||||
// TODO fallback when some of these are null
|
|
||||||
init {
|
init {
|
||||||
primaryColor = Color.parseColor(themingCapability.color!!)
|
primaryColor = ColorUtil.getPrimaryColor(context, themingCapability.color, R.color.colorPrimary)
|
||||||
colorElement = Color.parseColor(themingCapability.colorElement!!)
|
|
||||||
colorElementBright = Color.parseColor(themingCapability.colorElementBright!!)
|
colorElement = ColorUtil.getNullsafeColor(themingCapability.colorElement, primaryColor)
|
||||||
colorElementDark = Color.parseColor(themingCapability.colorElementDark!!)
|
colorElementBright = ColorUtil.getNullsafeColor(themingCapability.colorElementBright, primaryColor)
|
||||||
colorText = Color.parseColor(themingCapability.colorText!!)
|
colorElementDark = ColorUtil.getNullsafeColor(themingCapability.colorElementDark, primaryColor)
|
||||||
|
|
||||||
|
colorText = ColorUtil.getTextColor(context, themingCapability.colorText, primaryColor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,6 +63,7 @@
|
|||||||
<color name="nc_darkGreen">#006400</color>
|
<color name="nc_darkGreen">#006400</color>
|
||||||
<color name="controller_chat_separator">#E8E8E8</color>
|
<color name="controller_chat_separator">#E8E8E8</color>
|
||||||
<color name="grey_600">#757575</color>
|
<color name="grey_600">#757575</color>
|
||||||
|
<color name="grey_900">#212121</color>
|
||||||
<color name="nc_grey">#D5D5D5</color>
|
<color name="nc_grey">#D5D5D5</color>
|
||||||
<color name="controller_call_incomingCallTextView">#E9FFFFFF</color>
|
<color name="controller_call_incomingCallTextView">#E9FFFFFF</color>
|
||||||
<color name="grey950">#111111</color>
|
<color name="grey950">#111111</color>
|
||||||
|
Loading…
Reference in New Issue
Block a user