Update position of swatch when color is selected

This commit is contained in:
Moez Bhatti
2018-03-24 03:11:12 -04:00
parent 65a8a51c8a
commit e9232c639c
5 changed files with 36 additions and 21 deletions
@@ -19,25 +19,29 @@
package feature.themepicker package feature.themepicker
import android.content.Context import android.content.Context
import android.graphics.Color
import android.support.constraint.ConstraintLayout import android.support.constraint.ConstraintLayout
import android.support.v4.graphics.ColorUtils
import android.util.AttributeSet import android.util.AttributeSet
import android.view.MotionEvent import android.view.MotionEvent
import android.view.View import android.view.View
import com.moez.QKSMS.R import com.moez.QKSMS.R
import common.util.extensions.setBackgroundTint import common.util.extensions.setBackgroundTint
import common.util.extensions.within import common.util.extensions.within
import kotlinx.android.synthetic.main.hsl_picker_view.view.* import kotlinx.android.synthetic.main.hsv_picker_view.view.*
class HSLPickerView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : ConstraintLayout(context, attrs) { class HSVPickerView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : ConstraintLayout(context, attrs) {
private var swatchX = 0f private var min: Float = 0f
private var swatchY = 0f private var max = 0f
init { init {
View.inflate(context, R.layout.hsl_picker_view, this) View.inflate(context, R.layout.hsv_picker_view, this)
var swatchX = 0f
var swatchY = 0f
saturation.setOnTouchListener { _, event -> saturation.setOnTouchListener { _, event ->
setupBounds()
when (event.action) { when (event.action) {
MotionEvent.ACTION_DOWN -> { MotionEvent.ACTION_DOWN -> {
swatchX = event.x - event.rawX swatchX = event.x - event.rawX
@@ -45,9 +49,6 @@ class HSLPickerView @JvmOverloads constructor(context: Context, attrs: Attribute
} }
MotionEvent.ACTION_MOVE -> { MotionEvent.ACTION_MOVE -> {
val min = saturation.x - swatch.width / 2
val max = min + saturation.width
swatch.x = (event.rawX + swatchX + min).within(min, max) swatch.x = (event.rawX + swatchX + min).within(min, max)
swatch.y = (event.rawY + swatchY + min).within(min, max) swatch.y = (event.rawY + swatchY + min).within(min, max)
} }
@@ -58,16 +59,30 @@ class HSLPickerView @JvmOverloads constructor(context: Context, attrs: Attribute
} }
} }
fun setColor(color: Int) { private fun setupBounds() {
swatchPreview.setBackgroundTint(color) if (min == 0f || max == 0f) {
min = saturation.x - swatch.width / 2
val hsl = FloatArray(3).apply { max = min + saturation.width
ColorUtils.colorToHSL(color, this)
this[1] = 1f
this[2] = 0.5f
} }
}
saturation.setBackgroundTint(ColorUtils.HSLToColor(hsl)) fun setColor(color: Int) {
// Convert the rgb color to HSV
val hsv = FloatArray(3).apply { Color.colorToHSV(color, this) }
// Set the position of the swatch
setupBounds()
val range = max - min
swatch.x = range * hsv[1] + min
swatch.y = range * (1 - hsv[2]) + min
// Change the HSV saturation and lightness, for tinting the gradient
hsv[1] = 1f
hsv[2] = 1f
swatchPreview.setBackgroundTint(color)
saturation.setBackgroundTint(Color.HSVToColor(hsv))
} }
} }
@@ -26,7 +26,7 @@ import com.uber.autodispose.kotlin.autoDisposable
import common.base.QkThemedActivity import common.base.QkThemedActivity
import injection.appComponent import injection.appComponent
import kotlinx.android.synthetic.main.theme_picker_activity.* import kotlinx.android.synthetic.main.theme_picker_activity.*
import kotlinx.android.synthetic.main.theme_picker_hsl.* import kotlinx.android.synthetic.main.theme_picker_hsv.*
import javax.inject.Inject import javax.inject.Inject
@@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
tools:parentTag="feature.themepicker.HSLPickerView"> tools:parentTag="feature.themepicker.HSVPickerView">
<View <View
android:id="@+id/saturation" android:id="@+id/saturation"
@@ -50,7 +50,7 @@
<include <include
android:id="@+id/rgbPicker" android:id="@+id/rgbPicker"
layout="@layout/theme_picker_hsl" layout="@layout/theme_picker_hsv"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />
@@ -4,7 +4,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<feature.themepicker.HSLPickerView <feature.themepicker.HSVPickerView
android:id="@+id/picker" android:id="@+id/picker"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"