From e48d21718c3a0b0663fb5e2a7ca67a20853df584 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Fri, 23 Jun 2017 10:39:46 -0700 Subject: Rgba string --- .../kotlin/ca/allanwang/kau/utils/ColorUtils.kt | 2 ++ .../main/kotlin/ca/allanwang/kau/utils/Utils.kt | 11 ++++++++++ .../allanwang/kprefs/library/ExampleUnitTest.java | 17 --------------- .../ca/allanwang/kprefs/library/UtilsTest.kt | 25 ++++++++++++++++++++++ 4 files changed, 38 insertions(+), 17 deletions(-) delete mode 100644 library/src/test/java/ca/allanwang/kprefs/library/ExampleUnitTest.java create mode 100644 library/src/test/kotlin/ca/allanwang/kprefs/library/UtilsTest.kt (limited to 'library') 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 diff --git a/library/src/test/java/ca/allanwang/kprefs/library/ExampleUnitTest.java b/library/src/test/java/ca/allanwang/kprefs/library/ExampleUnitTest.java deleted file mode 100644 index 12e478b..0000000 --- a/library/src/test/java/ca/allanwang/kprefs/library/ExampleUnitTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package ca.allanwang.kprefs.library; - -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * Example local unit test, which will execute on the development machine (host). - * - * @see Testing documentation - */ -public class ExampleUnitTest { - @Test - public void addition_isCorrect() throws Exception { - assertEquals(4, 2 + 2); - } -} \ No newline at end of file diff --git a/library/src/test/kotlin/ca/allanwang/kprefs/library/UtilsTest.kt b/library/src/test/kotlin/ca/allanwang/kprefs/library/UtilsTest.kt new file mode 100644 index 0000000..c2c96e4 --- /dev/null +++ b/library/src/test/kotlin/ca/allanwang/kprefs/library/UtilsTest.kt @@ -0,0 +1,25 @@ +package ca.allanwang.kprefs.library + +import android.graphics.Color +import ca.allanwang.kau.utils.round +import ca.allanwang.kau.utils.toHexString +import org.junit.Test +import kotlin.test.assertEquals + +/** + * Created by Allan Wang on 2017-06-23. + */ +class UtilsTest { + + @Test + fun colorToHex() { + assertEquals("#ffffff", Color.WHITE.toHexString(withAlpha = false, withHexPrefix = true).toLowerCase()) + } + + @Test + fun rounding() { + assertEquals("1.23", 1.23456f.round(2)) + assertEquals("22.466", 22.465920439.round(3)) + assertEquals("22", 22f.round(3)) + } +} \ No newline at end of file -- cgit v1.2.3