summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorŁukasz Paczos <lukas.paczos@gmail.com>2019-06-18 12:58:23 +0200
committerŁukasz Paczos <lukasz.paczos@mapbox.com>2019-06-18 15:41:37 +0200
commit06807735eb29b63db842bd64bb8d6002aa6985dc (patch)
tree8b2a5596464d0dd8674414d5a723b9769cdda418
parenta51e59e28b9c44df1effa606b28de771d3add77e (diff)
downloadqtlocation-mapboxgl-06807735eb29b63db842bd64bb8d6002aa6985dc.tar.gz
[android] accept floating numbers during core->platform color conversion
(cherry picked from commit 55527d041231448945904cdb67bef0dd08d62110)
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/ColorUtils.java12
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/utils/ColorUtilsTest.kt22
2 files changed, 29 insertions, 5 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/ColorUtils.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/ColorUtils.java
index ab3d68547e..8d741d1bdc 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/ColorUtils.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/ColorUtils.java
@@ -124,14 +124,16 @@ public class ColorUtils {
*/
@ColorInt
public static int rgbaToColor(@NonNull String value) {
- Pattern c = Pattern.compile("rgba?\\s*\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,?\\s*(\\d+\\.?\\d*)?\\s*\\)");
+ // we need to accept and floor float values as well, as those can come from core
+ Pattern c = Pattern.compile("rgba?\\s*\\(\\s*(\\d+\\.?\\d*)\\s*,\\s*(\\d+\\.?\\d*)\\s*,\\s*(\\d+\\.?\\d*)\\s*,"
+ + "?\\s*(\\d+\\.?\\d*)?\\s*\\)");
Matcher m = c.matcher(value);
if (m.matches() && m.groupCount() == 3) {
- return Color.rgb(Integer.parseInt(m.group(1)), Integer.parseInt(m.group(2)),
- Integer.parseInt(m.group(3)));
+ return Color.rgb((int) Float.parseFloat(m.group(1)), (int) Float.parseFloat(m.group(2)),
+ (int) Float.parseFloat(m.group(3)));
} else if (m.matches() && m.groupCount() == 4) {
- return Color.argb((int) (Float.parseFloat(m.group(4)) * 255), Integer.parseInt(m.group(1)),
- Integer.parseInt(m.group(2)), Integer.parseInt(m.group(3)));
+ return Color.argb((int) (Float.parseFloat(m.group(4)) * 255), (int) Float.parseFloat(m.group(1)),
+ (int) Float.parseFloat(m.group(2)), (int) Float.parseFloat(m.group(3)));
} else {
throw new ConversionException("Not a valid rgb/rgba value");
}
diff --git a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/utils/ColorUtilsTest.kt b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/utils/ColorUtilsTest.kt
new file mode 100644
index 0000000000..530a08a4e5
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/utils/ColorUtilsTest.kt
@@ -0,0 +1,22 @@
+package com.mapbox.mapboxsdk.utils
+
+import android.graphics.Color
+import junit.framework.Assert
+import org.junit.Test
+
+class ColorUtilsTest {
+
+ @Test
+ fun rgbaToColor_decimalComponent() {
+ val input = "rgba(255,128.0000952303,0,0.7)"
+ val result = ColorUtils.rgbaToColor(input)
+ Assert.assertEquals(Color.argb(255, 128, 0, (0.7 * 255).toInt()), result)
+ }
+
+ @Test
+ fun rgbaToColor_decimalComponent_floor() {
+ val input = "rgba(255,128.70123,0,0.7)"
+ val result = ColorUtils.rgbaToColor(input)
+ Assert.assertEquals(Color.argb(255, 128, 0, (0.7 * 255).toInt()), result)
+ }
+} \ No newline at end of file