Skip to content

Commit

Permalink
Merge pull request #7955 from m1ga/rgba
Browse files Browse the repository at this point in the history
[TIMOB-6412] Android: rgba() fixes and addition for floats
  • Loading branch information
sriks committed Apr 22, 2016
2 parents f57dcc2 + 3bab59e commit e4a8d60
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
public class TiColorHelper
{
static Pattern shortHexPattern = Pattern.compile("#([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f]?)");
static Pattern rgbPattern = Pattern.compile("rgb\\(([0-9]{1,3}),([0-9]{1,3}),([0-9]{1,3})\\)");
static Pattern argbPattern = Pattern.compile("rgba\\(([0-9]{1,3}),([0-9]{1,3}),([0-9]{1,3}),([0-9]{1,3})\\)");
static Pattern rgbPattern = Pattern.compile("rgb\\(\\s*([0-9]{1,3})\\s*,\\s*([0-9]{1,3})\\s*,\\s*([0-9]{1,3})\\s*\\)");
static Pattern argbPattern = Pattern.compile("rgba\\(\\s*([0-9]{1,3})\\s*,\\s*([0-9]{1,3})\\s*,\\s*([0-9]{1,3})\\s*,\\s*([0-9]{1,3}[^\\.\\)])\\s*\\)");
static Pattern rgbaPattern = Pattern.compile("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*\\)");

private static final String TAG = "TiColorHelper";
private static HashMap<String, Integer> colorTable;
Expand Down Expand Up @@ -64,6 +66,20 @@ public static int parseColor(String value) {
Integer.valueOf(m.group(2)),
Integer.valueOf(m.group(3))
);
} else if ((m = rgbaPattern.matcher(lowval)).matches()) {
color = 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))
);
} else if ((m = floatsPattern.matcher(lowval)).matches()) {
color = Color.argb(
Math.round(Float.valueOf(m.group(4))*255f),
Math.round(Float.valueOf(m.group(1))*255f),
Math.round(Float.valueOf(m.group(2))*255f),
Math.round(Float.valueOf(m.group(3))*255f)
);
} else {
// Try the parser, will throw illegalArgument if it can't parse it.
try {
Expand Down

0 comments on commit e4a8d60

Please sign in to comment.