[android] - correctly convert Android int colors with to-color expression (#11506)
This commit is contained in:
@@ -135,7 +135,7 @@ public class Expression {
|
||||
* @return the color expression
|
||||
*/
|
||||
public static Expression color(@ColorInt int color) {
|
||||
return new ExpressionLiteral(new Color(color));
|
||||
return toColor(literal(PropertyFactory.colorToRgbaString(color)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1735,9 +1735,7 @@ public class Expression {
|
||||
*/
|
||||
private Object toValue(ExpressionLiteral expressionValue) {
|
||||
Object value = expressionValue.toValue();
|
||||
if (value instanceof Expression.Color) {
|
||||
return ((Expression.Color) value).convertColor();
|
||||
} else if (value instanceof Expression.ExpressionLiteral) {
|
||||
if (value instanceof Expression.ExpressionLiteral) {
|
||||
return toValue((ExpressionLiteral) value);
|
||||
} else if (value instanceof Expression) {
|
||||
return ((Expression) value).toArray();
|
||||
@@ -1893,32 +1891,6 @@ public class Expression {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Expression color type.
|
||||
*/
|
||||
public static class Color {
|
||||
|
||||
private int color;
|
||||
|
||||
/**
|
||||
* Creates a color color type from a color int.
|
||||
*
|
||||
* @param color the int color
|
||||
*/
|
||||
public Color(@ColorInt int color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the int color to rgba(d, d, d, d) string representation
|
||||
*
|
||||
* @return the string representation of a color
|
||||
*/
|
||||
String convertColor() {
|
||||
return PropertyFactory.colorToRgbaString(color);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Expression array type.
|
||||
*/
|
||||
|
||||
@@ -2,8 +2,6 @@ package com.mapbox.mapboxsdk.style.expressions;
|
||||
|
||||
import android.graphics.Color;
|
||||
|
||||
import com.mapbox.mapboxsdk.style.layers.PropertyFactory;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -113,7 +111,7 @@ public class ExpressionTest {
|
||||
|
||||
@Test
|
||||
public void testToRgba() throws Exception {
|
||||
Object[] expected = new Object[] {"to-rgba", PropertyFactory.colorToRgbaString(Color.RED)};
|
||||
Object[] expected = new Object[] {"to-rgba", new Object[] {"to-color", "rgba(255, 0, 0, 255)"}};
|
||||
Object[] actual = toRgba(color(Color.RED)).toArray();
|
||||
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
|
||||
}
|
||||
@@ -1087,6 +1085,14 @@ public class ExpressionTest {
|
||||
float[] array = new float[] {0.2f, 0.5f};
|
||||
Object[] expected = new Object[] {"literal", new Object[] {0.2f, 0.5f}};
|
||||
Object[] actual = literal(array).toArray();
|
||||
assertEquals("primitive array should be convered", expected, actual);
|
||||
assertEquals("primitive array should be converted", expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testColorConversion() {
|
||||
Expression greenColor = color(0xFF00FF00);
|
||||
Object[] expected = new Object[] {"to-color", "rgba(0, 255, 0, 255)"};
|
||||
assertTrue("expression should match", Arrays.deepEquals(expected, greenColor.toArray()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.mapbox.mapboxsdk.testapp.activity.style;
|
||||
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.view.Menu;
|
||||
@@ -33,6 +34,7 @@ import static com.mapbox.mapboxsdk.style.expressions.Expression.rgba;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.step;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.stop;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.zoom;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.color;
|
||||
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillAntialias;
|
||||
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillColor;
|
||||
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillOpacity;
|
||||
@@ -173,9 +175,9 @@ public class DataDrivenStyleActivity extends AppCompatActivity {
|
||||
fillColor(
|
||||
interpolate(
|
||||
exponential(0.5f), zoom(),
|
||||
stop(1, rgb(255, 0, 0)),
|
||||
stop(5, rgb(0, 0, 255)),
|
||||
stop(10, rgb(0, 255, 0))
|
||||
stop(1, color(Color.RED)),
|
||||
stop(5, color(Color.BLUE)),
|
||||
stop(10, color(Color.GREEN))
|
||||
)
|
||||
)
|
||||
);
|
||||
@@ -460,7 +462,7 @@ public class DataDrivenStyleActivity extends AppCompatActivity {
|
||||
// Add a fill layer
|
||||
mapboxMap.addLayer(new FillLayer(AMSTERDAM_PARKS_LAYER, source.getId())
|
||||
.withProperties(
|
||||
fillColor(rgba(0.0f, 0.0f, 0.0f, 0.5f)),
|
||||
fillColor(color(Color.GREEN)),
|
||||
fillOutlineColor(rgb(0, 0, 255)),
|
||||
fillAntialias(true)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user