From 3b92c13dc377cbc4e985c89536da661eb83e0e43 Mon Sep 17 00:00:00 2001 From: Dhaval Patel Date: Fri, 20 Nov 2020 23:24:19 +0530 Subject: [PATCH] Added Java Compatibility Modified the classes for easier access from Java class #8 --- .../colorpicker/ColorPickerDialog.kt | 16 +++++----- .../colorpicker/MaterialColorPickerDialog.kt | 31 ++++++++++++------- .../dhaval2404/colorpicker/util/ColorUtil.kt | 6 ++++ 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/colorpicker/src/main/kotlin/com/github/dhaval2404/colorpicker/ColorPickerDialog.kt b/colorpicker/src/main/kotlin/com/github/dhaval2404/colorpicker/ColorPickerDialog.kt index 01490fd..2c2d457 100644 --- a/colorpicker/src/main/kotlin/com/github/dhaval2404/colorpicker/ColorPickerDialog.kt +++ b/colorpicker/src/main/kotlin/com/github/dhaval2404/colorpicker/ColorPickerDialog.kt @@ -35,15 +35,13 @@ class ColorPickerDialog private constructor( var colorShape: ColorShape ) { - data class Builder( - val context: Context, - var title: String = context.getString(R.string.material_dialog_title), - var positiveButton: String = context.getString(R.string.material_dialog_positive_button), - var negativeButton: String = context.getString(R.string.material_dialog_negative_button), - var colorListener: ColorListener? = null, - var defaultColor: String? = null, - var colorShape: ColorShape = ColorShape.CIRCLE - ) { + class Builder(val context: Context) { + private var title: String = context.getString(R.string.material_dialog_title) + private var positiveButton: String = context.getString(R.string.material_dialog_positive_button) + private var negativeButton: String = context.getString(R.string.material_dialog_negative_button) + private var colorListener: ColorListener? = null + private var defaultColor: String? = null + private var colorShape: ColorShape = ColorShape.CIRCLE /** * Set Dialog Title diff --git a/colorpicker/src/main/kotlin/com/github/dhaval2404/colorpicker/MaterialColorPickerDialog.kt b/colorpicker/src/main/kotlin/com/github/dhaval2404/colorpicker/MaterialColorPickerDialog.kt index f67a27d..3787d2c 100644 --- a/colorpicker/src/main/kotlin/com/github/dhaval2404/colorpicker/MaterialColorPickerDialog.kt +++ b/colorpicker/src/main/kotlin/com/github/dhaval2404/colorpicker/MaterialColorPickerDialog.kt @@ -36,17 +36,16 @@ class MaterialColorPickerDialog private constructor( val colors: List? = null ) { - data class Builder( - val context: Context, - var title: String = context.getString(R.string.material_dialog_title), - var positiveButton: String = context.getString(R.string.material_dialog_positive_button), - var negativeButton: String = context.getString(R.string.material_dialog_negative_button), - var colorListener: ColorListener? = null, - var defaultColor: String? = null, - var colorSwatch: ColorSwatch = ColorSwatch._300, - var colorShape: ColorShape = ColorShape.CIRCLE, - var colors: List? = null - ) { + class Builder(val context: Context) { + + private var title: String = context.getString(R.string.material_dialog_title) + private var positiveButton: String = context.getString(R.string.material_dialog_positive_button) + private var negativeButton: String = context.getString(R.string.material_dialog_negative_button) + private var colorListener: ColorListener? = null + private var defaultColor: String? = null + private var colorSwatch: ColorSwatch = ColorSwatch._300 + private var colorShape: ColorShape = ColorShape.CIRCLE + private var colors: List? = null /** * Set Dialog Title @@ -185,6 +184,11 @@ class MaterialColorPickerDialog private constructor( return this } + fun setColors(colors: Array): Builder { + this.colors = colors.toList() + return this + } + /** * Provide PreDefined Colors, * @@ -198,6 +202,11 @@ class MaterialColorPickerDialog private constructor( return this } + fun setColorRes(colors: IntArray): Builder { + this.colors = colors.map { ColorUtil.formatColor(it) } + return this + } + /** * Creates an {@link MaterialColorPickerDialog} with the arguments supplied to this * builder. diff --git a/colorpicker/src/main/kotlin/com/github/dhaval2404/colorpicker/util/ColorUtil.kt b/colorpicker/src/main/kotlin/com/github/dhaval2404/colorpicker/util/ColorUtil.kt index 9542f91..c4aee25 100644 --- a/colorpicker/src/main/kotlin/com/github/dhaval2404/colorpicker/util/ColorUtil.kt +++ b/colorpicker/src/main/kotlin/com/github/dhaval2404/colorpicker/util/ColorUtil.kt @@ -51,25 +51,31 @@ object ColorUtil { return colorMap } + @JvmStatic fun parseColor(color: String): Int { return if (color.isBlank()) 0 else Color.parseColor(color) } + @JvmStatic fun formatColor(color: Int): String { return String.format("#%06x", color and 0xffffff) } + @JvmStatic fun isDarkColor(color: String) = isDarkColor(parseColor(color)) + @JvmStatic fun isDarkColor(color: Int): Boolean { return ColorUtils.calculateLuminance(color) <= 0.4 } + @JvmStatic fun isEqualColor(color1: String, color2: String, tolerance: Int = 50): Boolean { return isEqualColor(parseColor(color1), parseColor(color2), tolerance) } + @JvmStatic fun isEqualColor(color1: Int, color2: Int, tolerance: Int = 50): Boolean { val red1 = Color.red(color1) val green1 = Color.green(color1)