From 5431209bf04f1ea7a4d283893e0d8a4e796b0c90 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 12 Jun 2017 18:58:57 -0700 Subject: Add color to fg/bg --- .../main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerView.kt | 2 +- library/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerView.kt b/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerView.kt index 9d0c9c1..8c26645 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerView.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerView.kt @@ -32,7 +32,7 @@ internal class ColorPickerView @JvmOverloads constructor( var circleSize: Int = context.dimen(R.dimen.kau_color_circle_size).toInt() val backgroundColor = context.resolveColor(R.attr.md_background_color, if (context.resolveColor(android.R.attr.textColorPrimary).isColorDark()) Color.WHITE else 0xff424242.toInt()) - val backgroundColorTint = if (backgroundColor.isColorDark()) backgroundColor.lighten(0.2f) else backgroundColor.darken(0.2f) + val backgroundColorTint = backgroundColor.colorToForeground(0.2f) lateinit var dialog: MaterialDialog lateinit var builder: Builder lateinit var colorsTop: IntArray diff --git a/library/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt b/library/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt index 35681b0..0e835eb 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt @@ -70,6 +70,14 @@ fun Int.darken(@FloatRange(from = 0.0, to = 1.0) factor: Float = 0.2f): Int { return Color.argb(Color.alpha(this), red, green, blue) } +@ColorInt +fun Int.colorToBackground(@FloatRange(from = 0.0, to = 1.0) factor: Float = 0.2f): Int + = if (isColorDark()) darken(factor) else lighten(factor) + +@ColorInt +fun Int.colorToForeground(@FloatRange(from = 0.0, to = 1.0) factor: Float = 0.2f): Int + = if (isColorDark()) lighten(factor) else darken(factor) + @Throws(IllegalArgumentException::class) fun String.toColor(): Int { val toParse: String -- cgit v1.2.3