Skip to content

Commit

Permalink
fix(android): color regex for rgba() (#13213)
Browse files Browse the repository at this point in the history
* fix(android): color regex for rgba()

* update regex
  • Loading branch information
m1ga authored Mar 21, 2022
1 parent 89685b7 commit 07b108a
Showing 1 changed file with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@
*/
package org.appcelerator.titanium.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.util.TypedValue;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* This class contain utility methods that converts a String color, like "red", into its corresponding RGB/RGBA representation.
*/
Expand All @@ -38,6 +39,9 @@ public class TiColorHelper
"rgba\\(\\s*([0-9]{1,3})\\s*,\\s*([0-9]{1,3})\\s*,\\s*([0-9]{1,3})\\s*,\\s*(\\d\\.\\d+)\\s*\\)");
static Pattern floatsPattern = Pattern.compile(
"rgba\\(\\s*(\\d\\.\\d+)\\s*,\\s*(\\d\\.\\d+)\\s*,\\s*(\\d\\.\\d+)\\s*,\\s*(\\d\\.\\d+)\\s*\\)");
static Pattern rgbafloatPattern = Pattern.compile(
"rgba\\((0|[1-9]\\d{0,2}),\\s*(0|[1-9]\\d{0,2}),\\s*(0|[1-9]\\d{0,2}),"
+ "\\s*(0|1|(0){0,1}\\.\\d{1,10}|1\\.0{1,10})\\s*\\)");
static Pattern rgbaPatternFallback =
Pattern.compile("rgba\\(\\s*([0-9]{1,3})\\s*,\\s*([0-9]{1,3})\\s*,\\s*([0-9]{1,3})\\s*\\)");

Expand Down Expand Up @@ -84,6 +88,11 @@ public static int parseColor(String value)
return Color.argb(Math.round(Float.valueOf(m.group(4)) * 255f), Integer.valueOf(m.group(1)),
Integer.valueOf(m.group(2)), Integer.valueOf(m.group(3)));
}
// rgba(int, int, int, float)
if ((m = rgbafloatPattern.matcher(lowval)).matches()) {
return Color.argb(Math.round(Float.valueOf(m.group(4)) * 255f), Integer.valueOf(m.group(1)),
Integer.valueOf(m.group(2)), Integer.valueOf(m.group(3)));
}
// rgba(int, int, int) with missing alpha value
if ((m = rgbaPatternFallback.matcher(lowval)).matches()) {
return Color.rgb(Integer.valueOf(m.group(1)), Integer.valueOf(m.group(2)), Integer.valueOf(m.group(3)));
Expand Down

0 comments on commit 07b108a

Please sign in to comment.