Rework color selector and background.

fork-5.53.8
Alex Hart 2022-04-19 12:49:25 -03:00
rodzic 405d99fbe2
commit e2703b459f
3 zmienionych plików z 38 dodań i 21 usunięć

Wyświetl plik

@ -4,6 +4,7 @@ import android.graphics.Color
import android.os.Parcelable import android.os.Parcelable
import androidx.annotation.ColorInt import androidx.annotation.ColorInt
import androidx.annotation.IntRange import androidx.annotation.IntRange
import androidx.core.graphics.ColorUtils
import kotlinx.parcelize.IgnoredOnParcel import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize import kotlinx.parcelize.Parcelize
import org.thoughtcrime.securesms.conversation.colors.ChatColors import org.thoughtcrime.securesms.conversation.colors.ChatColors
@ -28,14 +29,27 @@ data class TextStoryPostCreationState(
val textForegroundColor: Int = when (textColorStyle) { val textForegroundColor: Int = when (textColorStyle) {
TextColorStyle.NO_BACKGROUND -> textColor TextColorStyle.NO_BACKGROUND -> textColor
TextColorStyle.NORMAL -> textColor TextColorStyle.NORMAL -> textColor
TextColorStyle.INVERT -> Color.WHITE TextColorStyle.INVERT -> getDefaultColorForLightness(textColor)
} }
@ColorInt @ColorInt
@IgnoredOnParcel @IgnoredOnParcel
val textBackgroundColor: Int = when (textColorStyle) { val textBackgroundColor: Int = when (textColorStyle) {
TextColorStyle.NO_BACKGROUND -> Color.TRANSPARENT TextColorStyle.NO_BACKGROUND -> Color.TRANSPARENT
TextColorStyle.NORMAL -> Color.WHITE TextColorStyle.NORMAL -> getDefaultColorForLightness(textColor)
TextColorStyle.INVERT -> textColor TextColorStyle.INVERT -> textColor
} }
private fun getDefaultColorForLightness(textColor: Int): Int {
val hsl = floatArrayOf(0f, 0f, 0f)
ColorUtils.colorToHSL(textColor, hsl)
val lightness = hsl[2]
return if (lightness >= 0.9f) {
Color.BLACK
} else {
Color.WHITE
}
}
} }

Wyświetl plik

@ -1,5 +1,6 @@
package org.thoughtcrime.securesms.scribbles package org.thoughtcrime.securesms.scribbles
import android.animation.FloatEvaluator
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.Color import android.graphics.Color
import android.graphics.ColorFilter import android.graphics.ColorFilter
@ -15,6 +16,7 @@ import androidx.annotation.ColorInt
import androidx.annotation.Dimension import androidx.annotation.Dimension
import androidx.appcompat.widget.AppCompatSeekBar import androidx.appcompat.widget.AppCompatSeekBar
import androidx.core.graphics.ColorUtils import androidx.core.graphics.ColorUtils
import org.thoughtcrime.securesms.scribbles.HSVColorSlider.toHue
import org.thoughtcrime.securesms.util.Util import org.thoughtcrime.securesms.util.Util
import org.thoughtcrime.securesms.util.ViewUtil import org.thoughtcrime.securesms.util.ViewUtil
import org.thoughtcrime.securesms.util.customizeOnDraw import org.thoughtcrime.securesms.util.customizeOnDraw
@ -30,16 +32,9 @@ object HSVColorSlider {
private const val WHITE_DIVISIONS = 125 private const val WHITE_DIVISIONS = 125
private const val MAX_SEEK_DIVISIONS = COLOR_DIVISIONS + BLACK_DIVISIONS + WHITE_DIVISIONS private const val MAX_SEEK_DIVISIONS = COLOR_DIVISIONS + BLACK_DIVISIONS + WHITE_DIVISIONS
private const val STANDARD_LIGHTNESS = 0.4f private const val STANDARD_LIGHTNESS = 0.4f
private val EVALUATOR = FloatEvaluator()
private val colors: IntArray = (0..COLOR_DIVISIONS).map { hue -> private val colors: IntArray = (0..BLACK_DIVISIONS).map { value ->
ColorUtils.HSLToColor(
floatArrayOf(
hue.toHue(COLOR_DIVISIONS),
1f,
calculateLightness(hue.toFloat(), STANDARD_LIGHTNESS)
)
)
}.toIntArray() + (BLACK_DIVISIONS downTo 0).map { value ->
ColorUtils.HSLToColor( ColorUtils.HSLToColor(
floatArrayOf( floatArrayOf(
MAX_HUE.toFloat(), MAX_HUE.toFloat(),
@ -47,12 +42,20 @@ object HSVColorSlider {
value / BLACK_DIVISIONS.toFloat() * STANDARD_LIGHTNESS value / BLACK_DIVISIONS.toFloat() * STANDARD_LIGHTNESS
) )
) )
}.toIntArray() + (0..COLOR_DIVISIONS).map { hue ->
ColorUtils.HSLToColor(
floatArrayOf(
hue.toHue(COLOR_DIVISIONS),
1f,
calculateLightness(hue.toFloat(), STANDARD_LIGHTNESS)
)
)
}.toIntArray() + (0..WHITE_DIVISIONS).map { value -> }.toIntArray() + (0..WHITE_DIVISIONS).map { value ->
ColorUtils.HSLToColor( ColorUtils.HSLToColor(
floatArrayOf( floatArrayOf(
MAX_HUE.toFloat(), COLOR_DIVISIONS.toHue(COLOR_DIVISIONS),
0f, 1f,
value / WHITE_DIVISIONS.toFloat() EVALUATOR.evaluate(value / WHITE_DIVISIONS.toFloat(), calculateLightness(COLOR_DIVISIONS.toFloat(), STANDARD_LIGHTNESS), 1f)
) )
) )
}.toIntArray() }.toIntArray()

Wyświetl plik

@ -60,7 +60,7 @@ class StoryTextPostView @JvmOverloads constructor(
textView.typeface = typeface textView.typeface = typeface
} }
fun setPostBackground(drawable: Drawable) { private fun setPostBackground(drawable: Drawable) {
backgroundView.setImageDrawable(drawable) backgroundView.setImageDrawable(drawable)
} }
@ -72,30 +72,30 @@ class StoryTextPostView @JvmOverloads constructor(
} }
} }
fun setText(text: CharSequence, isPlaceholder: Boolean) { private fun setText(text: CharSequence, isPlaceholder: Boolean) {
this.isPlaceholder = isPlaceholder this.isPlaceholder = isPlaceholder
textView.text = text textView.text = text
} }
fun setTextSize(@Px textSize: Float) { private fun setTextSize(@Px textSize: Float) {
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize) textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize)
} }
fun setTextGravity(textAlignment: TextAlignment) { private fun setTextGravity(textAlignment: TextAlignment) {
textView.gravity = textAlignment.gravity textView.gravity = textAlignment.gravity
} }
fun setTextScale(scalePercent: Int) { private fun setTextScale(scalePercent: Int) {
val scale = TextStoryScale.convertToScale(scalePercent) val scale = TextStoryScale.convertToScale(scalePercent)
textView.scaleX = scale textView.scaleX = scale
textView.scaleY = scale textView.scaleY = scale
} }
fun setTextVisible(visible: Boolean) { private fun setTextVisible(visible: Boolean) {
textView.visible = visible textView.visible = visible
} }
fun setTextBackgroundColor(@ColorInt color: Int) { private fun setTextBackgroundColor(@ColorInt color: Int) {
textView.setWrappedBackgroundColor(color) textView.setWrappedBackgroundColor(color)
} }