]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/utils/Colors.java
darker colour palette, matching theme (pre-generated) and run-time colours
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / utils / Colors.java
index 3c89df9c788a82dab020919e54d6ef7b4214ffd7..c61d0144fa251b9de618a53c46e063a8857985cf 100644 (file)
@@ -27,11 +27,14 @@ import androidx.annotation.ColorLong;
 import androidx.annotation.NonNull;
 import androidx.lifecycle.MutableLiveData;
 
+import net.ktnx.mobileledger.BuildConfig;
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
 import net.ktnx.mobileledger.ui.HueRing;
 
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Locale;
 
 import static java.lang.Math.abs;
@@ -39,6 +42,8 @@ import static net.ktnx.mobileledger.utils.Logger.debug;
 
 public class Colors {
     public static final int DEFAULT_HUE_DEG = 261;
+    public static final int THEME_HUE_STEP_DEG = 5;
+    public static final int baseHueStep = 60;
     private static final float blueLightness = 0.665f;
     private static final float yellowLightness = 0.350f;
     private static final int[][] EMPTY_STATES = new int[][]{new int[0]};
@@ -106,61 +111,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;
@@ -170,18 +125,23 @@ public class Colors {
         int r, g, b;
         float m = lightness - c / 2f;
 
-        if (h < 1 || h == 6) return tupleToColor(c + m, x + m, 0 + m);
-        if (h < 2) return tupleToColor(x + m, c + m, 0 + m);
-        if (h < 3) return tupleToColor(0 + m, c + m, x + m);
-        if (h < 4) return tupleToColor(0 + m, x + m, c + m);
-        if (h < 5) return tupleToColor(x + m, 0 + m, c + m);
-        if (h < 6) return tupleToColor(c + m, 0 + m, x + m);
+        if (h < 1 || h == 6)
+            return tupleToColor(c + m, x + m, 0 + m);
+        if (h < 2)
+            return tupleToColor(x + m, c + m, 0 + m);
+        if (h < 3)
+            return tupleToColor(0 + m, c + m, x + m);
+        if (h < 4)
+            return tupleToColor(0 + m, x + m, c + m);
+        if (h < 5)
+            return tupleToColor(x + m, 0 + m, c + m);
+        if (h < 6)
+            return tupleToColor(c + m, 0 + m, x + m);
 
         throw new IllegalArgumentException(String.format(
                 "Unexpected value for h (%1.3f) while converting hsl(%1.3f, %1.3f, %1.3f) to rgb",
                 h, hueRatio, saturation, lightness));
     }
-
     public static @ColorInt
     int tupleToColor(float r, float g, float b) {
         int r_int = Math.round(255 * r);
@@ -189,14 +149,39 @@ public class Colors {
         int b_int = Math.round(255 * b);
         return (r_int << 16) | (g_int << 8) | b_int;
     }
+    public static float baseHueLightness(int baseHueDegrees) {
+        switch (baseHueDegrees % 360) {
+            case 0:
+                return 0.450f;   // red
+            case 60:
+                return 0.400f;  // yellow
+            case 120:
+                return 0.400f;  // green
+            case 180:
+                return 0.400f;  // cyan
+            case 240:
+                return 0.750f;  // blue
+            case 300:
+                return 0.500f;   // magenta
+            default:
+                throw new IllegalStateException(
+                        String.format(Locale.US, "baseHueLightness called with invalid value %d",
+                                baseHueDegrees));
+        }
+    }
+    public static float hueLightness(int hueDegrees) {
+        int mod = hueDegrees % baseHueStep;
+        int x0 = hueDegrees - mod;
+        int x1 = x0 + baseHueStep;
+
+        float y0 = baseHueLightness(x0);
+        float y1 = baseHueLightness(x1);
+
+        return y0 + (hueDegrees - x0) * (y1 - y0) / (x1 - x0);
+    }
     public static @ColorInt
     int getPrimaryColorForHue(int hueDegrees) {
-//        int result = hsvColor(hueDegrees, 0.61f, 0.95f);
-        float y = hueDegrees - 60;
-        if (y < 0) y += 360;
-        float l = yellowLightness + (blueLightness - yellowLightness) *
-                                    (float) Math.cos(Math.toRadians(Math.abs(180 - y) / 2f));
-        int result = hslColor(hueDegrees / 360f, 0.845f, l);
+        int result = hslColor(hueDegrees / 360f, 0.845f, hueLightness(hueDegrees));
         debug("colors", String.format(Locale.ENGLISH, "getPrimaryColorForHue(%d) = %x", hueDegrees,
                 result));
         return result;
@@ -209,23 +194,35 @@ public class Colors {
         final int themeHue = (profile == null) ? -1 : profile.getThemeHue();
         setupTheme(activity, themeHue);
     }
-    public static void setupTheme(Activity activity, int themeHue) {
+    public static int getThemeIdForHue(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 == 360)
+            themeHue = 0;
+        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) {
-            activity.setTheme(R.style.AppTheme_NoActionBar);
+            themeId = R.style.AppTheme_NoActionBar;
             debug("profiles",
                     String.format(Locale.ENGLISH, "Theme hue %d not supported, using the default",
                             themeHue));
         }
-        else {
-            activity.setTheme(themeId);
-        }
+
+        return themeId;
+    }
+    public static void setupTheme(Activity activity, int themeHue) {
+        int themeId = getThemeIdForHue(themeHue);
+        activity.setTheme(themeId);
 
         refreshColors(activity.getTheme());
     }
@@ -248,4 +245,89 @@ public class Colors {
         }
         return colors;
     }
+    public static int getNewProfileThemeHue(ArrayList<MobileLedgerProfile> profiles) {
+        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;
+            chosenHue = opposite;
+        }
+        else {
+            ArrayList<Integer> hues = new ArrayList<>();
+            for (MobileLedgerProfile p : profiles) {
+                int hue = p.getThemeHue();
+                if (hue == -1)
+                    hue = DEFAULT_HUE_DEG;
+                hues.add(hue);
+            }
+            Collections.sort(hues);
+            if (BuildConfig.DEBUG) {
+                StringBuilder huesSB = new StringBuilder();
+                for (int h : hues) {
+                    if (huesSB.length() > 0)
+                        huesSB.append(", ");
+                    huesSB.append(String.valueOf(h));
+                }
+                debug("profiles", String.format("used hues: %s", huesSB.toString()));
+            }
+            hues.add(hues.get(0));
+
+            int lastHue = -1;
+            int largestInterval = 0;
+            ArrayList<Integer> largestIntervalStarts = new ArrayList<>();
+
+            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
+
+                if (interval > largestInterval) {
+                    largestInterval = interval;
+                    largestIntervalStarts.clear();
+                    largestIntervalStarts.add(lastHue);
+                }
+                else if (interval == largestInterval) {
+                    largestIntervalStarts.add(lastHue);
+                }
+
+                lastHue = h;
+            }
+
+            final int chosenIndex = (int) (Math.random() * largestIntervalStarts.size());
+            int chosenIntervalStart = largestIntervalStarts.get(chosenIndex);
+
+            debug("profiles",
+                    String.format(Locale.US, "Choosing the middle colour between %d and %d",
+                            chosenIntervalStart, chosenIntervalStart + largestInterval));
+
+            if (largestInterval % 2 != 0)
+                largestInterval++;    // round up the middle point
+
+            chosenHue = (chosenIntervalStart + (largestInterval / 2)) % 360;
+        }
+
+        final int mod = chosenHue % THEME_HUE_STEP_DEG;
+        if (mod != 0) {
+            if (mod > THEME_HUE_STEP_DEG / 2)
+                chosenHue += (THEME_HUE_STEP_DEG - mod); // 13 += (5-3) = 15
+            else
+                chosenHue -= mod;       // 12 -= 2 = 10
+        }
+
+        debug("profiles", String.format(Locale.US, "New profile hue: %d", chosenHue));
+
+        return chosenHue;
+    }
 }