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

Convert Android int colors with to-color expression #11506

Merged
merged 1 commit into from
Mar 23, 2018
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 @@ -135,7 +135,7 @@ public static Expression literal(@NonNull Object[] array) {
* @return the color expression
*/
public static Expression color(@ColorInt int color) {
return new ExpressionLiteral(new Color(color));
return toColor(literal(PropertyFactory.colorToRgbaString(color)));
}

/**
Expand Down Expand Up @@ -1735,9 +1735,7 @@ public Object[] toArray() {
*/
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();
Expand Down Expand Up @@ -1893,32 +1891,6 @@ public static class Interpolator extends 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import android.graphics.Color;

import com.mapbox.mapboxsdk.style.layers.PropertyFactory;

import org.junit.Test;

import java.util.Arrays;
Expand Down Expand Up @@ -113,7 +111,7 @@ public void testRgbaLiteral() throws Exception {

@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));
}
Expand Down Expand Up @@ -1087,6 +1085,14 @@ public void testLiteralPrimitiveArrayConversion() throws Exception {
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()));
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -33,6 +34,7 @@
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;
Expand Down Expand Up @@ -173,9 +175,9 @@ private void addExponentialZoomFunction() {
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))
)
)
);
Expand Down Expand Up @@ -460,7 +462,7 @@ private void addParksLayer() {
// 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)
)
Expand Down