aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt
diff options
context:
space:
mode:
Diffstat (limited to 'library/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt')
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt11
1 files changed, 10 insertions, 1 deletions
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 8e396c7..c8fc90d 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt
@@ -7,6 +7,7 @@ import android.graphics.PorterDuff
import android.graphics.drawable.Drawable
import android.os.Build
import android.support.annotation.ColorInt
+import android.support.annotation.FloatRange
import android.support.annotation.IntRange
import android.support.v4.content.ContextCompat
import android.support.v4.graphics.drawable.DrawableCompat
@@ -20,12 +21,20 @@ import com.afollestad.materialdialogs.R
fun Int.isColorDark(): Boolean
= (0.299 * Color.red(this) + 0.587 * Color.green(this) + 0.114 * Color.blue(this)) / 255.0 < 0.5
-fun Int.toHexString(withAlpha: Boolean = false, withHexPrefix:Boolean = true): String {
+fun Int.toHexString(withAlpha: Boolean = false, withHexPrefix: Boolean = true): String {
val hex = if (withAlpha) String.format("#%08X", this)
else String.format("#%06X", 0xFFFFFF and this)
return if (withHexPrefix) hex else hex.substring(1)
}
+fun Int.toHSV(): FloatArray {
+ val hsv = FloatArray(3)
+ Color.colorToHSV(this, hsv)
+ return hsv
+}
+
+fun FloatArray.toColor():Int=Color.HSVToColor(this)
+
fun Int.isColorVisibleOn(@ColorInt color: Int, @IntRange(from = 0L, to = 255L) delta: Int = 8,
@IntRange(from = 0L, to = 255L) minAlpha: Int = 50): Boolean =
if (Color.alpha(this) < minAlpha) false