From 908c9bc6247462c4a18d605bbabc3ebb6520774d Mon Sep 17 00:00:00 2001 From: arkon Date: Fri, 30 Apr 2021 22:35:49 -0400 Subject: [PATCH] Reader grayscale filter (closes #2822) --- .../data/preference/PreferenceKeys.kt | 2 + .../data/preference/PreferencesHelper.kt | 2 + .../tachiyomi/ui/reader/ReaderActivity.kt | 23 +++ .../setting/ReaderColorFilterSettings.kt | 20 +- .../layout/reader_color_filter_settings.xml | 190 +++++++++--------- app/src/main/res/values/strings.xml | 1 + 6 files changed, 131 insertions(+), 107 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt index abf35341a..c30c4da48 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt @@ -49,6 +49,8 @@ object PreferenceKeys { const val colorFilterMode = "color_filter_mode" + const val grayscale = "pref_grayscale" + const val defaultReadingMode = "pref_default_reading_mode_key" const val defaultOrientationType = "pref_default_orientation_type_key" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index 91ccd83f0..5340ae67c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -121,6 +121,8 @@ class PreferencesHelper(val context: Context) { fun colorFilterMode() = flowPrefs.getInt(Keys.colorFilterMode, 0) + fun grayscale() = flowPrefs.getBoolean(Keys.grayscale, false) + fun defaultReadingMode() = prefs.getInt(Keys.defaultReadingMode, ReadingModeType.RIGHT_TO_LEFT.flagValue) fun defaultOrientationType() = prefs.getInt(Keys.defaultOrientationType, OrientationType.FREE.flagValue) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt index 8f7461bbc..1adb6bd9e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt @@ -8,12 +8,16 @@ import android.content.Context import android.content.Intent import android.graphics.Bitmap import android.graphics.Color +import android.graphics.ColorMatrix +import android.graphics.ColorMatrixColorFilter +import android.graphics.Paint import android.os.Build import android.os.Bundle import android.view.KeyEvent import android.view.Menu import android.view.MenuItem import android.view.MotionEvent +import android.view.View.LAYER_TYPE_HARDWARE import android.view.WindowManager import android.view.animation.Animation import android.view.animation.AnimationUtils @@ -789,6 +793,16 @@ class ReaderActivity : BaseRxActivity() */ private inner class ReaderConfig { + private val grayscalePaint by lazy { + Paint().apply { + colorFilter = ColorMatrixColorFilter( + ColorMatrix().apply { + setSaturation(0f) + } + ) + } + } + /** * Initializes the reader subscriptions. */ @@ -827,6 +841,10 @@ class ReaderActivity : BaseRxActivity() preferences.colorFilterMode().asFlow() .onEach { setColorFilter(preferences.colorFilter().get()) } .launchIn(lifecycleScope) + + preferences.grayscale().asFlow() + .onEach { setGrayscale(it) } + .launchIn(lifecycleScope) } /** @@ -934,5 +952,10 @@ class ReaderActivity : BaseRxActivity() binding.colorOverlay.isVisible = true binding.colorOverlay.setFilterColor(value, preferences.colorFilterMode().get()) } + + private fun setGrayscale(enabled: Boolean) { + val paint = if (enabled) grayscalePaint else null + binding.viewerContainer.setLayerType(LAYER_TYPE_HARDWARE, paint) + } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderColorFilterSettings.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderColorFilterSettings.kt index 1a71fed83..2985cdd3b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderColorFilterSettings.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderColorFilterSettings.kt @@ -14,7 +14,7 @@ import androidx.lifecycle.lifecycleScope import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.databinding.ReaderColorFilterSettingsBinding import eu.kanade.tachiyomi.ui.reader.ReaderActivity -import eu.kanade.tachiyomi.widget.IgnoreFirstSpinnerListener +import eu.kanade.tachiyomi.util.preference.bindToPreference import eu.kanade.tachiyomi.widget.SimpleSeekBarListener import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach @@ -63,20 +63,10 @@ class ReaderColorFilterSettings @JvmOverloads constructor(context: Context, attr binding.seekbarColorFilterBlue.progress = argb[3] // Set listeners - binding.switchColorFilter.isChecked = preferences.colorFilter().get() - binding.switchColorFilter.setOnCheckedChangeListener { _, isChecked -> - preferences.colorFilter().set(isChecked) - } - - binding.customBrightness.isChecked = preferences.customBrightness().get() - binding.customBrightness.setOnCheckedChangeListener { _, isChecked -> - preferences.customBrightness().set(isChecked) - } - - binding.colorFilterMode.onItemSelectedListener = IgnoreFirstSpinnerListener { position -> - preferences.colorFilterMode().set(position) - } - binding.colorFilterMode.setSelection(preferences.colorFilterMode().get(), false) + binding.switchColorFilter.bindToPreference(preferences.colorFilter()) + binding.customBrightness.bindToPreference(preferences.customBrightness()) + binding.colorFilterMode.bindToPreference(preferences.colorFilterMode()) + binding.grayscale.bindToPreference(preferences.grayscale()) binding.seekbarColorFilterAlpha.setOnSeekBarChangeListener( object : SimpleSeekBarListener() { diff --git a/app/src/main/res/layout/reader_color_filter_settings.xml b/app/src/main/res/layout/reader_color_filter_settings.xml index 80899cf9b..9701574f7 100644 --- a/app/src/main/res/layout/reader_color_filter_settings.xml +++ b/app/src/main/res/layout/reader_color_filter_settings.xml @@ -3,8 +3,7 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" - android:layout_height="wrap_content" - android:padding="16dp"> + android:layout_height="wrap_content"> + + - - + app:layout_constraintTop_toTopOf="@id/seekbar_color_filter_red" + tools:text="255" /> + + - - + app:layout_constraintTop_toTopOf="@id/seekbar_color_filter_green" + tools:text="255" /> + + - - + app:layout_constraintTop_toTopOf="@id/seekbar_color_filter_blue" + tools:text="255" /> + + - - + app:layout_constraintTop_toTopOf="@id/seekbar_color_filter_alpha" + tools:text="255" /> - - - + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/seekbar_color_filter_alpha" + app:title="@string/pref_color_filter_mode" /> + + + + @@ -185,11 +200,25 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" + android:paddingEnd="16dp" + android:paddingStart="16dp" android:text="@string/pref_custom_brightness" - app:layout_constraintTop_toBottomOf="@id/color_filter_mode_text" /> + app:layout_constraintTop_toBottomOf="@id/grayscale" /> + + - - + app:layout_constraintTop_toTopOf="@id/brightness_seekbar" + tools:text="50" /> - - - - diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c2d861a80..bbce163f9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -271,6 +271,7 @@ Reduces banding, but impacts performance Crop borders Custom brightness + Grayscale Custom color filter Color filter blend mode Default