diff options
author | Łukasz Paczos <lukas.paczos@gmail.com> | 2019-06-18 12:58:23 +0200 |
---|---|---|
committer | Łukasz Paczos <lukasz.paczos@mapbox.com> | 2019-06-18 13:22:44 +0200 |
commit | 55527d041231448945904cdb67bef0dd08d62110 (patch) | |
tree | 53280d103e0f40e8e7f9888f6fb16babeb8d62cd | |
parent | c3e94a06480339fe911386cd8be43bdf3e54a17d (diff) | |
download | qtlocation-mapboxgl-55527d041231448945904cdb67bef0dd08d62110.tar.gz |
[android] accept floating numbers during core->platform color conversion
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 |