safeguard theme values in case of null values

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
Andy Scherzinger 2022-07-28 16:06:52 +02:00
parent d9c59b6f87
commit d4c07f1278
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B
3 changed files with 81 additions and 7 deletions

View 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)
}
}
}

View file

@ -2,7 +2,9 @@
* Nextcloud Talk application
*
* @author Álvaro Brey
* @author Andy Scherzinger
* Copyright (C) 2022 Álvaro Brey
* Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
* Copyright (C) 2022 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
@ -22,7 +24,7 @@
package com.nextcloud.talk.ui.theme
import android.content.Context
import android.graphics.Color
import com.nextcloud.talk.R
import com.nextcloud.talk.models.json.capabilities.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 colorText: Int
// TODO fallback when some of these are null
init {
primaryColor = Color.parseColor(themingCapability.color!!)
colorElement = Color.parseColor(themingCapability.colorElement!!)
colorElementBright = Color.parseColor(themingCapability.colorElementBright!!)
colorElementDark = Color.parseColor(themingCapability.colorElementDark!!)
colorText = Color.parseColor(themingCapability.colorText!!)
primaryColor = ColorUtil.getPrimaryColor(context, themingCapability.color, R.color.colorPrimary)
colorElement = ColorUtil.getNullsafeColor(themingCapability.colorElement, primaryColor)
colorElementBright = ColorUtil.getNullsafeColor(themingCapability.colorElementBright, primaryColor)
colorElementDark = ColorUtil.getNullsafeColor(themingCapability.colorElementDark, primaryColor)
colorText = ColorUtil.getTextColor(context, themingCapability.colorText, primaryColor)
}
}

View file

@ -63,6 +63,7 @@
<color name="nc_darkGreen">#006400</color>
<color name="controller_chat_separator">#E8E8E8</color>
<color name="grey_600">#757575</color>
<color name="grey_900">#212121</color>
<color name="nc_grey">#D5D5D5</color>
<color name="controller_call_incomingCallTextView">#E9FFFFFF</color>
<color name="grey950">#111111</color>