Use Android's Color.argb() and Color.rgb() methods in ColorParser
PiperOrigin-RevId: 305216138
This commit is contained in:
parent
8df6a4b301
commit
5a7dbae18c
@ -15,7 +15,9 @@
|
|||||||
*/
|
*/
|
||||||
package com.google.android.exoplayer2.util;
|
package com.google.android.exoplayer2.util;
|
||||||
|
|
||||||
|
import android.graphics.Color;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
import androidx.annotation.ColorInt;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
@ -49,6 +51,7 @@ public final class ColorParser {
|
|||||||
* @param colorExpression The color expression.
|
* @param colorExpression The color expression.
|
||||||
* @return The parsed ARGB color.
|
* @return The parsed ARGB color.
|
||||||
*/
|
*/
|
||||||
|
@ColorInt
|
||||||
public static int parseTtmlColor(String colorExpression) {
|
public static int parseTtmlColor(String colorExpression) {
|
||||||
return parseColorInternal(colorExpression, false);
|
return parseColorInternal(colorExpression, false);
|
||||||
}
|
}
|
||||||
@ -59,10 +62,12 @@ public final class ColorParser {
|
|||||||
* @param colorExpression The color expression.
|
* @param colorExpression The color expression.
|
||||||
* @return The parsed ARGB color.
|
* @return The parsed ARGB color.
|
||||||
*/
|
*/
|
||||||
|
@ColorInt
|
||||||
public static int parseCssColor(String colorExpression) {
|
public static int parseCssColor(String colorExpression) {
|
||||||
return parseColorInternal(colorExpression, true);
|
return parseColorInternal(colorExpression, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ColorInt
|
||||||
private static int parseColorInternal(String colorExpression, boolean alphaHasFloatFormat) {
|
private static int parseColorInternal(String colorExpression, boolean alphaHasFloatFormat) {
|
||||||
Assertions.checkArgument(!TextUtils.isEmpty(colorExpression));
|
Assertions.checkArgument(!TextUtils.isEmpty(colorExpression));
|
||||||
colorExpression = colorExpression.replace(" ", "");
|
colorExpression = colorExpression.replace(" ", "");
|
||||||
@ -83,7 +88,7 @@ public final class ColorParser {
|
|||||||
Matcher matcher = (alphaHasFloatFormat ? RGBA_PATTERN_FLOAT_ALPHA : RGBA_PATTERN_INT_ALPHA)
|
Matcher matcher = (alphaHasFloatFormat ? RGBA_PATTERN_FLOAT_ALPHA : RGBA_PATTERN_INT_ALPHA)
|
||||||
.matcher(colorExpression);
|
.matcher(colorExpression);
|
||||||
if (matcher.matches()) {
|
if (matcher.matches()) {
|
||||||
return argb(
|
return Color.argb(
|
||||||
alphaHasFloatFormat
|
alphaHasFloatFormat
|
||||||
? (int) (255 * Float.parseFloat(Assertions.checkNotNull(matcher.group(4))))
|
? (int) (255 * Float.parseFloat(Assertions.checkNotNull(matcher.group(4))))
|
||||||
: Integer.parseInt(Assertions.checkNotNull(matcher.group(4)), 10),
|
: Integer.parseInt(Assertions.checkNotNull(matcher.group(4)), 10),
|
||||||
@ -94,7 +99,7 @@ public final class ColorParser {
|
|||||||
} else if (colorExpression.startsWith(RGB)) {
|
} else if (colorExpression.startsWith(RGB)) {
|
||||||
Matcher matcher = RGB_PATTERN.matcher(colorExpression);
|
Matcher matcher = RGB_PATTERN.matcher(colorExpression);
|
||||||
if (matcher.matches()) {
|
if (matcher.matches()) {
|
||||||
return rgb(
|
return Color.rgb(
|
||||||
Integer.parseInt(Assertions.checkNotNull(matcher.group(1)), 10),
|
Integer.parseInt(Assertions.checkNotNull(matcher.group(1)), 10),
|
||||||
Integer.parseInt(Assertions.checkNotNull(matcher.group(2)), 10),
|
Integer.parseInt(Assertions.checkNotNull(matcher.group(2)), 10),
|
||||||
Integer.parseInt(Assertions.checkNotNull(matcher.group(3)), 10));
|
Integer.parseInt(Assertions.checkNotNull(matcher.group(3)), 10));
|
||||||
@ -109,14 +114,6 @@ public final class ColorParser {
|
|||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int argb(int alpha, int red, int green, int blue) {
|
|
||||||
return (alpha << 24) | (red << 16) | (green << 8) | blue;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int rgb(int red, int green, int blue) {
|
|
||||||
return argb(0xFF, red, green, blue);
|
|
||||||
}
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
COLOR_MAP = new HashMap<>();
|
COLOR_MAP = new HashMap<>();
|
||||||
COLOR_MAP.put("aliceblue", 0xFFF0F8FF);
|
COLOR_MAP.put("aliceblue", 0xFFF0F8FF);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user