Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Accept floating numbers during core->platform color conversion #14954

Merged
merged 1 commit into from
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,16 @@ public static void setTintList(@NonNull ImageView imageView, @ColorInt int tintC
*/
@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");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}