MCPcopy Create free account
hub / github.com/codenameone/CodenameOne / parse

Method parse

CodenameOne/src/com/codename1/ui/CSSColor.java:49–82  ·  view source on GitHub ↗
(String value)

Source from the content-addressed store, hash-verified

47 /// `IllegalArgumentException` when the value is not a recognised color so
48 /// the caller can choose between a default and a propagated failure.
49 public static int parse(String value) {
50 String trimmed = value == null ? "" : value.trim();
51 if (trimmed.length() == 0) {
52 throw new IllegalArgumentException("Empty color");
53 }
54 if (trimmed.charAt(0) == '#') {
55 return parseHexColor(trimmed.substring(1));
56 }
57 String lower = trimmed.toLowerCase();
58 if (lower.startsWith("rgba(") || lower.startsWith("rgb(")) {
59 int open = trimmed.indexOf('(');
60 int close = trimmed.lastIndexOf(')');
61 if (open < 0 || close < open) {
62 throw new IllegalArgumentException("Unrecognised color: " + value);
63 }
64 List<String> comps = StringUtil.tokenize(trimmed.substring(open + 1, close), ',');
65 if (comps.size() < 3) {
66 throw new IllegalArgumentException("Unrecognised color: " + value);
67 }
68 int r = parseColorComponent(comps.get(0));
69 int g = parseColorComponent(comps.get(1));
70 int b = parseColorComponent(comps.get(2));
71 int a = 255;
72 if (comps.size() >= 4) {
73 a = clamp(Math.round(parseFloat(comps.get(3).trim()) * 255f));
74 }
75 return (a << 24) | (r << 16) | (g << 8) | b;
76 }
77 Integer named = NAMED_COLORS.get(lower);
78 if (named != null) {
79 return named.intValue();
80 }
81 throw new IllegalArgumentException("Unrecognised color: " + value);
82 }
83
84 /// Parses a CSS color string into `0xAARRGGBB`, returning `defaultColor`
85 /// when the value is `null` or not a recognised color. The lenient variant

Callers 9

themeColorMethod · 0.95
colorMethod · 0.95
parseStopsMethod · 0.95
colorParserFormsMethod · 0.95
hexColorVariantsMethod · 0.95
rgbAndRgbaSyntaxMethod · 0.95
namedColorsMethod · 0.95

Calls 15

lengthMethod · 0.95
charAtMethod · 0.95
parseHexColorMethod · 0.95
substringMethod · 0.95
toLowerCaseMethod · 0.95
startsWithMethod · 0.95
indexOfMethod · 0.95
lastIndexOfMethod · 0.95
tokenizeMethod · 0.95
parseColorComponentMethod · 0.95
clampMethod · 0.95
roundMethod · 0.95

Tested by 6

colorParserFormsMethod · 0.76
hexColorVariantsMethod · 0.76
rgbAndRgbaSyntaxMethod · 0.76
namedColorsMethod · 0.76