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

Return color string values in the [0-255] range #14233

Merged
merged 2 commits into from
May 22, 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 @@ -184,8 +184,7 @@ public void setTextColor(@ColorInt int textColor) {

void setTextColor(@NonNull Object textColor) {
// called from JNI
// because core is returning R, G and B components in range of 0 to 1, we need to convert them to be in 0 to 255.
setTextColor(ColorUtils.colorToRgbaString(ColorUtils.rgbaToColor((String) textColor)));
setTextColor((String) textColor);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.support.v4.widget.ImageViewCompat;
import android.util.TypedValue;
import android.widget.ImageView;

import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.exceptions.ConversionException;

Expand Down Expand Up @@ -111,28 +112,26 @@ public static void setTintList(@NonNull ImageView imageView, @ColorInt int tintC
ImageViewCompat.setImageTintList(imageView, getSelector(tintColor));
}

private static int normalizeColorComponent(String value) {
return (int) (Float.parseFloat(value) * 255);
}

/**
* Convert an rgba string to a Color int.
* <p>
* R, G, B color components have to be in the [0-255] range, while alpha has to be in the [0.0-1.0] range.
* For example: "rgba(255, 128, 0, 0.7)".
*
* @param value the String representation of rgba
* @return the int representation of rgba
* @throws ConversionException on illegal input
*/
@ColorInt
public static int rgbaToColor(@NonNull String value) {
Pattern c = Pattern.compile("rgba?\\s*\\(\\s*(\\d+\\.?\\d*)\\s*,\\s*(\\d+\\.?\\d*)\\s*,\\s*(\\d+\\.?\\d*)\\s*,"
+ "?\\s*(\\d+\\.?\\d*)?\\s*\\)");
Pattern c = Pattern.compile("rgba?\\s*\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,?\\s*(\\d+\\.?\\d*)?\\s*\\)");
Matcher m = c.matcher(value);
if (m.matches() && m.groupCount() == 3) {
return Color.rgb(normalizeColorComponent(m.group(1)), normalizeColorComponent(m.group(2)),
normalizeColorComponent(m.group(3)));
return Color.rgb(Integer.parseInt(m.group(1)), Integer.parseInt(m.group(2)),
Integer.parseInt(m.group(3)));
} else if (m.matches() && m.groupCount() == 4) {
return Color.argb(normalizeColorComponent(m.group(4)), normalizeColorComponent(m.group(1)),
normalizeColorComponent(m.group(2)), normalizeColorComponent(m.group(3)));
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)));
} else {
throw new ConversionException("Not a valid rgb/rgba value");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void testBackgroundColorAsConstant() {
assertNull(layer.getBackgroundColor().getValue());

// Set and Get
String propertyValue = "rgba(0, 0, 0, 1)";
String propertyValue = "rgba(255,128,0,0.7)";
layer.setProperties(backgroundColor(propertyValue));
assertEquals(layer.getBackgroundColor().getValue(), propertyValue);
}
Expand All @@ -85,8 +85,8 @@ public void testBackgroundColorAsIntConstant() {
assertNotNull(layer);

// Set and Get
layer.setProperties(backgroundColor(Color.RED));
assertEquals(layer.getBackgroundColorAsInt(), Color.RED);
layer.setProperties(backgroundColor(Color.argb(127, 255, 127, 0)));
assertEquals(layer.getBackgroundColorAsInt(), Color.argb(127, 255, 127, 0));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public void testCircleColorAsConstant() {
assertNull(layer.getCircleColor().getValue());

// Set and Get
String propertyValue = "rgba(0, 0, 0, 1)";
String propertyValue = "rgba(255,128,0,0.7)";
layer.setProperties(circleColor(propertyValue));
assertEquals(layer.getCircleColor().getValue(), propertyValue);
}
Expand All @@ -182,8 +182,8 @@ public void testCircleColorAsIntConstant() {
assertNotNull(layer);

// Set and Get
layer.setProperties(circleColor(Color.RED));
assertEquals(layer.getCircleColorAsInt(), Color.RED);
layer.setProperties(circleColor(Color.argb(127, 255, 127, 0)));
assertEquals(layer.getCircleColorAsInt(), Color.argb(127, 255, 127, 0));
}

@Test
Expand Down Expand Up @@ -384,7 +384,7 @@ public void testCircleStrokeColorAsConstant() {
assertNull(layer.getCircleStrokeColor().getValue());

// Set and Get
String propertyValue = "rgba(0, 0, 0, 1)";
String propertyValue = "rgba(255,128,0,0.7)";
layer.setProperties(circleStrokeColor(propertyValue));
assertEquals(layer.getCircleStrokeColor().getValue(), propertyValue);
}
Expand All @@ -409,8 +409,8 @@ public void testCircleStrokeColorAsIntConstant() {
assertNotNull(layer);

// Set and Get
layer.setProperties(circleStrokeColor(Color.RED));
assertEquals(layer.getCircleStrokeColorAsInt(), Color.RED);
layer.setProperties(circleStrokeColor(Color.argb(127, 255, 127, 0)));
assertEquals(layer.getCircleStrokeColorAsInt(), Color.argb(127, 255, 127, 0));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public void testConstFormatExpressionTextColorParam() {
);
assertNull(layer.getTextField().getExpression());
assertEquals(new Formatted(
new FormattedSection("test", null, null, "rgba(255, 255, 0, 1)")
new FormattedSection("test", null, null, "rgba(255,255,0,1)")
), layer.getTextField().getValue());
});
}
Expand All @@ -398,7 +398,7 @@ public void testConstFormatExpressionAllParams() {
"test",
formatFontScale(0.5),
formatTextFont(new String[] {"DIN Offc Pro Regular", "Arial Unicode MS Regular"}),
formatTextColor(rgb(126.7, 0, 0))
formatTextColor(rgb(126, 0, 0))
)
);
layer.setProperties(textField(expression));
Expand All @@ -412,7 +412,7 @@ public void testConstFormatExpressionAllParams() {
new FormattedSection("test",
0.5,
new String[] {"DIN Offc Pro Regular", "Arial Unicode MS Regular"},
"rgba(126, 0, 0, 1)")
"rgba(126,0,0,1)")
), layer.getTextField().getValue());
});
}
Expand All @@ -433,7 +433,8 @@ public void testConstFormatExpressionMultipleInputs() {
formatFontScale(1.5),
formatTextFont(new String[] {"DIN Offc Pro Regular", "Arial Unicode MS Regular"})
),
formatEntry("\ntest2", formatFontScale(2), formatTextColor(Color.BLUE))
formatEntry("\ntest2", formatFontScale(2), formatTextColor(Color.BLUE)),
formatEntry("\ntest3", formatFontScale(2.5), formatTextColor(toColor(literal("rgba(0, 128, 255, 0.5)"))))
);
layer.setProperties(textField(expression));
TestingAsyncUtils.INSTANCE.waitForLayer(uiController, mapView);
Expand All @@ -445,7 +446,8 @@ public void testConstFormatExpressionMultipleInputs() {
assertEquals(new Formatted(
new FormattedSection("test", 1.5,
new String[] {"DIN Offc Pro Regular", "Arial Unicode MS Regular"}),
new FormattedSection("\ntest2", 2.0, null, "rgba(0, 0, 255, 1)")
new FormattedSection("\ntest2", 2.0, null, "rgba(0,0,255,1)"),
new FormattedSection("\ntest3", 2.5, null, "rgba(0,128,255,0.5)")
), layer.getTextField().getValue());
});
}
Expand Down Expand Up @@ -547,9 +549,9 @@ public void testTextFieldFormattedArgument() {
Formatted formatted = new Formatted(
new FormattedSection("test", 1.5),
new FormattedSection("\ntest", 0.5, new String[] {"Arial Unicode MS Regular", "DIN Offc Pro Regular"}),
new FormattedSection("test", null, null, "rgba(0, 255, 0, 1)")
new FormattedSection("test", null, null, "rgba(0,255,0,1)")
);
layer.setProperties(textField(formatted), textColor("rgba(128, 0, 0, 1)"));
layer.setProperties(textField(formatted), textColor("rgba(128,0,0,1)"));
TestingAsyncUtils.INSTANCE.waitForLayer(uiController, mapView);

assertFalse(mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng), "layer")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void testFillExtrusionColorAsConstant() {
assertNull(layer.getFillExtrusionColor().getValue());

// Set and Get
String propertyValue = "rgba(0, 0, 0, 1)";
String propertyValue = "rgba(255,128,0,0.7)";
layer.setProperties(fillExtrusionColor(propertyValue));
assertEquals(layer.getFillExtrusionColor().getValue(), propertyValue);
}
Expand All @@ -169,8 +169,8 @@ public void testFillExtrusionColorAsIntConstant() {
assertNotNull(layer);

// Set and Get
layer.setProperties(fillExtrusionColor(Color.RED));
assertEquals(layer.getFillExtrusionColorAsInt(), Color.RED);
layer.setProperties(fillExtrusionColor(Color.argb(127, 255, 127, 0)));
assertEquals(layer.getFillExtrusionColorAsInt(), Color.argb(127, 255, 127, 0));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void testFillColorAsConstant() {
assertNull(layer.getFillColor().getValue());

// Set and Get
String propertyValue = "rgba(0, 0, 0, 1)";
String propertyValue = "rgba(255,128,0,0.7)";
layer.setProperties(fillColor(propertyValue));
assertEquals(layer.getFillColor().getValue(), propertyValue);
}
Expand All @@ -195,8 +195,8 @@ public void testFillColorAsIntConstant() {
assertNotNull(layer);

// Set and Get
layer.setProperties(fillColor(Color.RED));
assertEquals(layer.getFillColorAsInt(), Color.RED);
layer.setProperties(fillColor(Color.argb(127, 255, 127, 0)));
assertEquals(layer.getFillColorAsInt(), Color.argb(127, 255, 127, 0));
}

@Test
Expand All @@ -219,7 +219,7 @@ public void testFillOutlineColorAsConstant() {
assertNull(layer.getFillOutlineColor().getValue());

// Set and Get
String propertyValue = "rgba(0, 0, 0, 1)";
String propertyValue = "rgba(255,128,0,0.7)";
layer.setProperties(fillOutlineColor(propertyValue));
assertEquals(layer.getFillOutlineColor().getValue(), propertyValue);
}
Expand All @@ -244,8 +244,8 @@ public void testFillOutlineColorAsIntConstant() {
assertNotNull(layer);

// Set and Get
layer.setProperties(fillOutlineColor(Color.RED));
assertEquals(layer.getFillOutlineColorAsInt(), Color.RED);
layer.setProperties(fillOutlineColor(Color.argb(127, 255, 127, 0)));
assertEquals(layer.getFillOutlineColorAsInt(), Color.argb(127, 255, 127, 0));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void testHillshadeShadowColorAsConstant() {
assertNull(layer.getHillshadeShadowColor().getValue());

// Set and Get
String propertyValue = "rgba(0, 0, 0, 1)";
String propertyValue = "rgba(255,128,0,0.7)";
layer.setProperties(hillshadeShadowColor(propertyValue));
assertEquals(layer.getHillshadeShadowColor().getValue(), propertyValue);
}
Expand All @@ -145,8 +145,8 @@ public void testHillshadeShadowColorAsIntConstant() {
assertNotNull(layer);

// Set and Get
layer.setProperties(hillshadeShadowColor(Color.RED));
assertEquals(layer.getHillshadeShadowColorAsInt(), Color.RED);
layer.setProperties(hillshadeShadowColor(Color.argb(127, 255, 127, 0)));
assertEquals(layer.getHillshadeShadowColorAsInt(), Color.argb(127, 255, 127, 0));
}

@Test
Expand All @@ -169,7 +169,7 @@ public void testHillshadeHighlightColorAsConstant() {
assertNull(layer.getHillshadeHighlightColor().getValue());

// Set and Get
String propertyValue = "rgba(0, 0, 0, 1)";
String propertyValue = "rgba(255,128,0,0.7)";
layer.setProperties(hillshadeHighlightColor(propertyValue));
assertEquals(layer.getHillshadeHighlightColor().getValue(), propertyValue);
}
Expand All @@ -181,8 +181,8 @@ public void testHillshadeHighlightColorAsIntConstant() {
assertNotNull(layer);

// Set and Get
layer.setProperties(hillshadeHighlightColor(Color.RED));
assertEquals(layer.getHillshadeHighlightColorAsInt(), Color.RED);
layer.setProperties(hillshadeHighlightColor(Color.argb(127, 255, 127, 0)));
assertEquals(layer.getHillshadeHighlightColorAsInt(), Color.argb(127, 255, 127, 0));
}

@Test
Expand All @@ -205,7 +205,7 @@ public void testHillshadeAccentColorAsConstant() {
assertNull(layer.getHillshadeAccentColor().getValue());

// Set and Get
String propertyValue = "rgba(0, 0, 0, 1)";
String propertyValue = "rgba(255,128,0,0.7)";
layer.setProperties(hillshadeAccentColor(propertyValue));
assertEquals(layer.getHillshadeAccentColor().getValue(), propertyValue);
}
Expand All @@ -217,7 +217,7 @@ public void testHillshadeAccentColorAsIntConstant() {
assertNotNull(layer);

// Set and Get
layer.setProperties(hillshadeAccentColor(Color.RED));
assertEquals(layer.getHillshadeAccentColorAsInt(), Color.RED);
layer.setProperties(hillshadeAccentColor(Color.argb(127, 255, 127, 0)));
assertEquals(layer.getHillshadeAccentColorAsInt(), Color.argb(127, 255, 127, 0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ public void testColor() {
invoke(mapboxMap, (uiController, mapboxMap) -> {
assertNotNull(light);
// Set and Get
light.setColor("rgba(0, 0, 0, 1)");
assertEquals("Color should match", "rgba(0, 0, 0, 1)".replaceAll("\\s+", ""), light.getColor());
light.setColor("rgba(255,128,0,0.7)");
assertEquals("Color should match", "rgba(255,128,0,0.7)", light.getColor());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public void testLineColorAsConstant() {
assertNull(layer.getLineColor().getValue());

// Set and Get
String propertyValue = "rgba(0, 0, 0, 1)";
String propertyValue = "rgba(255,128,0,0.7)";
layer.setProperties(lineColor(propertyValue));
assertEquals(layer.getLineColor().getValue(), propertyValue);
}
Expand All @@ -247,8 +247,8 @@ public void testLineColorAsIntConstant() {
assertNotNull(layer);

// Set and Get
layer.setProperties(lineColor(Color.RED));
assertEquals(layer.getLineColorAsInt(), Color.RED);
layer.setProperties(lineColor(Color.argb(127, 255, 127, 0)));
assertEquals(layer.getLineColorAsInt(), Color.argb(127, 255, 127, 0));
}

@Test
Expand Down
Loading