|
48 | 48 | import java.util.ArrayList;
|
49 | 49 | import java.util.HashMap;
|
50 | 50 | import java.util.List;
|
51 |
| -import java.util.Locale; |
52 | 51 | import java.util.Map;
|
53 | 52 | import java.util.Objects;
|
54 | 53 | import kotlin.Pair;
|
@@ -680,22 +679,30 @@ private static Integer parseColor(Object object) {
|
680 | 679 | return parseColor(object.toString());
|
681 | 680 | }
|
682 | 681 |
|
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; |
686 | 686 | 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 ... |
693 | 700 | }
|
694 |
| - color = Color.parseColor(s.replace("0x", "#")); |
| 701 | + return Color.parseColor(completed); |
695 | 702 | } catch (Exception e) {
|
696 |
| - // Log.e(TAG, "unknown color " + s); |
| 703 | + Timber.w(e, "Error on parsing color: %s", s); |
| 704 | + return null; |
697 | 705 | }
|
698 |
| - return color; |
699 | 706 | }
|
700 | 707 |
|
701 | 708 | public Integer getCurrentColor(String key) {
|
@@ -1030,14 +1037,7 @@ private Object getColorRealValue(Object object) {
|
1030 | 1037 | String s = object.toString();
|
1031 | 1038 | if (!s.matches(".*[.\\\\/].*")) {
|
1032 | 1039 | 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); |
1041 | 1041 | } catch (Exception e) {
|
1042 | 1042 | Timber.e("getColorRealValue() unknown color, %s ; object %s", s, object);
|
1043 | 1043 | e.printStackTrace();
|
|
0 commit comments