[android] - range alpha between 0 and 1 with android color conversion

This commit is contained in:
tobrun
2018-06-25 13:36:57 +02:00
committed by Tobrun
parent 192bee256b
commit abca28c66b
5 changed files with 53 additions and 18 deletions

View File

@@ -124,6 +124,7 @@ android {
testOptions {
unitTests {
returnDefaultValues true
includeAndroidResources = true
}
}

View File

@@ -213,7 +213,7 @@ public class Expression {
* @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]);
}

View File

@@ -6,6 +6,7 @@ import android.support.annotation.ColorInt;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import java.text.DecimalFormat;
import java.util.Locale;
/**
@@ -2397,22 +2398,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};
}
}
}

View File

@@ -10,6 +10,7 @@ import android.support.annotation.ColorInt;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import java.text.DecimalFormat;
import java.util.Locale;
/**
@@ -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};
}
}
}

View File

@@ -3,6 +3,8 @@ package com.mapbox.mapboxsdk.style.expressions;
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;
@@ -85,6 +87,7 @@ import static org.junit.Assert.assertEquals;
/**
* Expression unit tests that validate the expression output with the expected Object[]array representation.
*/
@RunWith(RobolectricTestRunner.class)
public class ExpressionTest {
@Test
@@ -117,7 +120,7 @@ public class ExpressionTest {
@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));
}
@@ -138,7 +141,7 @@ public class ExpressionTest {
@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));
}
@@ -159,7 +162,7 @@ public class ExpressionTest {
@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));
}
@@ -1140,7 +1143,7 @@ public class ExpressionTest {
@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()));
}
@@ -1250,7 +1253,7 @@ public class ExpressionTest {
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)));
@@ -1272,4 +1275,19 @@ public class ExpressionTest {
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);
}
}