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

Commit

Permalink
[android] - range alpha between 0 and 1 with android color conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Jun 27, 2018
1 parent 192bee2 commit 2edb0d2
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 18 deletions.
1 change: 1 addition & 0 deletions platform/android/MapboxGLAndroidSDK/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ android {
testOptions {
unitTests {
returnDefaultValues true
includeAndroidResources = true
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public static Expression literal(@NonNull Object[] array) {
* @return the color expression
*/
public static Expression color(@ColorInt int color) {
int[] rgba = PropertyFactory.colorToRgbaArray(color);
float[] rgba = PropertyFactory.colorToRgbaArray(color);
return rgba(rgba[0], rgba[1], rgba[2], rgba[3]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.mapbox.mapboxsdk.style.expressions.Expression;

import java.text.DecimalFormat;
import java.util.Locale;

/**
Expand Down Expand Up @@ -2397,22 +2398,29 @@ public static PropertyValue<Expression> textOptional(Expression value) {

/**
* Converts Android color int to "rbga(r, g, b, a)" String equivalent.
* <p>
* Alpha value will be converted from 0-255 range to 0-1.
* </p>
*
* @param color Android color int
* @return String rgba color
*/
public static String colorToRgbaString(@ColorInt int color) {
return String.format(Locale.US, "rgba(%d, %d, %d, %d)",
(color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, (color >> 24) & 0xFF);
String alpha = new DecimalFormat("#.###").format(((float)((color >> 24) & 0xFF)) / 255.0f);
return String.format(Locale.US, "rgba(%d, %d, %d, %s)",
(color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, alpha);
}

/**
* Converts Android color int to rgba int array.
* Converts Android color int to rgba float array.
* <p>
* Alpha value will be converted from 0-255 range to 0-1.
* </p>
*
* @param color Android color int
* @return int rgba array
*/
public static int[] colorToRgbaArray(@ColorInt int color) {
return new int[] {(color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, (color >> 24) & 0xFF};
public static float[] colorToRgbaArray(@ColorInt int color) {
return new float[] {(color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, ((color >> 24) & 0xFF) / 255.0f};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.support.annotation.ColorInt;

import com.mapbox.mapboxsdk.style.expressions.Expression;

import java.text.DecimalFormat;
import java.util.Locale;

/**
Expand Down Expand Up @@ -87,22 +88,29 @@ public class PropertyFactory {
<% } -%>
/**
* Converts Android color int to "rbga(r, g, b, a)" String equivalent.
* <p>
* Alpha value will be converted from 0-255 range to 0-1.
* </p>
*
* @param color Android color int
* @return String rgba color
*/
public static String colorToRgbaString(@ColorInt int color) {
return String.format(Locale.US, "rgba(%d, %d, %d, %d)",
(color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, (color >> 24) & 0xFF);
String alpha = new DecimalFormat("#.###").format(((float)((color >> 24) & 0xFF)) / 255.0f);
return String.format(Locale.US, "rgba(%d, %d, %d, %s)",
(color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, alpha);
}

/**
* Converts Android color int to rgba int array.
* Converts Android color int to rgba float array.
* <p>
* Alpha value will be converted from 0-255 range to 0-1.
* </p>
*
* @param color Android color int
* @return int rgba array
*/
public static int[] colorToRgbaArray(@ColorInt int color) {
return new int[] {(color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, (color >> 24) & 0xFF};
public static float[] colorToRgbaArray(@ColorInt int color) {
return new float[] {(color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, ((color >> 24) & 0xFF) / 255.0f};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.graphics.Color;
import com.mapbox.mapboxsdk.style.layers.PropertyFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

import java.util.Arrays;

Expand Down Expand Up @@ -85,6 +87,7 @@
/**
* Expression unit tests that validate the expression output with the expected Object[]array representation.
*/
@RunWith(RobolectricTestRunner.class)
public class ExpressionTest {

@Test
Expand Down Expand Up @@ -117,7 +120,7 @@ public void testRgbaLiteral() throws Exception {

@Test
public void testToRgba() throws Exception {
Object[] expected = new Object[] {"to-rgba", new Object[] {"to-color", "rgba(255, 0, 0, 255)"}};
Object[] expected = new Object[] {"to-rgba", new Object[] {"to-color", "rgba(255, 0, 0, 1)"}};
Object[] actual = toRgba(toColor(literal(PropertyFactory.colorToRgbaString(Color.RED)))).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}
Expand All @@ -138,7 +141,7 @@ public void testEqLiteral() throws Exception {

@Test
public void testEqExpression() throws Exception {
Object[] expected = new Object[] {"==",new Object[]{"get", "hello"}, 1f};
Object[] expected = new Object[] {"==", new Object[] {"get", "hello"}, 1f};
Object[] actual = eq(get("hello"), 1).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}
Expand All @@ -159,7 +162,7 @@ public void testNeqLiteral() throws Exception {

@Test
public void testNeqExpression() throws Exception {
Object[] expected = new Object[] {"!=",new Object[]{"get", "hello"}, 1f};
Object[] expected = new Object[] {"!=", new Object[] {"get", "hello"}, 1f};
Object[] actual = neq(get("hello"), 1).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}
Expand Down Expand Up @@ -1140,7 +1143,7 @@ public void testLiteralPrimitiveArrayConversion() throws Exception {
@Test
public void testColorConversion() {
Expression greenColor = color(0xFF00FF00);
Object[] expected = new Object[] {"rgba", 0f, 255f, 0f, 255f};
Object[] expected = new Object[] {"rgba", 0f, 255f, 0f, 1f};
assertTrue("expression should match", Arrays.deepEquals(expected, greenColor.toArray()));
}

Expand Down Expand Up @@ -1250,7 +1253,7 @@ public void testRawAndroidColors() {
public void testRawRgbaColor() {
Expression expected = interpolate(
exponential(2f), zoom(),
literal(5f), literal("rgba(0, 0, 0, 255)"),
literal(5f), literal("rgba(0, 0, 0, 1)"),
literal(10.5f), literal("rgb(255, 0, 0)"),
literal(15), color(Color.GREEN),
literal(20), literal(PropertyFactory.colorToRgbaString(Color.BLUE)));
Expand All @@ -1272,4 +1275,19 @@ public void testRawMatchNumbers() {
stop("layer2", 2.7));
assertEquals("expressions should match", expected, raw(expected.toString()));
}

@Test
public void testAlphaValueInColorConversion() {
// regression test for #12198
Expression colorExpression = color(Color.parseColor("#41FF0000")); // 25.4% alpha red
Object[] result = colorExpression.toArray();
assertEquals("alpha value should match", 0.254f, (Float) result[4], 0.001f);
}

@Test
public void testAlphaValueInStringConversion() {
String color = PropertyFactory.colorToRgbaString(Color.parseColor("#41FF0000")).split(" ")[3];
String alpha = color.substring(0, color.length() - 1);
assertEquals("alpha value should match", 0.254f, Float.valueOf(alpha), 0.001f);
}
}

0 comments on commit 2edb0d2

Please sign in to comment.