aboutsummaryrefslogtreecommitdiff
path: root/library/src/main
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-06-23 10:39:46 -0700
committerAllan Wang <me@allanwang.ca>2017-06-23 10:39:46 -0700
commite48d21718c3a0b0663fb5e2a7ca67a20853df584 (patch)
treeab4dd19c754f07904643f512e494be3556a4b354 /library/src/main
parentff17cd5daac40418e6e1bc89e9f65bd0fc8ea6af (diff)
downloadkau-e48d21718c3a0b0663fb5e2a7ca67a20853df584.tar.gz
kau-e48d21718c3a0b0663fb5e2a7ca67a20853df584.tar.bz2
kau-e48d21718c3a0b0663fb5e2a7ca67a20853df584.zip
Rgba string
Diffstat (limited to 'library/src/main')
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt2
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt11
2 files changed, 13 insertions, 0 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 8ae23d1..3499647 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt
@@ -28,6 +28,8 @@ fun Int.toHexString(withAlpha: Boolean = false, withHexPrefix: Boolean = true):
return if (withHexPrefix) hex else hex.substring(1)
}
+fun Int.toRgbaString() :String = "rgba(${Color.red(this)}, ${Color.green(this)}, ${Color.blue(this)}, ${(Color.alpha(this)/255f).round(3)})"
+
fun Int.toHSV(): FloatArray {
val hsv = FloatArray(3)
Color.colorToHSV(this, hsv)
diff --git a/library/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt b/library/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt
index d3db05e..1ba54e0 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt
@@ -5,8 +5,11 @@ import android.content.pm.PackageManager
import android.content.res.Resources
import android.os.Build
import android.os.Looper
+import android.support.annotation.IntRange
import ca.allanwang.kau.R
import ca.allanwang.kau.logging.KL
+import java.math.RoundingMode
+import java.text.DecimalFormat
/**
@@ -61,4 +64,12 @@ fun Context.minuteToText(minutes: Long): String = with(minutes) {
else if (this % 1440L == 0L) String.format(string(R.string.kau_x_days), this / 1440L)
else if (this % 60L == 0L) String.format(string(R.string.kau_x_hours), this / 60L)
else String.format(string(R.string.kau_x_minutes), this)
+}
+
+fun Number.round(@IntRange(from = 1L) decimalCount: Int): String {
+ val expression = StringBuilder().append("#.")
+ (1..decimalCount).forEach { expression.append("#") }
+ val formatter = DecimalFormat(expression.toString())
+ formatter.roundingMode = RoundingMode.HALF_UP
+ return formatter.format(this)
} \ No newline at end of file