Skip to content

Commit d831538

Browse files
committed
refactor(Config): tidy the code that how to parse color string
1 parent 6ad478a commit d831538

File tree

1 file changed

+21
-21
lines changed
  • app/src/main/java/com/osfans/trime/data/theme

1 file changed

+21
-21
lines changed

app/src/main/java/com/osfans/trime/data/theme/Config.java

+21-21
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import java.util.ArrayList;
4949
import java.util.HashMap;
5050
import java.util.List;
51-
import java.util.Locale;
5251
import java.util.Map;
5352
import java.util.Objects;
5453
import kotlin.Pair;
@@ -680,22 +679,30 @@ private static Integer parseColor(Object object) {
680679
return parseColor(object.toString());
681680
}
682681

683-
private static Integer parseColor(String s) {
684-
Integer color = null;
685-
if (s.contains(".")) return color; // picture name
682+
@Nullable
683+
private static Integer parseColor(@NonNull String s) {
684+
if (s.contains(".")) return null; // picture name
685+
final String hex = s.startsWith("#") ? s.replace("#", "0x") : s;
686686
try {
687-
s = s.toLowerCase(Locale.getDefault());
688-
if (s.startsWith("0x")) {
689-
if (s.length() == 3 || s.length() == 4)
690-
s = String.format("#%02x000000", Long.decode(s.substring(2))); // 0xAA
691-
else if (s.length() < 8) s = String.format("#%06x", Long.decode(s.substring(2)));
692-
else if (s.length() == 9) s = "#0" + s.substring(2);
687+
final String completed;
688+
if (hex.startsWith("0x") || hex.startsWith("0X")) {
689+
if (hex.length() == 3 || hex.length() == 4) {
690+
completed = String.format("#%02x000000", Long.decode(hex)); // 0xA -> #AA000000
691+
} else if (hex.length() < 8) { // 0xGBB -> #RRGGBB
692+
completed = String.format("#%06x", Long.decode(hex));
693+
} else if (hex.length() == 9) { // 0xARRGGBB -> #AARRGGBB
694+
completed = "#0" + hex.substring(2);
695+
} else {
696+
completed = "#" + hex.substring(2); // 0xAARRGGBB -> #AARRGGBB, 0xRRGGBB -> #RRGGBB
697+
}
698+
} else {
699+
completed = hex; // red, green, blue ...
693700
}
694-
color = Color.parseColor(s.replace("0x", "#"));
701+
return Color.parseColor(completed);
695702
} catch (Exception e) {
696-
// Log.e(TAG, "unknown color " + s);
703+
Timber.w(e, "Error on parsing color: %s", s);
704+
return null;
697705
}
698-
return color;
699706
}
700707

701708
public Integer getCurrentColor(String key) {
@@ -1030,14 +1037,7 @@ private Object getColorRealValue(Object object) {
10301037
String s = object.toString();
10311038
if (!s.matches(".*[.\\\\/].*")) {
10321039
try {
1033-
s = s.toLowerCase(Locale.getDefault());
1034-
if (s.startsWith("0x")) {
1035-
if (s.length() == 3 || s.length() == 4)
1036-
s = String.format("#%02x000000", Long.decode(s.substring(2))); // 0xAA
1037-
else if (s.length() < 8) s = String.format("#%06x", Long.decode(s.substring(2)));
1038-
else if (s.length() == 9) s = "#0" + s.substring(2);
1039-
}
1040-
if (s.matches("(0x|#)?[a-f0-9]+")) return Color.parseColor(s.replace("0x", "#"));
1040+
return parseColor(s);
10411041
} catch (Exception e) {
10421042
Timber.e("getColorRealValue() unknown color, %s ; object %s", s, object);
10431043
e.printStackTrace();

0 commit comments

Comments
 (0)