From 5bca8f7e0d25965891cb80a3acc5e4b15fb8bac4 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sun, 3 May 2020 22:23:00 +0300 Subject: [PATCH] fix rounding of the automatic profile color of the second profile after using the default for the first was causing invalid theme IDs, falling back the default --- .../net/ktnx/mobileledger/utils/Colors.java | 92 +++++++++++-------- 1 file changed, 52 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/Colors.java b/app/src/main/java/net/ktnx/mobileledger/utils/Colors.java index 1cff8970..7635ef3c 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/Colors.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/Colors.java @@ -222,11 +222,19 @@ public class Colors { } public static void setupTheme(Activity activity, int themeHue) { int themeId = -1; - // Relies that theme resource IDs are sequential numbers if (themeHue == 360) themeHue = 0; - if ((themeHue >= 0) && (themeHue < 360) && ((themeHue % HueRing.hueStepDegrees) == 0)) { - themeId = themeIDs[themeHue / HueRing.hueStepDegrees]; + if ((themeHue >= 0) && (themeHue < 360)) { + int index; + if ((themeHue % HueRing.hueStepDegrees) != 0) { + Logger.warn("profiles", + String.format(Locale.US, "Adjusting unexpected hue %d", themeHue)); + index = Math.round(1f * themeHue / HueRing.hueStepDegrees); + } + else + index = themeHue / HueRing.hueStepDegrees; + + themeId = themeIDs[index]; } if (themeId < 0) { @@ -264,57 +272,61 @@ public class Colors { if ((profiles == null) || (profiles.size() == 0)) return DEFAULT_HUE_DEG; + int chosenHue; + if (profiles.size() == 1) { int opposite = profiles.get(0) .getThemeHue() + 180; opposite %= 360; - return opposite; + chosenHue = opposite; } + else { + ArrayList hues = new ArrayList<>(); + for (MobileLedgerProfile p : profiles) { + int hue = p.getThemeHue(); + if (hue == -1) + hue = DEFAULT_HUE_DEG; + hues.add(hue); + } + Collections.sort(hues); + hues.add(hues.get(0)); - ArrayList hues = new ArrayList<>(); - for (MobileLedgerProfile p : profiles) { - int hue = p.getThemeHue(); - if (hue == -1) - hue = DEFAULT_HUE_DEG; - hues.add(hue); - } - Collections.sort(hues); - hues.add(hues.get(0)); + int lastHue = -1; + int largestInterval = 0; + ArrayList largestIntervalStarts = new ArrayList<>(); - int lastHue = -1; - int largestInterval = 0; - ArrayList largestIntervalStarts = new ArrayList<>(); + for (int h : hues) { + if (lastHue == -1) { + lastHue = h; + continue; + } - for (int h : hues) { - if (lastHue == -1) { - lastHue = h; - continue; - } + int interval; + if (h > lastHue) + interval = h - lastHue; // 10 -> 20 is a step of 10 + else + interval = h + (360 - lastHue); // 350 -> 20 is a step of 30 - int interval; - if (h > lastHue) - interval = h - lastHue; // 10 -> 20 is a step of 10 - else - interval = h + (360 - lastHue); // 350 -> 20 is a step of 30 + if (interval > largestInterval) { + largestInterval = interval; + largestIntervalStarts.clear(); + largestIntervalStarts.add(lastHue); + } + else if (interval == largestInterval) { + largestIntervalStarts.add(lastHue); + } - if (interval > largestInterval) { - largestInterval = interval; - largestIntervalStarts.clear(); - largestIntervalStarts.add(lastHue); - } - else if (interval == largestInterval) { - largestIntervalStarts.add(lastHue); + lastHue = h; } - lastHue = h; - } + final int chosenIndex = (int) (Math.random() * largestIntervalStarts.size()); + int chosenIntervalStart = largestIntervalStarts.get(chosenIndex); - final int chosenIndex = (int) (Math.random() * largestIntervalStarts.size()); - int chosenIntervalStart = largestIntervalStarts.get(chosenIndex); + if (largestInterval % 2 != 0) + largestInterval++; // round up the middle point - if (largestInterval % 2 != 0) - largestInterval++; // round up the middle point - int chosenHue = (chosenIntervalStart + (largestInterval / 2)) % 360; + chosenHue = (chosenIntervalStart + (largestInterval / 2)) % 360; + } final int mod = chosenHue % THEME_HUE_STEP_DEG; if (mod != 0) { -- 2.39.5