[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

@@ -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};
}
}
}