diff options
Diffstat (limited to 'colorpicker/src')
24 files changed, 189 insertions, 574 deletions
diff --git a/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/CircleView.kt b/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/CircleView.kt index 310fd53..e748677 100644 --- a/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/CircleView.kt +++ b/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/CircleView.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.colorpicker import android.animation.ValueAnimator @@ -13,15 +28,15 @@ import android.graphics.drawable.ShapeDrawable import android.graphics.drawable.StateListDrawable import android.graphics.drawable.shapes.OvalShape import android.os.Build -import android.support.annotation.ColorInt -import android.support.annotation.ColorRes -import android.support.annotation.FloatRange -import android.support.v4.view.GravityCompat -import android.support.v4.view.ViewCompat import android.util.AttributeSet import android.view.Gravity import android.widget.FrameLayout import android.widget.Toast +import androidx.annotation.ColorInt +import androidx.annotation.ColorRes +import androidx.annotation.FloatRange +import androidx.core.view.GravityCompat +import androidx.core.view.ViewCompat import ca.allanwang.kau.utils.getDip import ca.allanwang.kau.utils.setBackgroundColorRes import ca.allanwang.kau.utils.toColor @@ -33,7 +48,8 @@ import ca.allanwang.kau.utils.toHSV * An extension of MaterialDialog's CircleView with animation selection * [https://github.com/afollestad/material-dialogs/blob/master/commons/src/main/java/com/afollestad/materialdialogs/color/CircleView.java] */ -class CircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : FrameLayout(context, attrs, defStyleAttr) { +class CircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : + FrameLayout(context, attrs, defStyleAttr) { private val borderWidthMicro: Float = context.getDip(1f) private val borderWidthSmall: Float = context.getDip(3f) @@ -108,14 +124,14 @@ class CircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet fun animateSelected(selected: Boolean) { if (this.selected == selected) return this.selected = selected // We need to draw the other bands - val range = if (selected) Pair(-borderWidthSmall, borderWidthLarge) else Pair(borderWidthLarge, -borderWidthSmall) + val range = + if (selected) Pair(-borderWidthSmall, borderWidthLarge) else Pair(borderWidthLarge, -borderWidthSmall) ValueAnimator.ofFloat(range.first, range.second).apply { reverse() duration = 150L addUpdateListener { animation -> whiteOuterBound = animation.animatedValue as Float invalidate() - } start() } @@ -137,12 +153,22 @@ class CircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet if (whiteRadius >= centerWidth) { canvas.drawCircle(centerWidth, centerHeight, centerWidth, whitePaint) } else { - canvas.drawCircle(centerWidth, centerHeight, if (withBorder) centerWidth - borderWidthMicro else centerWidth, outerPaint) + canvas.drawCircle( + centerWidth, + centerHeight, + if (withBorder) centerWidth - borderWidthMicro else centerWidth, + outerPaint + ) canvas.drawCircle(centerWidth, centerHeight, whiteRadius, whitePaint) } canvas.drawCircle(centerWidth, centerHeight, innerRadius, innerPaint) } else { - canvas.drawCircle(centerWidth, centerHeight, if (withBorder) centerWidth - borderWidthMicro else centerWidth, innerPaint) + canvas.drawCircle( + centerWidth, + centerHeight, + if (withBorder) centerWidth - borderWidthMicro else centerWidth, + innerPaint + ) } } @@ -169,11 +195,13 @@ class CircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet referenceX = screenWidth - referenceX // mirror } val cheatSheet = Toast - .makeText(context, String.format("#%06X", 0xFFFFFF and color), Toast.LENGTH_SHORT) + .makeText(context, String.format("#%06X", 0xFFFFFF and color), Toast.LENGTH_SHORT) if (midy < displayFrame.height()) { // Show along the top; follow action buttons - cheatSheet.setGravity(Gravity.TOP or GravityCompat.END, referenceX, - screenPos[1] + height - displayFrame.top) + cheatSheet.setGravity( + Gravity.TOP or GravityCompat.END, referenceX, + screenPos[1] + height - displayFrame.top + ) } else { // Show along the bottom center cheatSheet.setGravity(Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL, 0, height) @@ -194,8 +222,10 @@ class CircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet } @ColorInt - fun shiftColor(@ColorInt color: Int, - @FloatRange(from = 0.0, to = 2.0) by: Float): Int { + fun shiftColor( + @ColorInt color: Int, + @FloatRange(from = 0.0, to = 2.0) by: Float + ): Int { if (by == 1f) return color val hsv = color.toHSV() hsv[2] *= by // value component diff --git a/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPalette.kt b/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPalette.kt index 68e3461..113020c 100644 --- a/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPalette.kt +++ b/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPalette.kt @@ -1,37 +1,56 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.colorpicker import android.graphics.Color /** * @author Aidan Follestad (afollestad) + * + * Modified by Allan Wang */ internal object ColorPalette { val PRIMARY_COLORS: IntArray by lazy { colorArrayOf( - "#F44336", - "#E91E63", - "#9C27B0", - "#673AB7", - "#3F51B5", - "#2196F3", - "#03A9F4", - "#00BCD4", - "#009688", - "#4CAF50", - "#8BC34A", - "#CDDC39", - "#FFEB3B", - "#FFC107", - "#FF9800", - "#FF5722", - "#795548", - "#9E9E9E", - "#607D8B") + "#F44336", + "#E91E63", + "#9C27B0", + "#673AB7", + "#3F51B5", + "#2196F3", + "#03A9F4", + "#00BCD4", + "#009688", + "#4CAF50", + "#8BC34A", + "#CDDC39", + "#FFEB3B", + "#FFC107", + "#FF9800", + "#FF5722", + "#795548", + "#9E9E9E", + "#607D8B" + ) } val PRIMARY_COLORS_SUB: Array<IntArray> by lazy { - arrayOf(colorArrayOf( + arrayOf( + colorArrayOf( "#FFEBEE", "#FFCDD2", "#EF9A9A", @@ -42,7 +61,7 @@ internal object ColorPalette { "#D32F2F", "#C62828", "#B71C1C" - ), colorArrayOf( + ), colorArrayOf( "#FCE4EC", "#F8BBD0", "#F48FB1", @@ -53,7 +72,7 @@ internal object ColorPalette { "#C2185B", "#AD1457", "#880E4F" - ), colorArrayOf( + ), colorArrayOf( "#F3E5F5", "#E1BEE7", "#CE93D8", @@ -64,7 +83,7 @@ internal object ColorPalette { "#7B1FA2", "#6A1B9A", "#4A148C" - ), colorArrayOf( + ), colorArrayOf( "#EDE7F6", "#D1C4E9", "#B39DDB", @@ -75,7 +94,7 @@ internal object ColorPalette { "#512DA8", "#4527A0", "#311B92" - ), colorArrayOf( + ), colorArrayOf( "#E8EAF6", "#C5CAE9", "#9FA8DA", @@ -86,7 +105,7 @@ internal object ColorPalette { "#303F9F", "#283593", "#1A237E" - ), colorArrayOf( + ), colorArrayOf( "#E3F2FD", "#BBDEFB", "#90CAF9", @@ -97,7 +116,7 @@ internal object ColorPalette { "#1976D2", "#1565C0", "#0D47A1" - ), colorArrayOf( + ), colorArrayOf( "#E1F5FE", "#B3E5FC", "#81D4FA", @@ -108,7 +127,7 @@ internal object ColorPalette { "#0288D1", "#0277BD", "#01579B" - ), colorArrayOf( + ), colorArrayOf( "#E0F7FA", "#B2EBF2", "#80DEEA", @@ -119,7 +138,7 @@ internal object ColorPalette { "#0097A7", "#00838F", "#006064" - ), colorArrayOf( + ), colorArrayOf( "#E0F2F1", "#B2DFDB", "#80CBC4", @@ -130,7 +149,7 @@ internal object ColorPalette { "#00796B", "#00695C", "#004D40" - ), colorArrayOf( + ), colorArrayOf( "#E8F5E9", "#C8E6C9", "#A5D6A7", @@ -141,7 +160,7 @@ internal object ColorPalette { "#388E3C", "#2E7D32", "#1B5E20" - ), colorArrayOf( + ), colorArrayOf( "#F1F8E9", "#DCEDC8", "#C5E1A5", @@ -152,7 +171,7 @@ internal object ColorPalette { "#689F38", "#558B2F", "#33691E" - ), colorArrayOf( + ), colorArrayOf( "#F9FBE7", "#F0F4C3", "#E6EE9C", @@ -163,7 +182,7 @@ internal object ColorPalette { "#AFB42B", "#9E9D24", "#827717" - ), colorArrayOf( + ), colorArrayOf( "#FFFDE7", "#FFF9C4", "#FFF59D", @@ -174,7 +193,7 @@ internal object ColorPalette { "#FBC02D", "#F9A825", "#F57F17" - ), colorArrayOf( + ), colorArrayOf( "#FFF8E1", "#FFECB3", "#FFE082", @@ -185,7 +204,7 @@ internal object ColorPalette { "#FFA000", "#FF8F00", "#FF6F00" - ), colorArrayOf( + ), colorArrayOf( "#FFF3E0", "#FFE0B2", "#FFCC80", @@ -196,7 +215,7 @@ internal object ColorPalette { "#F57C00", "#EF6C00", "#E65100" - ), colorArrayOf( + ), colorArrayOf( "#FBE9E7", "#FFCCBC", "#FFAB91", @@ -207,7 +226,7 @@ internal object ColorPalette { "#E64A19", "#D84315", "#BF360C" - ), colorArrayOf( + ), colorArrayOf( "#EFEBE9", "#D7CCC8", "#BCAAA4", @@ -218,7 +237,7 @@ internal object ColorPalette { "#5D4037", "#4E342E", "#3E2723" - ), colorArrayOf( + ), colorArrayOf( "#FAFAFA", "#F5F5F5", "#EEEEEE", @@ -229,7 +248,7 @@ internal object ColorPalette { "#616161", "#424242", "#212121" - ), colorArrayOf( + ), colorArrayOf( "#ECEFF1", "#CFD8DC", "#B0BEC5", @@ -239,111 +258,117 @@ internal object ColorPalette { "#546E7A", "#455A64", "#37474F", - "#263238")) + "#263238" + ) + ) } val ACCENT_COLORS: IntArray by lazy { colorArrayOf( - "#FF1744", - "#F50057", - "#D500F9", - "#651FFF", - "#3D5AFE", - "#2979FF", - "#00B0FF", - "#00E5FF", - "#1DE9B6", - "#00E676", - "#76FF03", - "#C6FF00", - "#FFEA00", - "#FFC400", - "#FF9100", - "#FF3D00") + "#FF1744", + "#F50057", + "#D500F9", + "#651FFF", + "#3D5AFE", + "#2979FF", + "#00B0FF", + "#00E5FF", + "#1DE9B6", + "#00E676", + "#76FF03", + "#C6FF00", + "#FFEA00", + "#FFC400", + "#FF9100", + "#FF3D00" + ) } val ACCENT_COLORS_SUB: Array<IntArray> by lazy { - arrayOf(colorArrayOf("#FF8A80", + arrayOf( + colorArrayOf( + "#FF8A80", "#FF5252", "#FF1744", "#D50000" - ), colorArrayOf( + ), colorArrayOf( "#FF80AB", "#FF4081", "#F50057", "#C51162" - ), colorArrayOf( + ), colorArrayOf( "#EA80FC", "#E040FB", "#D500F9", "#AA00FF" - ), colorArrayOf( + ), colorArrayOf( "#B388FF", "#7C4DFF", "#651FFF", "#6200EA" - ), colorArrayOf( + ), colorArrayOf( "#8C9EFF", "#536DFE", "#3D5AFE", "#304FFE" - ), colorArrayOf( + ), colorArrayOf( "#82B1FF", "#448AFF", "#2979FF", "#2962FF" - ), colorArrayOf( + ), colorArrayOf( "#80D8FF", "#40C4FF", "#00B0FF", "#0091EA" - ), colorArrayOf( + ), colorArrayOf( "#84FFFF", "#18FFFF", "#00E5FF", "#00B8D4" - ), colorArrayOf( + ), colorArrayOf( "#A7FFEB", "#64FFDA", "#1DE9B6", "#00BFA5" - ), colorArrayOf( + ), colorArrayOf( "#B9F6CA", "#69F0AE", "#00E676", "#00C853" - ), colorArrayOf( + ), colorArrayOf( "#CCFF90", "#B2FF59", "#76FF03", "#64DD17" - ), colorArrayOf( + ), colorArrayOf( "#F4FF81", "#EEFF41", "#C6FF00", "#AEEA00" - ), colorArrayOf( + ), colorArrayOf( "#FFFF8D", "#FFFF00", "#FFEA00", "#FFD600" - ), colorArrayOf( + ), colorArrayOf( "#FFE57F", "#FFD740", "#FFC400", "#FFAB00" - ), colorArrayOf( + ), colorArrayOf( "#FFD180", "#FFAB40", "#FF9100", "#FF6D00" - ), colorArrayOf( + ), colorArrayOf( "#FF9E80", "#FF6E40", "#FF3D00", - "#DD2C00")) + "#DD2C00" + ) + ) } private fun colorArrayOf(vararg colors: String) = colors.map { Color.parseColor(it) }.toIntArray() } - diff --git a/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPickerDialog.kt b/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPickerDialog.kt index b264e58..02a1fff 100644 --- a/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPickerDialog.kt +++ b/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPickerDialog.kt @@ -1,89 +1,66 @@ +/* + * Copyright 2018 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.colorpicker -import android.content.Context +import android.annotation.SuppressLint import android.graphics.Color -import android.support.annotation.DimenRes -import android.support.annotation.StringRes -import ca.allanwang.kau.utils.INVALID_ID -import ca.allanwang.kau.utils.string +import androidx.annotation.ColorInt import com.afollestad.materialdialogs.MaterialDialog -import com.afollestad.materialdialogs.Theme +import com.afollestad.materialdialogs.color.ColorCallback +import com.afollestad.materialdialogs.color.colorChooser + +sealed class ColorOptions(val colors: IntArray, val subColors: Array<IntArray>?) + +object PrimaryColors : ColorOptions(ColorPalette.PRIMARY_COLORS, ColorPalette.PRIMARY_COLORS_SUB) +object AccentColors : ColorOptions(ColorPalette.ACCENT_COLORS, ColorPalette.ACCENT_COLORS_SUB) +class CustomColors(colors: IntArray, subColors: Array<IntArray>? = null) : ColorOptions(colors, subColors) class ColorBuilder : ColorContract { - override var title: String? = null - override var titleRes: Int = INVALID_ID + override var colors: ColorOptions = PrimaryColors override var allowCustom: Boolean = true override var allowCustomAlpha: Boolean = false - override var isAccent: Boolean = false override var defaultColor: Int = Color.BLACK - override var doneText: Int = R.string.kau_done - override var backText: Int = R.string.kau_back - override var cancelText: Int = R.string.kau_cancel - override var presetText: Int = R.string.kau_md_presets - override var customText: Int = R.string.kau_custom - get() = if (allowCustom) field else 0 - override var dynamicButtonColors: Boolean = true - override var circleSizeRes: Int = R.dimen.kau_color_circle_size - override var colorCallback: ((selectedColor: Int) -> Unit)? = null - override var colorsTop: IntArray? = null - override var colorsSub: Array<IntArray>? = null - override var theme: Theme? = null + override var callback: ColorCallback = null } interface ColorContract { - var title: String? - @setparam:StringRes - var titleRes: Int + var colors: ColorOptions var allowCustom: Boolean var allowCustomAlpha: Boolean - var isAccent: Boolean - @setparam:StringRes + @setparam:ColorInt var defaultColor: Int - @setparam:StringRes - var doneText: Int - @setparam:StringRes - var backText: Int - @setparam:StringRes - var cancelText: Int - @setparam:StringRes - var presetText: Int - @setparam:StringRes - var customText: Int - var dynamicButtonColors: Boolean - @setparam:DimenRes - var circleSizeRes: Int - var colorCallback: ((selectedColor: Int) -> Unit)? - var colorsTop: IntArray? - var colorsSub: Array<IntArray>? - var theme: Theme? + var callback: ColorCallback } +@SuppressLint("CheckResult") +fun MaterialDialog.kauColorChooser(action: ColorContract.() -> Unit) = + kauColorChooser(ColorBuilder().apply(action)) + /** - * This is the extension that allows us to initialize the dialog - * Note that this returns just the dialog; you still need to call .show() to show it + * Thin wrapper that exposes color chooser options as [ColorContract] */ -fun Context.colorPickerDialog(action: ColorContract.() -> Unit): MaterialDialog { - val b = ColorBuilder() - b.action() - return colorPickerDialog(b) -} - -fun Context.colorPickerDialog(contract: ColorContract): MaterialDialog { - val view = ColorPickerView(this) - val dialog = with(MaterialDialog.Builder(this)) { - title(string(contract.titleRes, contract.title) ?: string(R.string.kau_md_color_palette)) - customView(view, false) - autoDismiss(false) - positiveText(contract.doneText) - negativeText(contract.cancelText) - if (contract.allowCustom) neutralText(contract.presetText) - onPositive { dialog, _ -> contract.colorCallback?.invoke(view.selectedColor); dialog.dismiss() } - onNegative { _, _ -> view.backOrCancel() } - if (contract.allowCustom) onNeutral { _, _ -> view.toggleCustom() } - showListener { view.refreshColors() } - if (contract.theme != null) theme(contract.theme!!) - build() - } - view.bind(contract, dialog) - return dialog +@SuppressLint("CheckResult") +fun MaterialDialog.kauColorChooser(c: ColorContract) { + colorChooser( + colors = c.colors.colors, + subColors = c.colors.subColors, + initialSelection = c.defaultColor, + allowCustomArgb = c.allowCustom, + showAlphaSelector = c.allowCustomAlpha, + selection = c.callback + ) + positiveButton(R.string.kau_done) }
\ No newline at end of file diff --git a/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPickerView.kt b/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPickerView.kt deleted file mode 100644 index b9cea99..0000000 --- a/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPickerView.kt +++ /dev/null @@ -1,319 +0,0 @@ -package ca.allanwang.kau.colorpicker - -import android.annotation.SuppressLint -import android.content.Context -import android.graphics.Color -import android.support.annotation.ColorInt -import android.support.v4.content.res.ResourcesCompat -import android.text.Editable -import android.text.InputFilter -import android.text.TextWatcher -import android.util.AttributeSet -import android.view.View -import android.view.ViewGroup -import android.widget.* -import ca.allanwang.kau.utils.* -import com.afollestad.materialdialogs.DialogAction -import com.afollestad.materialdialogs.MaterialDialog -import com.afollestad.materialdialogs.color.FillGridView -import java.util.* - -/** - * Created by Allan Wang on 2017-06-08. - * - * ColorPicker component of the ColorPickerDialog - */ -internal class ColorPickerView @JvmOverloads constructor( - context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 -) : ScrollView(context, attrs, defStyleAttr) { - val selectedColor: Int - get() = _selectedColor - private var _selectedColor: Int = -1 - private var isInSub: Boolean = false - private var isInCustom: Boolean = false - private var circleSize: Int = context.dimen(R.dimen.kau_color_circle_size).toInt() - @SuppressLint("PrivateResource") - private val backgroundColor = context.resolveColor(R.attr.md_background_color, - if (context.resolveColor(android.R.attr.textColorPrimary).isColorDark) Color.WHITE else 0xff424242.toInt()) - private val backgroundColorTint = backgroundColor.colorToForeground() - private lateinit var dialog: MaterialDialog - private lateinit var builder: ColorContract - private lateinit var colorsTop: IntArray - private var colorsSub: Array<IntArray>? = null - private var topIndex: Int = -1 - private var subIndex: Int = -1 - private var colorIndex: Int - get() = if (isInSub) subIndex else topIndex - set(value) { - if (isInSub) subIndex = value - else { - topIndex = value - if (colorsSub != null && colorsSub!!.size > value) { - dialog.setActionButton(DialogAction.NEGATIVE, builder.backText) - isInSub = true - invalidateGrid() - } - } - } - - private val gridView: FillGridView - private val customFrame: LinearLayout - private val customColorIndicator: View - private val hexInput: EditText - private val alphaLabel: TextView - private val alphaSeekbar: SeekBar - private val alphaValue: TextView - private val redSeekbar: SeekBar - private val redValue: TextView - private val greenSeekbar: SeekBar - private val greenValue: TextView - private val blueSeekbar: SeekBar - private val blueValue: TextView - - private var customHexTextWatcher: TextWatcher? = null - private var customRgbListener: SeekBar.OnSeekBarChangeListener? = null - - init { - //noinspection PrivateResource - View.inflate(context, R.layout.md_dialog_colorchooser, this) - gridView = findViewById(R.id.md_grid) - customFrame = findViewById(R.id.md_colorChooserCustomFrame) - customColorIndicator = findViewById(R.id.md_colorIndicator) - hexInput = findViewById(R.id.md_hexInput) - alphaLabel = findViewById(R.id.md_colorALabel) - alphaSeekbar = findViewById(R.id.md_colorA) - alphaValue = findViewById(R.id.md_colorAValue) - redSeekbar = findViewById(R.id.md_colorR) - redValue = findViewById(R.id.md_colorRValue) - greenSeekbar = findViewById(R.id.md_colorG) - greenValue = findViewById(R.id.md_colorGValue) - blueSeekbar = findViewById(R.id.md_colorB) - blueValue = findViewById(R.id.md_colorBValue) - } - - fun bind(builder: ColorContract, dialog: MaterialDialog) { - this.builder = builder - this.dialog = dialog - this.colorsTop = with(builder) { - when { - colorsTop != null -> colorsTop!! - isAccent -> ColorPalette.ACCENT_COLORS - else -> ColorPalette.PRIMARY_COLORS - } - } - this.colorsSub = with(builder) { - when { - colorsTop != null -> colorsSub - isAccent -> ColorPalette.ACCENT_COLORS_SUB - else -> ColorPalette.PRIMARY_COLORS_SUB - } - } - this._selectedColor = builder.defaultColor - if (builder.allowCustom) { - if (!builder.allowCustomAlpha) { - alphaLabel.gone() - alphaSeekbar.gone() - alphaValue.gone() - hexInput.hint = String.format("%06X", _selectedColor) - hexInput.filters = arrayOf(InputFilter.LengthFilter(6)) - } else { - hexInput.hint = String.format("%08X", _selectedColor) - hexInput.filters = arrayOf(InputFilter.LengthFilter(8)) - } - } - if (findColor(_selectedColor) || !builder.allowCustom) isInCustom = true // when toggled this will be false - toggleCustom() - } - - fun backOrCancel() { - if (isInSub) { - dialog.setActionButton(DialogAction.NEGATIVE, builder.cancelText) - //to top - isInSub = false - subIndex = -1 - invalidateGrid() - } else { - dialog.cancel() - } - } - - fun toggleCustom() { - isInCustom = !isInCustom - if (isInCustom) { - isInSub = false - if (builder.allowCustom) dialog.setActionButton(DialogAction.NEUTRAL, builder.presetText) - dialog.setActionButton(DialogAction.NEGATIVE, builder.cancelText) - customHexTextWatcher = object : TextWatcher { - override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} - - override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { - _selectedColor = try { - Color.parseColor("#$s") - } catch (e: IllegalArgumentException) { - Color.BLACK - } - customColorIndicator.setBackgroundColor(_selectedColor) - if (alphaSeekbar.isVisible) { - val alpha = Color.alpha(_selectedColor) - alphaSeekbar.progress = alpha - alphaValue.text = String.format(Locale.CANADA, "%d", alpha) - } - redSeekbar.progress = Color.red(_selectedColor) - greenSeekbar.progress = Color.green(_selectedColor) - blueSeekbar.progress = Color.blue(_selectedColor) - isInSub = false - topIndex = -1 - subIndex = -1 - refreshColors() - } - - override fun afterTextChanged(s: Editable?) {} - } - hexInput.setText(_selectedColor.toHexString(builder.allowCustomAlpha, false)) - hexInput.addTextChangedListener(customHexTextWatcher) - customRgbListener = object : SeekBar.OnSeekBarChangeListener { - override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) { - if (fromUser) { - val color = if (builder.allowCustomAlpha) - Color.argb(alphaSeekbar.progress, - redSeekbar.progress, - greenSeekbar.progress, - blueSeekbar.progress) - else Color.rgb(redSeekbar.progress, - greenSeekbar.progress, - blueSeekbar.progress) - - hexInput.setText(color.toHexString(builder.allowCustomAlpha, false)) - } - if (builder.allowCustomAlpha) alphaValue.text = alphaSeekbar.progress.toString() - redValue.text = redSeekbar.progress.toString() - greenValue.text = greenSeekbar.progress.toString() - blueValue.text = blueSeekbar.progress.toString() - } - - override fun onStartTrackingTouch(seekBar: SeekBar) = Unit - - override fun onStopTrackingTouch(seekBar: SeekBar) = Unit - } - redSeekbar.setOnSeekBarChangeListener(customRgbListener) - greenSeekbar.setOnSeekBarChangeListener(customRgbListener) - blueSeekbar.setOnSeekBarChangeListener(customRgbListener) - if (alphaSeekbar.isVisible) - alphaSeekbar.setOnSeekBarChangeListener(customRgbListener) - hexInput.setText(_selectedColor.toHexString(alphaSeekbar.isVisible, false)) - gridView.fadeOut(onFinish = { gridView.gone() }) - customFrame.fadeIn() - } else { - findColor(_selectedColor) - if (builder.allowCustom) dialog.setActionButton(DialogAction.NEUTRAL, builder.customText) - dialog.setActionButton(DialogAction.NEGATIVE, if (isInSub) builder.backText else builder.cancelText) - gridView.fadeIn(onStart = this::invalidateGrid) - customFrame.fadeOut(onFinish = { customFrame.gone() }) - hexInput.removeTextChangedListener(customHexTextWatcher) - customHexTextWatcher = null - alphaSeekbar.setOnSeekBarChangeListener(null) - redSeekbar.setOnSeekBarChangeListener(null) - greenSeekbar.setOnSeekBarChangeListener(null) - blueSeekbar.setOnSeekBarChangeListener(null) - customRgbListener = null - } - } - - fun refreshColors() { - if (!isInCustom) findColor(_selectedColor) - // Ensure that our tinted color is still visible against the background - val visibleColor = if (_selectedColor.isColorVisibleOn(backgroundColor)) _selectedColor else backgroundColorTint - if (builder.dynamicButtonColors) { - dialog.getActionButton(DialogAction.POSITIVE).setTextColor(visibleColor) - dialog.getActionButton(DialogAction.NEGATIVE).setTextColor(visibleColor) - dialog.getActionButton(DialogAction.NEUTRAL).setTextColor(visibleColor) - } - if (!builder.allowCustom || !isInCustom) return - if (builder.allowCustomAlpha) - alphaSeekbar.visible().tint(visibleColor) - redSeekbar.tint(visibleColor) - greenSeekbar.tint(visibleColor) - blueSeekbar.tint(visibleColor) - hexInput.tint(visibleColor) - } - - private fun findColor(@ColorInt color: Int): Boolean { - topIndex = -1 - subIndex = -1 - colorsTop.forEachIndexed { index, topColor -> - // First check for sub colors, then if the top color matches - if (findSubColor(color, index) || topColor == color) { - topIndex = index - return true - } - } - return false - } - - private fun findSubColor(@ColorInt color: Int, topIndex: Int): Boolean { - subIndex = colorsSub?.getOrNull(topIndex)?.indexOfFirst { color == it } ?: -1 - return subIndex != -1 - } - - private fun invalidateGrid() { - if (gridView.adapter == null) { - gridView.adapter = ColorGridAdapter() - gridView.selector = ResourcesCompat.getDrawable(resources, R.drawable.kau_transparent, null) - } else { - (gridView.adapter as BaseAdapter).notifyDataSetChanged() - } - } - - inner class ColorGridAdapter : BaseAdapter(), OnClickListener, OnLongClickListener { - override fun onClick(v: View) { - val (pos, color) = v.tagData ?: return - if (colorIndex == pos && isInSub) - return - circleAt(colorIndex)?.animateSelected(false) - _selectedColor = color - colorIndex = pos - refreshColors() - if (isInSub) - circleAt(colorIndex)?.animateSelected(true) - // Otherwise we are invalidating our grid, so there is no point in animating - } - - private fun circleAt(index: Int): CircleView? = - if (index == -1) null - else gridView.getChildAt(index) as? CircleView - - private val View.tagData: Pair<Int, Int>? - get() { - val tags = (tag as? String)?.split(":") ?: return null - val pos = tags[0].toIntOrNull() ?: return null - val color = tags[1].toIntOrNull() ?: return null - return pos to color - } - - override fun onLongClick(v: View): Boolean { - val (_, color) = v.tagData ?: return false - (v as? CircleView)?.showHint(color) ?: return false - return true - } - - override fun getItem(position: Int): Int = if (isInSub) colorsSub!![topIndex][position] else colorsTop[position] - - override fun getCount(): Int = if (isInSub) colorsSub!![topIndex].size else colorsTop.size - - override fun getItemId(position: Int): Long = position.toLong() - - override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { - val view: CircleView = convertView as? CircleView ?: CircleView(context).apply { - layoutParams = AbsListView.LayoutParams(circleSize, circleSize) - setOnClickListener(this@ColorGridAdapter) - setOnLongClickListener(this@ColorGridAdapter) - } - val color: Int = getItem(position) - return view.apply { - setBackgroundColor(color) - isSelected = colorIndex == position - tag = "$position:$color" - } - } - } -}
\ No newline at end of file diff --git a/colorpicker/src/main/res/values-da-rDK/strings_colorpicker.xml b/colorpicker/src/main/res/values-da-rDK/strings_colorpicker.xml deleted file mode 100644 index a5f7487..0000000 --- a/colorpicker/src/main/res/values-da-rDK/strings_colorpicker.xml +++ /dev/null @@ -1,5 +0,0 @@ -<?xml version="1.0" encoding="utf-8" standalone="no"?><!--Generated by crowdin.com--> -<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName"> - <string name="kau_md_color_palette">Farvepalette</string> - <string name="kau_md_presets">Forhåndsvalg</string> -</resources> diff --git a/colorpicker/src/main/res/values-de-rDE/strings_colorpicker.xml b/colorpicker/src/main/res/values-de-rDE/strings_colorpicker.xml deleted file mode 100644 index 5dd7b38..0000000 --- a/colorpicker/src/main/res/values-de-rDE/strings_colorpicker.xml +++ /dev/null @@ -1,5 +0,0 @@ -<?xml version="1.0" encoding="utf-8" standalone="no"?><!--Generated by crowdin.com--> -<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName"> - <string name="kau_md_color_palette">Farbpalette</string> - <string name="kau_md_presets">Vorlagen</string> -</resources> diff --git a/colorpicker/src/main/res/values-es-rES/strings_colorpicker.xml b/colorpicker/src/main/res/values-es-rES/strings_colorpicker.xml deleted file mode 100644 index 56148fd..0000000 --- a/colorpicker/src/main/res/values-es-rES/strings_colorpicker.xml +++ /dev/null @@ -1,5 +0,0 @@ -<?xml version="1.0" encoding="utf-8" standalone="no"?><!--Generated by crowdin.com--> -<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName"> - <string name="kau_md_color_palette">Paleta de colores</string> - <string name="kau_md_presets">Ajustes preestablecidos</string> -</resources> diff --git a/colorpicker/src/main/res/values-fr-rFR/strings_colorpicker.xml b/colorpicker/src/main/res/values-fr-rFR/strings_colorpicker.xml deleted file mode 100644 index a044e6e..0000000 --- a/colorpicker/src/main/res/values-fr-rFR/strings_colorpicker.xml +++ /dev/null @@ -1,5 +0,0 @@ -<?xml version="1.0" encoding="utf-8" standalone="no"?><!--Generated by crowdin.com--> -<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName"> - <string name="kau_md_color_palette">Palette de couleurs</string> - <string name="kau_md_presets">Réglages prédéfinis</string> -</resources> diff --git a/colorpicker/src/main/res/values-gl-rES/strings_colorpicker.xml b/colorpicker/src/main/res/values-gl-rES/strings_colorpicker.xml deleted file mode 100644 index fbc1d90..0000000 --- a/colorpicker/src/main/res/values-gl-rES/strings_colorpicker.xml +++ /dev/null @@ -1,5 +0,0 @@ -<?xml version="1.0" encoding="utf-8" standalone="no"?><!--Generated by crowdin.com--> -<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName"> - <string name="kau_md_color_palette">Paleta de cores</string> - <string name="kau_md_presets">Axustes predefinidos</string> -</resources> diff --git a/colorpicker/src/main/res/values-hu-rHU/strings_colorpicker.xml b/colorpicker/src/main/res/values-hu-rHU/strings_colorpicker.xml deleted file mode 100644 index 42f3700..0000000 --- a/colorpicker/src/main/res/values-hu-rHU/strings_colorpicker.xml +++ /dev/null @@ -1,5 +0,0 @@ -<?xml version="1.0" encoding="utf-8" standalone="no"?><!--Generated by crowdin.com--> -<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName"> - <string name="kau_md_color_palette">Színpaletta</string> - <string name="kau_md_presets">Sablonok</string> -</resources> diff --git a/colorpicker/src/main/res/values-in-rID/strings_colorpicker.xml b/colorpicker/src/main/res/values-in-rID/strings_colorpicker.xml deleted file mode 100644 index 52e59bc..0000000 --- a/colorpicker/src/main/res/values-in-rID/strings_colorpicker.xml +++ /dev/null @@ -1,5 +0,0 @@ -<?xml version="1.0" encoding="utf-8" standalone="no"?><!--Generated by crowdin.com--> -<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName"> - <string name="kau_md_color_palette">Palet warna</string> - <string name="kau_md_presets">Preset</string> -</resources> diff --git a/colorpicker/src/main/res/values-it-rIT/strings_colorpicker.xml b/colorpicker/src/main/res/values-it-rIT/strings_colorpicker.xml deleted file mode 100644 index 4f3e282..0000000 --- a/colorpicker/src/main/res/values-it-rIT/strings_colorpicker.xml +++ /dev/null @@ -1,5 +0,0 @@ -<?xml version="1.0" encoding="utf-8" standalone="no"?><!--Generated by crowdin.com--> -<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName"> - <string name="kau_md_color_palette">Tavolozza dei colori</string> - <string name="kau_md_presets">Preimpostazioni</string> -</resources> diff --git a/colorpicker/src/main/res/values-ko-rKR/strings_colorpicker.xml b/colorpicker/src/main/res/values-ko-rKR/strings_colorpicker.xml deleted file mode 100644 index 7c65db1..0000000 --- a/colorpicker/src/main/res/values-ko-rKR/strings_colorpicker.xml +++ /dev/null @@ -1,5 +0,0 @@ -<?xml version="1.0" encoding="utf-8" standalone="no"?><!--Generated by crowdin.com--> -<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName"> - <string name="kau_md_color_palette">색상 팔레트</string> - <string name="kau_md_presets">사전 설정</string> -</resources> diff --git a/colorpicker/src/main/res/values-no-rNO/strings_colorpicker.xml b/colorpicker/src/main/res/values-no-rNO/strings_colorpicker.xml deleted file mode 100644 index d79a6d8..0000000 --- a/colorpicker/src/main/res/values-no-rNO/strings_colorpicker.xml +++ /dev/null @@ -1,5 +0,0 @@ -<?xml version="1.0" encoding="utf-8" standalone="no"?><!--Generated by crowdin.com--> -<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName"> - <string name="kau_md_color_palette">Fargepalett</string> - <string name="kau_md_presets">Forhåndsinnstillinger</string> -</resources> diff --git a/colorpicker/src/main/res/values-pl-rPL/strings_colorpicker.xml b/colorpicker/src/main/res/values-pl-rPL/strings_colorpicker.xml deleted file mode 100644 index 42cb6e0..0000000 --- a/colorpicker/src/main/res/values-pl-rPL/strings_colorpicker.xml +++ /dev/null @@ -1,5 +0,0 @@ -<?xml version="1.0" encoding="utf-8" standalone="no"?><!--Generated by crowdin.com--> -<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName"> - <string name="kau_md_color_palette">Paleta kolorów</string> - <string name="kau_md_presets">Zdefiniowane</string> -</resources> diff --git a/colorpicker/src/main/res/values-pt-rBR/strings_colorpicker.xml b/colorpicker/src/main/res/values-pt-rBR/strings_colorpicker.xml deleted file mode 100644 index 0694ed4..0000000 --- a/colorpicker/src/main/res/values-pt-rBR/strings_colorpicker.xml +++ /dev/null @@ -1,5 +0,0 @@ -<?xml version="1.0" encoding="utf-8" standalone="no"?><!--Generated by crowdin.com--> -<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName"> - <string name="kau_md_color_palette">Paleta de Cores</string> - <string name="kau_md_presets">Predefinições</string> -</resources> diff --git a/colorpicker/src/main/res/values-sv-rSE/strings_colorpicker.xml b/colorpicker/src/main/res/values-sv-rSE/strings_colorpicker.xml deleted file mode 100644 index 6b06ba9..0000000 --- a/colorpicker/src/main/res/values-sv-rSE/strings_colorpicker.xml +++ /dev/null @@ -1,5 +0,0 @@ -<?xml version="1.0" encoding="utf-8" standalone="no"?><!--Generated by crowdin.com--> -<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName"> - <string name="kau_md_color_palette">Färgpalett</string> - <string name="kau_md_presets">Förval</string> -</resources> diff --git a/colorpicker/src/main/res/values-th-rTH/strings_colorpicker.xml b/colorpicker/src/main/res/values-th-rTH/strings_colorpicker.xml deleted file mode 100644 index 7fdf610..0000000 --- a/colorpicker/src/main/res/values-th-rTH/strings_colorpicker.xml +++ /dev/null @@ -1,5 +0,0 @@ -<?xml version="1.0" encoding="utf-8" standalone="no"?><!--Generated by crowdin.com--> -<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName"> - <string name="kau_md_color_palette">ชุดสี</string> - <string name="kau_md_presets">ค่าที่ตั้งไว้</string> -</resources> diff --git a/colorpicker/src/main/res/values-tr-rTR/strings_colorpicker.xml b/colorpicker/src/main/res/values-tr-rTR/strings_colorpicker.xml deleted file mode 100644 index eddc8ad..0000000 --- a/colorpicker/src/main/res/values-tr-rTR/strings_colorpicker.xml +++ /dev/null @@ -1,5 +0,0 @@ -<?xml version="1.0" encoding="utf-8" standalone="no"?><!--Generated by crowdin.com--> -<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName"> - <string name="kau_md_color_palette">Renk paleti</string> - <string name="kau_md_presets">Hazır ayarlar</string> -</resources> diff --git a/colorpicker/src/main/res/values-uk-rUA/strings_colorpicker.xml b/colorpicker/src/main/res/values-uk-rUA/strings_colorpicker.xml deleted file mode 100644 index d114ba5..0000000 --- a/colorpicker/src/main/res/values-uk-rUA/strings_colorpicker.xml +++ /dev/null @@ -1,5 +0,0 @@ -<?xml version="1.0" encoding="utf-8" standalone="no"?><!--Generated by crowdin.com--> -<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName"> - <string name="kau_md_color_palette">Кольорова палітра</string> - <string name="kau_md_presets">Пресети</string> -</resources> diff --git a/colorpicker/src/main/res/values-vi-rVN/strings_colorpicker.xml b/colorpicker/src/main/res/values-vi-rVN/strings_colorpicker.xml deleted file mode 100644 index f14a044..0000000 --- a/colorpicker/src/main/res/values-vi-rVN/strings_colorpicker.xml +++ /dev/null @@ -1,5 +0,0 @@ -<?xml version="1.0" encoding="utf-8" standalone="no"?><!--Generated by crowdin.com--> -<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName"> - <string name="kau_md_color_palette">Bảng màu</string> - <string name="kau_md_presets">Cài đặt sẵn</string> -</resources> diff --git a/colorpicker/src/main/res/values-zh-rCN/strings_colorpicker.xml b/colorpicker/src/main/res/values-zh-rCN/strings_colorpicker.xml deleted file mode 100644 index 9b117f5..0000000 --- a/colorpicker/src/main/res/values-zh-rCN/strings_colorpicker.xml +++ /dev/null @@ -1,5 +0,0 @@ -<?xml version="1.0" encoding="utf-8" standalone="no"?><!--Generated by crowdin.com--> -<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName"> - <string name="kau_md_color_palette">调色板</string> - <string name="kau_md_presets">预设</string> -</resources> diff --git a/colorpicker/src/main/res/values/dimens.xml b/colorpicker/src/main/res/values/dimens.xml deleted file mode 100644 index 193940e..0000000 --- a/colorpicker/src/main/res/values/dimens.xml +++ /dev/null @@ -1,3 +0,0 @@ -<resources> - <dimen name="kau_color_circle_size">56dp</dimen> -</resources> diff --git a/colorpicker/src/main/res/values/strings_colorpicker.xml b/colorpicker/src/main/res/values/strings_colorpicker.xml deleted file mode 100644 index a470390..0000000 --- a/colorpicker/src/main/res/values/strings_colorpicker.xml +++ /dev/null @@ -1,5 +0,0 @@ -<?xml version="1.0" encoding="UTF-8" standalone="no"?> -<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName"> - <string name="kau_md_color_palette">Color Palette</string> - <string name="kau_md_presets">Presets</string> -</resources> |