adjust icon and text color of status/navigation bar if API is provided.

This commit is contained in:
tateisu 2019-09-30 05:38:09 +09:00
parent a1ca016558
commit 5feb228ddc
3 changed files with 134 additions and 63 deletions

View File

@ -13,6 +13,7 @@ import android.graphics.Color
import android.net.Uri
import android.os.Build
import android.util.Log
import android.view.View
import android.view.WindowManager
import androidx.browser.customtabs.CustomTabsIntent
import com.bumptech.glide.Glide
@ -48,6 +49,7 @@ import java.util.concurrent.atomic.AtomicInteger
import java.util.regex.Pattern
import kotlin.math.max
import kotlin.math.min
import kotlin.math.pow
class App1 : Application() {
@ -643,18 +645,88 @@ class App1 : Application() {
// }
}
private fun rgbToLab(rgb : Int) : Triple<Float, Float, Float> {
fun Int.revGamma() : Float {
val v = toFloat() / 255f
return when {
v > 0.04045f -> ((v + 0.055f) / 1.055f).pow(2.4f)
else -> v / 12.92f
}
}
val r = Color.red(rgb).revGamma()
val g = Color.green(rgb).revGamma()
val b = Color.blue(rgb).revGamma()
//https://en.wikipedia.org/wiki/Lab_color_space#CIELAB-CIEXYZ_conversions
fun f(src : Float, k : Float) : Float {
val v = src * k
return when {
v > 0.008856f -> v.pow(1f / 3f)
else -> (7.787f * v) + (4f / 29f)
}
}
val x = f(r * 0.4124f + g * 0.3576f + b * 0.1805f, 100f / 95.047f)
val y = f(r * 0.2126f + g * 0.7152f + b * 0.0722f, 100f / 100f)
val z = f(r * 0.0193f + g * 0.1192f + b * 0.9505f, 100f / 108.883f)
return Triple(
(116 * y) - 16, // L
500 * (x - y), // a
200 * (y - z) //b
)
}
fun setStatusBarColor(activity : Activity) {
activity.window?.apply {
var c = Pref.ipStatusBarColor(pref).notZero() ?: getAttributeColor(activity,R.attr.colorPrimaryDark)
clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
var c = Pref.ipStatusBarColor(pref).notZero() ?: getAttributeColor(
activity,
R.attr.colorPrimaryDark
)
statusBarColor = c or Color.BLACK
if(Build.VERSION.SDK_INT >= 23) {
val min = rgbToLab(Color.BLACK)
val max = rgbToLab(Color.WHITE)
val current = rgbToLab(c)
decorView.systemUiVisibility =
if(current.first >= (min.first + max.first) / 2f) {
//Dark Text to show up on your light status bar
decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
} else {
//Light Text to show up on your dark status bar
decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
}
}
c = Pref.ipNavigationBarColor(pref)
if(c != 0) {
navigationBarColor = c or Color.BLACK
if( Build.VERSION.SDK_INT >= 26){
val current = rgbToLab(c)
decorView.systemUiVisibility =
if(current.first >= 50) {
//Dark Text to show up on your light status bar
decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
} else {
//Light Text to show up on your dark status bar
decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR.inv()
}
}
} // else: need restart app.
}
}
}

View File

@ -936,7 +936,7 @@
<string name="show_in_directory">ディレクトリに表示</string>
<string name="featured_hashtags">注目のハッシュタグ</string>
<string name="custom_emoji">カスタム絵文字</string>
<string name="status_bar_color">ステータスバーの色(アプリ再起動が必要)</string>
<string name="navigation_bar_color">ナビゲーションバーの色(アプリ再起動が必要)</string>
<string name="status_bar_color">ステータスバーの色(アプリ再起動が必要。Android 6未満はアイコンやテキストの色を調整できません)</string>
<string name="navigation_bar_color">ナビゲーションバーの色(アプリ再起動が必要。Android 8未満はアイコンの色を調整できません)</string>
</resources>

View File

@ -929,7 +929,6 @@
<string name="show_in_directory">Show in directory</string>
<string name="featured_hashtags">Featured hashtags</string>
<string name="custom_emoji">Custom emoji</string>
<string name="status_bar_color">Status bar color (app restart required)</string>
<string name="navigation_bar_color">Navigation bar color (app restart required)</string>
<string name="status_bar_color">Status bar color (app restart required. pre-Android 6 can\'t adjust icon and text color)</string>
<string name="navigation_bar_color">Navigation bar color (app restart required. pre-Android 8 can\'t adjust icon color)</string>
</resources>