Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subtitles background opacity #4423

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.jellyfin.androidtv.data.compat.StreamInfo;
import org.jellyfin.androidtv.preference.UserPreferences;
import org.jellyfin.androidtv.preference.constant.ZoomMode;
import org.jellyfin.androidtv.util.ColorUtilsKt;
import org.jellyfin.sdk.api.client.ApiClient;
import org.jellyfin.sdk.model.api.MediaStream;
import org.jellyfin.sdk.model.api.MediaStreamType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jellyfin.androidtv.ui.preference.screen

import okhttp3.internal.toHexString
import org.jellyfin.androidtv.R
import org.jellyfin.androidtv.preference.UserPreferences
import org.jellyfin.androidtv.preference.UserSettingPreferences
Expand All @@ -16,9 +17,12 @@ import org.jellyfin.androidtv.ui.preference.dsl.enum
import org.jellyfin.androidtv.ui.preference.dsl.link
import org.jellyfin.androidtv.ui.preference.dsl.optionsScreen
import org.jellyfin.androidtv.ui.preference.dsl.seekbar
import org.jellyfin.androidtv.util.getOpacity
import org.jellyfin.androidtv.util.withOpacity
import org.jellyfin.preference.store.PreferenceStore
import org.jellyfin.sdk.model.api.MediaSegmentType
import org.koin.android.ext.android.inject
import timber.log.Timber
import kotlin.math.roundToInt

class PlaybackPreferencesScreen : OptionsFragment() {
Expand Down Expand Up @@ -124,7 +128,54 @@ class PlaybackPreferencesScreen : OptionsFragment() {
0xFFD60080L to context.getString(R.string.color_pink),
0xFF009FDAL to context.getString(R.string.color_cyan),
)
bind(userPreferences, UserPreferences.subtitlesBackgroundColor)

bind {
get { userPreferences[UserPreferences.subtitlesBackgroundColor].let {
if (it == 0x00FFFFFFL) {
it
} else {
it.withOpacity(1f)
}
}
}
set { value ->
userPreferences[UserPreferences.subtitlesBackgroundColor] = if (value == 0x00FFFFFFL) {
value
} else {
value.withOpacity(userPreferences[UserPreferences.subtitlesBackgroundColor].let { if (it == 0x00FFFFFFL) 0xFF000000L else it })
}
}
default { UserPreferences.subtitlesBackgroundColor.defaultValue }
}
}

@Suppress("MagicNumber")
seekbar {
setTitle(R.string.pref_subtitles_background_opacity)
min = 20
max = 100
increment = 10
valueFormatter = object : DurationSeekBarPreference.ValueFormatter() {
override fun display(value: Int): String = "$value%"
}

bind {
get { (userPreferences[UserPreferences.subtitlesBackgroundColor].let {
if (it == 0x00FFFFFFL) {
1.0f
} else {
it.getOpacity()
}
} * 100f).roundToInt() }
set { value ->
userPreferences[UserPreferences.subtitlesBackgroundColor] = userPreferences[UserPreferences.subtitlesBackgroundColor].withOpacity(value / 100f)
}
default { 100 }
}

depends {
userPreferences[UserPreferences.subtitlesBackgroundColor] != 0x00FFFFFFL
}
}

colorList {
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/java/org/jellyfin/androidtv/util/ColorUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.jellyfin.androidtv.util

import android.util.Log
import okhttp3.internal.toHexString
import timber.log.Timber
import kotlin.math.min
import kotlin.math.roundToLong

/**
* Adds an opacity to a Long color.
* @param opacity Opacity as a float in range 0..1
* @return `this` color with opacity
*/
fun Long.withOpacity(opacity: Float): Long {
if (opacity > 1f) {
throw IllegalArgumentException("Opacity must be lower or equal to 1")
}

val op = ((opacity * 255f).roundToLong() shl 24)

return ((this and 0x00FFFFFF) or op)
}

/**
* Extract opacity from a color and applies it to color `this`.
* @param color Color providing the opacity
* @return `this` with the opacity of `color`
*/
fun Long.withOpacity(color: Long): Long = this.withOpacity(min(color.getOpacity(), 0.2f))

fun Long.getOpacity(): Float = (this shr 24) / 255f
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@
<string name="segment_type_unknown">Unknown segments</string>
<string name="skip_forward_length">Skip forward length</string>
<string name="preference_enable_trickplay">Enable trickplay in video player</string>
<string name="pref_subtitles_background_opacity">Subtitle background opacity</string>
<plurals name="seconds">
<item quantity="one">%1$s second</item>
<item quantity="other">%1$s seconds</item>
Expand Down