Skip to content

Commit

Permalink
Added Java Compatibility
Browse files Browse the repository at this point in the history
Modified the classes for easier access from Java class

#8
  • Loading branch information
Dhaval2404 committed Nov 20, 2020
1 parent 48819b6 commit 3b92c13
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,16 @@ class MaterialColorPickerDialog private constructor(
val colors: List<String>? = 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<String>? = 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<String>? = null

/**
* Set Dialog Title
Expand Down Expand Up @@ -185,6 +184,11 @@ class MaterialColorPickerDialog private constructor(
return this
}

fun setColors(colors: Array<String>): Builder {
this.colors = colors.toList()
return this
}

/**
* Provide PreDefined Colors,
*
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3b92c13

Please sign in to comment.