aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt
diff options
context:
space:
mode:
authorJahir Fiquitiva <jahir.fiquitiva@gmail.com>2017-07-10 01:22:52 -0500
committerAllan Wang <me@allanwang.ca>2017-07-09 23:22:52 -0700
commit3028d35c24da883dad34dbc4f6eb36d1df1838fa (patch)
tree3bf16d3f0e98c570731469a6969cc73b403efc3c /core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt
parentf1660aab8a25c93aebdb7993e4bfbc3bb7e65ee5 (diff)
downloadkau-3028d35c24da883dad34dbc4f6eb36d1df1838fa.tar.gz
kau-3028d35c24da883dad34dbc4f6eb36d1df1838fa.tar.bz2
kau-3028d35c24da883dad34dbc4f6eb36d1df1838fa.zip
Added some extensions (#4)
* Add a couple extra extensions. * Add more extensions * Make oneline fun * Remove duplicated line from modules.xml * Revert kprefsingle and fix minor docs (cherry picked from commit 76d0de9) * Clean up ColorUtils * Clean up ActivityUtils * Clean up DrawableUtils * Remove MenuUtils * Clean PackageUtils * Clean up ViewUtils * Clean up ViewUtils 2
Diffstat (limited to 'core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt17
1 files changed, 15 insertions, 2 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt
index 84a7c06..81bf0d9 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt
@@ -19,8 +19,11 @@ import com.afollestad.materialdialogs.R
/**
* Created by Allan Wang on 2017-06-08.
*/
-val Int.isColorDark: Boolean
- get() = (0.299 * Color.red(this) + 0.587 * Color.green(this) + 0.114 * Color.blue(this)) / 255.0 < 0.5
+inline val Int.isColorDark: Boolean
+ get() = isColorDark(0.5f)
+
+fun Int.isColorDark(minDarkness: Float): Boolean =
+ ((0.299 * Color.red(this) + 0.587 * Color.green(this) + 0.114 * Color.blue(this)) / 255.0) < minDarkness
fun Int.toHexString(withAlpha: Boolean = false, withHexPrefix: Boolean = true): String {
val hex = if (withAlpha) String.format("#%08X", this)
@@ -66,6 +69,16 @@ val Int.isColorTransparent: Boolean
get() = Color.alpha(this) != 255
@ColorInt
+fun Int.blendWith(@ColorInt color: Int, @FloatRange(from = 0.0, to = 1.0) ratio: Float): Int {
+ val inverseRatio = 1f - ratio
+ val a = Color.alpha(this) * inverseRatio + Color.alpha(color) * ratio
+ val r = Color.red(this) * inverseRatio + Color.red(color) * ratio
+ val g = Color.green(this) * inverseRatio + Color.green(color) * ratio
+ val b = Color.blue(this) * inverseRatio + Color.blue(color) * ratio
+ return Color.argb(a.toInt(), r.toInt(), g.toInt(), b.toInt())
+}
+
+@ColorInt
fun Int.withAlpha(@IntRange(from = 0L, to = 255L) alpha: Int): Int
= Color.argb(alpha, Color.red(this), Color.green(this), Color.blue(this))