Skip to content

Commit

Permalink
update: color?
Browse files Browse the repository at this point in the history
  • Loading branch information
ZTFtrue committed Feb 3, 2024
1 parent 26a88fa commit bc501b3
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 126 deletions.
142 changes: 60 additions & 82 deletions app/src/main/java/com/ztftrue/music/ui/other/SearchPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Divider
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.ProvideTextStyle
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
Expand Down Expand Up @@ -158,91 +160,67 @@ fun SearchPage(
) {
BackButton(navController)
Row(verticalAlignment = Alignment.CenterVertically) {
ProvideTextStyle(TextStyle(color = MaterialTheme.colorScheme.onPrimary)) {
OutlinedTextField(
value = keywords,
onValueChange = {
val newText = it.ifEmpty {
""
}
if (keywords != newText) {
keywords = newText
}
},
placeholder = {
Text("Enter text to search")
}, // Placeholder or hint text
keyboardOptions = KeyboardOptions.Default.copy(
imeAction = ImeAction.Done,
keyboardType = KeyboardType.Text
),
keyboardActions = KeyboardActions(
onDone = {
focusRequester.freeFocus()
keyboardController?.hide()
}
),
modifier = Modifier
.fillMaxWidth()
.focusRequester(focusRequester)
.background(MaterialTheme.colorScheme.primary),
suffix = {
CompositionLocalProvider(
LocalContentColor provides MaterialTheme.colorScheme.onPrimary
) {
ProvideTextStyle(TextStyle(color = MaterialTheme.colorScheme.onPrimary)) {
OutlinedTextField(
value = keywords,
onValueChange = {
val newText = it.ifEmpty {
""
}
if (keywords != newText) {
keywords = newText
}
},
placeholder = {
Text("Enter text to search")
}, // Placeholder or hint text
keyboardOptions = KeyboardOptions.Default.copy(
imeAction = ImeAction.Done,
keyboardType = KeyboardType.Text
),
keyboardActions = KeyboardActions(
onDone = {
focusRequester.freeFocus()
keyboardController?.hide()
}
),
modifier = Modifier
.fillMaxWidth()
.focusRequester(focusRequester)
.background(MaterialTheme.colorScheme.primary),
suffix = {

},
colors = TextFieldDefaults.colors(
focusedPlaceholderColor = Color.Red, // Set your desired text color here
unfocusedPlaceholderColor = Color.Red, // Set your desired text color here
disabledPlaceholderColor = Color.Red, // Set your desired text color here
errorPlaceholderColor = Color.Red, // Set your desired text color here
},
colors = TextFieldDefaults.colors(
focusedPlaceholderColor = Color.Red, // Set your desired text color here
unfocusedPlaceholderColor = Color.Red, // Set your desired text color here
disabledPlaceholderColor = Color.Red, // Set your desired text color here
errorPlaceholderColor = Color.Red, // Set your desired text color here
focusedLabelColor = Color.Red, // Set your desired text color here
unfocusedLabelColor = Color.Red, // Set your desired text color here
disabledLabelColor = Color.Red, // Set your desired text color here
errorLabelColor = Color.Red, // Set your desired text color here
focusedContainerColor = MaterialTheme.colorScheme.background, // Set your desired text color here
unfocusedContainerColor = MaterialTheme.colorScheme.background, // Set your desired text color here
disabledContainerColor = MaterialTheme.colorScheme.background, // Set your desired text color here
errorContainerColor = MaterialTheme.colorScheme.background, // Set your desired text color here
focusedTextColor = Color.Red, // Set your desired text color here
unfocusedTextColor = Color.Red, // Set your desired text color here
disabledTextColor = Color.Red, // Set your desired text color here
errorTextColor = Color.Red, // Set your desired text color here
focusedSupportingTextColor = Color.Red, // Set your desired text color here
unfocusedSupportingTextColor = Color.Red, // Set your desired text color here
disabledSupportingTextColor = Color.Red, // Set your desired text color here
errorSupportingTextColor = Color.Red, // Set your desired text color here
cursorColor = MaterialTheme.colorScheme.onBackground, // Set your desired text color here
errorCursorColor =MaterialTheme.colorScheme.onBackground, // Set your desired text color here
)
)
)
}
}
// TextField(
// value = keywords,
// onValueChange = {
// val newText = it.ifEmpty {
// ""
// }
// if (keywords != newText) {
// keywords = newText
// }
// },
// placeholder = {
// Text("Enter text")
// }, // Placeholder or hint text
// keyboardOptions = KeyboardOptions.Default.copy(
// imeAction = ImeAction.Done,
// keyboardType = KeyboardType.Text
// ),
// keyboardActions = KeyboardActions(
// onDone = {
// focusRequester.freeFocus()
// keyboardController?.hide()
// }
// ),
// modifier = Modifier
// .fillMaxWidth(0.9f)
// .focusRequester(focusRequester),
// suffix = {
//
// },
// colors = TextFieldDefaults.colors(
// focusedPlaceholderColor = Color.Red, // Set your desired text color here
// unfocusedPlaceholderColor = Color.Red, // Set your desired text color here
// disabledPlaceholderColor = Color.Red, // Set your desired text color here
// errorPlaceholderColor = Color.Red, // Set your desired text color here
// )
// )
// IconButton(onClick = {
// focusRequester.freeFocus()
// keyboardController?.hide()
// }) {
// Icon(
// Icons.Filled.Search, contentDescription = "Search",
// modifier = Modifier.size(40.dp)
// )
// }

}
}
}
Expand Down
32 changes: 24 additions & 8 deletions app/src/main/java/com/ztftrue/music/ui/play/LyricsView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.input.nestedscroll.nestedScroll
Expand Down Expand Up @@ -66,6 +67,8 @@ import androidx.compose.ui.window.PopupProperties
import androidx.media3.common.util.UnstableApi
import com.ztftrue.music.MainActivity
import com.ztftrue.music.MusicViewModel
import com.ztftrue.music.utils.CustomColorUtils.getContrastingColor
import com.ztftrue.music.utils.CustomColorUtils.toAndroidColor
import com.ztftrue.music.utils.ListStringCaption
import com.ztftrue.music.utils.LyricsType
import com.ztftrue.music.utils.Utils
Expand Down Expand Up @@ -299,7 +302,11 @@ fun LyricsView(
) {
val textToolbar = LocalTextToolbar.current
val focusManager = LocalFocusManager.current

val colorI: Int = getContrastingColor(
MaterialTheme.colorScheme.background.toArgb().toAndroidColor(),
MaterialTheme.colorScheme.onBackground.toArgb().toAndroidColor()
)
val color = Color(colorI)
SelectionContainer(
modifier = Modifier,
content = {
Expand All @@ -309,7 +316,7 @@ fun LyricsView(
available: Offset,
source: NestedScrollSource
): Offset {
if(textToolbar.status == TextToolbarStatus.Shown){
if (textToolbar.status == TextToolbarStatus.Shown) {
focusManager.clearFocus()
textToolbar.hide()
}
Expand Down Expand Up @@ -348,15 +355,14 @@ fun LyricsView(
false
}
.motionEventSpy {
if(it.action == MotionEvent.ACTION_DOWN&&textToolbar.status == TextToolbarStatus.Shown) {
if (it.action == MotionEvent.ACTION_DOWN && textToolbar.status == TextToolbarStatus.Shown) {
textToolbar.hide()
focusManager.clearFocus()
}
}
.onSizeChanged { sizeIt ->
size.value = sizeIt
}
.padding(start = 20.dp, end = 20.dp)
) {
items(musicViewModel.currentCaptionList.size) { listIndex ->
key(Unit) {
Expand Down Expand Up @@ -387,7 +393,7 @@ fun LyricsView(
text = annotatedString,
style = TextStyle(
color = if (currentI == listIndex && musicViewModel.autoHighLight.value) {
Color.Blue
MaterialTheme.colorScheme.onPrimary
} else {
MaterialTheme.colorScheme.onBackground
},
Expand All @@ -402,12 +408,22 @@ fun LyricsView(
),
modifier = Modifier
.fillMaxWidth()
.padding(2.dp)
.background(
if (currentI == listIndex && musicViewModel.autoHighLight.value) MaterialTheme.colorScheme.primaryContainer.copy(
alpha = 0.3f
) else MaterialTheme.colorScheme.background
)
.padding(
start = 20.dp,
end = 20.dp,
top = 2.dp,
bottom = 2.dp
)
) { offset ->
if(textToolbar.status == TextToolbarStatus.Shown) {
if (textToolbar.status == TextToolbarStatus.Shown) {
textToolbar.hide()
focusManager.clearFocus()
}else if (showMenu) {
} else if (showMenu) {
showMenu = false
} else {
val annotations =
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/ztftrue/music/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import androidx.core.view.WindowCompat
import androidx.media3.common.util.UnstableApi
import androidx.palette.graphics.Palette
import com.ztftrue.music.MusicViewModel
import com.ztftrue.music.utils.Utils
import com.ztftrue.music.utils.CustomColorUtils

/**
* “On” colors are primarily applied to text, iconography,
Expand Down Expand Up @@ -158,7 +158,7 @@ fun MusicPitchTheme(
val window = (view.context as Activity).window
window.statusBarColor = colorScheme.value.background.toArgb()
window.navigationBarColor = colorScheme.value.background.toArgb()
val darkColor = !Utils.isColorDark(colorScheme.value.background.toArgb())
val darkColor = !CustomColorUtils.isColorDark(colorScheme.value.background.toArgb())
WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkColor
WindowCompat.getInsetsController(window, view).isAppearanceLightNavigationBars =
darkColor
Expand Down
89 changes: 89 additions & 0 deletions app/src/main/java/com/ztftrue/music/utils/CustomColorUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.ztftrue.music.utils

import android.graphics.Color
import androidx.annotation.ColorInt
import androidx.annotation.FloatRange

object CustomColorUtils {
fun isColorDark(color: Int): Boolean {
// 计算相对亮度
// Color.luminance = (0.299 * R + 0.587 * G + 0.114 * B) / 255
val darkness: Double =
1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255
// 根据阈值判断是深色还是浅色
return darkness >= 0.5
}

fun getContrastingColor(color1: Int, color2: Int): Int {
val averageColor = averageColor(color1, color2)
return averageColor
}

fun averageColor(color1: Int, color2: Int): Int {
val red = ((Color.red(color1) + Color.red(color2)) / 2).toInt()
val green = (Color.green(color1) + Color.green(color2)) / 2.3
val blue = (Color.blue(color1) + Color.blue(color2)) / 2
return Color.rgb(red, green.toInt(), blue.toInt())
}

fun getContrastingColor(color: Int): Int {
val luminance =
(0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255

return if (luminance > 0.5) {
blendARGB(color, Color.BLACK, 0.7f)
} else {
blendARGB(color, Color.WHITE, 0.7f)
}
}

@ColorInt
fun blendARGB(
@ColorInt color1: Int, @ColorInt color2: Int,
@FloatRange(from = 0.0, to = 1.0) ratio: Float
): Int {
val inverseRatio = 1 - ratio
val a = Color.alpha(color1) * inverseRatio + Color.alpha(color2) * ratio
val r = Color.red(color1) * inverseRatio + Color.red(color2) * ratio
val g = Color.green(color1) * inverseRatio + Color.green(color2) * ratio
val b = Color.blue(color1) * inverseRatio + Color.blue(color2) * ratio
return Color.argb(a.toInt(), r.toInt(), g.toInt(), b.toInt())
}

fun alpha(color: Int): Int {
return color ushr 24
}

/**
* Return the red component of a color int. This is the same as saying
* (color >> 16) & 0xFF
*/
fun red(color: Int): Int {
return color shr 16 and 0xFF
}

/**
* Return the green component of a color int. This is the same as saying
* (color >> 8) & 0xFF
*/
fun green(color: Int): Int {
return color shr 8 and 0xFF
}

/**
* Return the blue component of a color int. This is the same as saying
* color & 0xFF
*/
fun blue(color: Int): Int {
return color and 0xFF
}

fun Int.toAndroidColor(): Int {
return Color.argb(
Color.alpha(this),
Color.red(this),
Color.green(this),
Color.blue(this)
)
}
}
8 changes: 0 additions & 8 deletions app/src/main/java/com/ztftrue/music/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.ztftrue.music.utils
import android.content.Context
import android.content.Intent
import android.content.pm.ResolveInfo
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.support.v4.media.MediaBrowserCompat
Expand Down Expand Up @@ -397,13 +396,6 @@ object Utils {
)
}

fun isColorDark(color: Int): Boolean {
// 计算相对亮度
val darkness: Double =
1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255
// 根据阈值判断是深色还是浅色
return darkness >= 0.5
}

fun getAllDictionaryActivity(context: Context): List<ResolveInfo> {
val shareIntent = Intent(Intent.ACTION_PROCESS_TEXT)
Expand Down
Loading

0 comments on commit bc501b3

Please sign in to comment.