Skip to content

Commit

Permalink
RGBA colors of status bar Fix Android (#7390)
Browse files Browse the repository at this point in the history
* Use alpha value from status bar color when present

* add test for alpha colors
  • Loading branch information
swabbass authored Dec 6, 2021
1 parent c52ccb8 commit c14724e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import kotlin.math.ceil
object SystemUiUtils {
private const val STATUS_BAR_HEIGHT_M = 24
private const val STATUS_BAR_HEIGHT_L = 25
private const val STATUS_BAR_HEIGHT_TRANSLUCENCY = 0.65f
internal const val STATUS_BAR_HEIGHT_TRANSLUCENCY = 0.65f
private var statusBarHeight = -1
var navigationBarDefaultColor = -1
private set
Expand Down Expand Up @@ -124,7 +124,8 @@ object SystemUiUtils {
val opaqueColor = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
Color.BLACK
}else{
val alpha = if (translucent) STATUS_BAR_HEIGHT_TRANSLUCENCY else 1f
val colorAlpha = Color.alpha(color)
val alpha = if (translucent && colorAlpha == 255) STATUS_BAR_HEIGHT_TRANSLUCENCY else colorAlpha/255.0f
val red: Int = Color.red(color)
val green: Int = Color.green(color)
val blue: Int = Color.blue(color)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.reactnativenavigation.utils

import android.graphics.Color
import android.view.Window
import com.reactnativenavigation.BaseTest
import com.reactnativenavigation.utils.SystemUiUtils.STATUS_BAR_HEIGHT_TRANSLUCENCY
import org.junit.Test
import org.mockito.Mockito
import org.mockito.kotlin.verify
import kotlin.math.ceil

class SystemUiUtilsTest : BaseTest() {

@Test
fun `setStatusBarColor - should change color considering alpha`() {
val window = Mockito.mock(Window::class.java)
val alphaColor = Color.argb(44, 22, 255, 255)
val color = Color.argb(255, 22, 255, 255)
SystemUiUtils.setStatusBarColor(window, alphaColor, false)

verify(window).statusBarColor = alphaColor

SystemUiUtils.setStatusBarColor(window, color, true)

verify(window).statusBarColor = Color.argb(ceil(STATUS_BAR_HEIGHT_TRANSLUCENCY*255).toInt(), 22, 255, 255)
}
}
1 change: 1 addition & 0 deletions playground/src/screens/SystemUiOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface State {
}
const colors = [
'#000000',
'#00AAAF00',
'#20303C',
'#3182C8',
'#00AAAF',
Expand Down

0 comments on commit c14724e

Please sign in to comment.