[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()));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user