Skip to content

Commit

Permalink
Fix #674 Parse colors with alpha channel
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Niedermann <info@niedermann.it>
  • Loading branch information
stefan-niedermann committed Sep 3, 2020
1 parent 6ccf215 commit c24621a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ public void testGetCleanHexaColorString() {
validColors.add(new Pair<>("ccc", "#cccccc"));
validColors.add(new Pair<>("af0", "#aaff00"));
validColors.add(new Pair<>("#af0", "#aaff00"));
// Strip alpha channel (https://github.com/stefan-niedermann/nextcloud-deck/issues/674)
validColors.add(new Pair<>("af05", "#aaff00"));
validColors.add(new Pair<>("#af05", "#aaff00"));
validColors.add(new Pair<>("aaff0055", "#aaff00"));
validColors.add(new Pair<>("#aaff0055", "#aaff00"));
for (Pair<String, String> color : validColors) {
assertEquals("Expect " + color.first + " to be cleaned up to " + color.second, color.second, ColorUtil.formatColorToParsableHexString(color.first));
}
Expand Down
31 changes: 24 additions & 7 deletions app/src/main/java/it/niedermann/nextcloud/deck/util/ColorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import androidx.annotation.Nullable;
import androidx.core.util.Pair;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -47,14 +48,30 @@ public static String formatColorToParsableHexString(String input) {
}
final char[] chars = input.replaceAll("#", "").toCharArray();
final StringBuilder sb = new StringBuilder(7).append("#");
if (chars.length == 6) {
sb.append(chars);
} else if (chars.length == 3) {
for (char c : chars) {
sb.append(c).append(c);
switch (chars.length) {
case 8: { // Strip alpha channel
sb.append(Arrays.copyOfRange(chars, 0, 6));
break;
}
case 6: { // Default long
sb.append(chars);
break;
}
case 4: { // Strip alpha channel
for (char c : Arrays.copyOfRange(chars, 0, 3)) {
sb.append(c).append(c);
}
break;
}
case 3: { // Default short
for (char c : chars) {
sb.append(c).append(c);
}
break;
}
default: {
throw new IllegalArgumentException("unparsable color string: \"" + input + "\"");
}
} else {
throw new IllegalArgumentException("unparsable color string: \"" + input + "\"");
}
final String formattedHexColor = sb.toString();
if (isParsableValidHexColorString(formattedHexColor)) {
Expand Down

0 comments on commit c24621a

Please sign in to comment.