X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Futils%2FColors.java;h=d820f2441d7e4b7f9562a1ea6c03e1847233a222;hb=9829a44a3f6146aa6e2ed23a0a54ccabdcde0f36;hp=1cff8970992fefec4a93545f547bb6f12f54b66f;hpb=3d89211c7a825602e385ff7cb22ef6f69d789735;p=mobile-ledger-staging.git 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..d820f244 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/Colors.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/Colors.java @@ -109,62 +109,11 @@ public class Colors { // trigger theme observers themeWatch.postValue(themeWatch.getValue() + 1); } - public static @ColorLong - long hsvaColor(float hue, float saturation, float value, float alpha) { - if (alpha < 0 || alpha > 1) - throw new IllegalArgumentException("alpha must be between 0 and 1"); - - @ColorLong long rgb = hsvTriplet(hue, saturation, value); - - long a_bits = Math.round(255 * alpha); - return (a_bits << 24) | rgb; - } - public static @ColorInt - int hsvColor(float hue, float saturation, float value) { - return 0xff000000 | hsvTriplet(hue, saturation, value); - } public static @ColorInt int hslColor(float hueRatio, float saturation, float lightness) { return 0xff000000 | hslTriplet(hueRatio, saturation, lightness); } public static @ColorInt - int hsvTriplet(float hue, float saturation, float value) { - @ColorLong long result; - int r, g, b; - - if ((hue < -0.00005) || (hue > 1.0000005) || (saturation < 0) || (saturation > 1) || - (value < 0) || (value > 1)) - throw new IllegalArgumentException(String.format( - "hue, saturation, value and alpha must all be between 0 and 1. Arguments " + - "given: " + "hue=%1.5f, sat=%1.5f, val=%1.5f", hue, saturation, value)); - - int h = (int) (hue * 6); - float f = hue * 6 - h; - float p = value * (1 - saturation); - float q = value * (1 - f * saturation); - float t = value * (1 - (1 - f) * saturation); - - switch (h) { - case 0: - case 6: - return tupleToColor(value, t, p); - case 1: - return tupleToColor(q, value, p); - case 2: - return tupleToColor(p, value, t); - case 3: - return tupleToColor(p, q, value); - case 4: - return tupleToColor(t, p, value); - case 5: - return tupleToColor(value, p, q); - default: - throw new RuntimeException(String.format("Unexpected value for h (%d) while " + - "converting hsv(%1.2f, %1.2f, %1.2f) to " + - "rgb", h, hue, saturation, value)); - } - } - public static @ColorInt int hslTriplet(float hueRatio, float saturation, float lightness) { @ColorLong long result; float h = hueRatio * 6; @@ -222,11 +171,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 +221,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) {