]> git.ktnx.net Git - mobile-ledger.git/blob - 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
1 /*
2  * Copyright © 2019 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.utils;
19
20 import android.app.Activity;
21 import android.content.res.ColorStateList;
22 import android.content.res.Resources;
23 import android.util.TypedValue;
24
25 import androidx.annotation.ColorInt;
26 import androidx.annotation.ColorLong;
27 import androidx.annotation.NonNull;
28 import androidx.lifecycle.MutableLiveData;
29
30 import net.ktnx.mobileledger.BuildConfig;
31 import net.ktnx.mobileledger.R;
32 import net.ktnx.mobileledger.model.Data;
33 import net.ktnx.mobileledger.model.MobileLedgerProfile;
34 import net.ktnx.mobileledger.ui.HueRing;
35
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.Locale;
39
40 import static java.lang.Math.abs;
41 import static net.ktnx.mobileledger.utils.Logger.debug;
42
43 public class Colors {
44     public static final int DEFAULT_HUE_DEG = 261;
45     public static final int THEME_HUE_STEP_DEG = 5;
46     public static final int baseHueStep = 60;
47     private static final float blueLightness = 0.665f;
48     private static final float yellowLightness = 0.350f;
49     private static final int[][] EMPTY_STATES = new int[][]{new int[0]};
50     public static @ColorInt
51     int accent;
52     @ColorInt
53     public static int tableRowLightBG;
54     @ColorInt
55     public static int tableRowDarkBG;
56     @ColorInt
57     public static int primary, defaultTextColor;
58     public static int profileThemeId = -1;
59     public static MutableLiveData<Integer> themeWatch = new MutableLiveData<>(0);
60     private static int[] themeIDs =
61             {R.style.AppTheme_NoActionBar_000, R.style.AppTheme_NoActionBar_005,
62              R.style.AppTheme_NoActionBar_010, R.style.AppTheme_NoActionBar_015,
63              R.style.AppTheme_NoActionBar_020, R.style.AppTheme_NoActionBar_025,
64              R.style.AppTheme_NoActionBar_030, R.style.AppTheme_NoActionBar_035,
65              R.style.AppTheme_NoActionBar_040, R.style.AppTheme_NoActionBar_045,
66              R.style.AppTheme_NoActionBar_050, R.style.AppTheme_NoActionBar_055,
67              R.style.AppTheme_NoActionBar_060, R.style.AppTheme_NoActionBar_065,
68              R.style.AppTheme_NoActionBar_070, R.style.AppTheme_NoActionBar_075,
69              R.style.AppTheme_NoActionBar_080, R.style.AppTheme_NoActionBar_085,
70              R.style.AppTheme_NoActionBar_090, R.style.AppTheme_NoActionBar_095,
71              R.style.AppTheme_NoActionBar_100, R.style.AppTheme_NoActionBar_105,
72              R.style.AppTheme_NoActionBar_110, R.style.AppTheme_NoActionBar_115,
73              R.style.AppTheme_NoActionBar_120, R.style.AppTheme_NoActionBar_125,
74              R.style.AppTheme_NoActionBar_130, R.style.AppTheme_NoActionBar_135,
75              R.style.AppTheme_NoActionBar_140, R.style.AppTheme_NoActionBar_145,
76              R.style.AppTheme_NoActionBar_150, R.style.AppTheme_NoActionBar_155,
77              R.style.AppTheme_NoActionBar_160, R.style.AppTheme_NoActionBar_165,
78              R.style.AppTheme_NoActionBar_170, R.style.AppTheme_NoActionBar_175,
79              R.style.AppTheme_NoActionBar_180, R.style.AppTheme_NoActionBar_185,
80              R.style.AppTheme_NoActionBar_190, R.style.AppTheme_NoActionBar_195,
81              R.style.AppTheme_NoActionBar_200, R.style.AppTheme_NoActionBar_205,
82              R.style.AppTheme_NoActionBar_210, R.style.AppTheme_NoActionBar_215,
83              R.style.AppTheme_NoActionBar_220, R.style.AppTheme_NoActionBar_225,
84              R.style.AppTheme_NoActionBar_230, R.style.AppTheme_NoActionBar_235,
85              R.style.AppTheme_NoActionBar_240, R.style.AppTheme_NoActionBar_245,
86              R.style.AppTheme_NoActionBar_250, R.style.AppTheme_NoActionBar_255,
87              R.style.AppTheme_NoActionBar_260, R.style.AppTheme_NoActionBar_265,
88              R.style.AppTheme_NoActionBar_270, R.style.AppTheme_NoActionBar_275,
89              R.style.AppTheme_NoActionBar_280, R.style.AppTheme_NoActionBar_285,
90              R.style.AppTheme_NoActionBar_290, R.style.AppTheme_NoActionBar_295,
91              R.style.AppTheme_NoActionBar_300, R.style.AppTheme_NoActionBar_305,
92              R.style.AppTheme_NoActionBar_310, R.style.AppTheme_NoActionBar_315,
93              R.style.AppTheme_NoActionBar_320, R.style.AppTheme_NoActionBar_325,
94              R.style.AppTheme_NoActionBar_330, R.style.AppTheme_NoActionBar_335,
95              R.style.AppTheme_NoActionBar_340, R.style.AppTheme_NoActionBar_345,
96              R.style.AppTheme_NoActionBar_350, R.style.AppTheme_NoActionBar_355,
97              };
98     public static void refreshColors(Resources.Theme theme) {
99         TypedValue tv = new TypedValue();
100         theme.resolveAttribute(R.attr.table_row_dark_bg, tv, true);
101         tableRowDarkBG = tv.data;
102         theme.resolveAttribute(R.attr.table_row_light_bg, tv, true);
103         tableRowLightBG = tv.data;
104         theme.resolveAttribute(R.attr.colorPrimary, tv, true);
105         primary = tv.data;
106         theme.resolveAttribute(R.attr.textColor, tv, true);
107         defaultTextColor = tv.data;
108         theme.resolveAttribute(R.attr.colorAccent, tv, true);
109         accent = tv.data;
110
111         // trigger theme observers
112         themeWatch.postValue(themeWatch.getValue() + 1);
113     }
114     public static @ColorInt
115     int hslColor(float hueRatio, float saturation, float lightness) {
116         return 0xff000000 | hslTriplet(hueRatio, saturation, lightness);
117     }
118     public static @ColorInt
119     int hslTriplet(float hueRatio, float saturation, float lightness) {
120         @ColorLong long result;
121         float h = hueRatio * 6;
122         float c = (1 - abs(2f * lightness - 1)) * saturation;
123         float h_mod_2 = h % 2;
124         float x = c * (1 - Math.abs(h_mod_2 - 1));
125         int r, g, b;
126         float m = lightness - c / 2f;
127
128         if (h < 1 || h == 6)
129             return tupleToColor(c + m, x + m, 0 + m);
130         if (h < 2)
131             return tupleToColor(x + m, c + m, 0 + m);
132         if (h < 3)
133             return tupleToColor(0 + m, c + m, x + m);
134         if (h < 4)
135             return tupleToColor(0 + m, x + m, c + m);
136         if (h < 5)
137             return tupleToColor(x + m, 0 + m, c + m);
138         if (h < 6)
139             return tupleToColor(c + m, 0 + m, x + m);
140
141         throw new IllegalArgumentException(String.format(
142                 "Unexpected value for h (%1.3f) while converting hsl(%1.3f, %1.3f, %1.3f) to rgb",
143                 h, hueRatio, saturation, lightness));
144     }
145     public static @ColorInt
146     int tupleToColor(float r, float g, float b) {
147         int r_int = Math.round(255 * r);
148         int g_int = Math.round(255 * g);
149         int b_int = Math.round(255 * b);
150         return (r_int << 16) | (g_int << 8) | b_int;
151     }
152     public static float baseHueLightness(int baseHueDegrees) {
153         switch (baseHueDegrees % 360) {
154             case 0:
155                 return 0.450f;   // red
156             case 60:
157                 return 0.400f;  // yellow
158             case 120:
159                 return 0.400f;  // green
160             case 180:
161                 return 0.400f;  // cyan
162             case 240:
163                 return 0.750f;  // blue
164             case 300:
165                 return 0.500f;   // magenta
166             default:
167                 throw new IllegalStateException(
168                         String.format(Locale.US, "baseHueLightness called with invalid value %d",
169                                 baseHueDegrees));
170         }
171     }
172     public static float hueLightness(int hueDegrees) {
173         int mod = hueDegrees % baseHueStep;
174         int x0 = hueDegrees - mod;
175         int x1 = x0 + baseHueStep;
176
177         float y0 = baseHueLightness(x0);
178         float y1 = baseHueLightness(x1);
179
180         return y0 + (hueDegrees - x0) * (y1 - y0) / (x1 - x0);
181     }
182     public static @ColorInt
183     int getPrimaryColorForHue(int hueDegrees) {
184         int result = hslColor(hueDegrees / 360f, 0.845f, hueLightness(hueDegrees));
185         debug("colors", String.format(Locale.ENGLISH, "getPrimaryColorForHue(%d) = %x", hueDegrees,
186                 result));
187         return result;
188     }
189     public static void setupTheme(Activity activity) {
190         MobileLedgerProfile profile = Data.profile.getValue();
191         setupTheme(activity, profile);
192     }
193     public static void setupTheme(Activity activity, MobileLedgerProfile profile) {
194         final int themeHue = (profile == null) ? -1 : profile.getThemeHue();
195         setupTheme(activity, themeHue);
196     }
197     public static int getThemeIdForHue(int themeHue) {
198         int themeId = -1;
199         if (themeHue == 360)
200             themeHue = 0;
201         if ((themeHue >= 0) && (themeHue < 360)) {
202             int index;
203             if ((themeHue % HueRing.hueStepDegrees) != 0) {
204                 Logger.warn("profiles",
205                         String.format(Locale.US, "Adjusting unexpected hue %d", themeHue));
206                 index = Math.round(1f * themeHue / HueRing.hueStepDegrees);
207             }
208             else
209                 index = themeHue / HueRing.hueStepDegrees;
210
211             themeId = themeIDs[index];
212         }
213
214         if (themeId < 0) {
215             themeId = R.style.AppTheme_NoActionBar;
216             debug("profiles",
217                     String.format(Locale.ENGLISH, "Theme hue %d not supported, using the default",
218                             themeHue));
219         }
220
221         return themeId;
222     }
223     public static void setupTheme(Activity activity, int themeHue) {
224         int themeId = getThemeIdForHue(themeHue);
225         activity.setTheme(themeId);
226
227         refreshColors(activity.getTheme());
228     }
229
230     public static @NonNull
231     ColorStateList getColorStateList() {
232         return getColorStateList(profileThemeId);
233     }
234     public static @NonNull
235     ColorStateList getColorStateList(int hue) {
236         return new ColorStateList(EMPTY_STATES, getColors(hue));
237     }
238     public static int[] getColors() {
239         return getColors(profileThemeId);
240     }
241     public static int[] getColors(int hue) {
242         int[] colors = new int[]{0, 0, 0, 0, 0, 0};
243         for (int i = 0; i < 6; i++, hue = (hue + 60) % 360) {
244             colors[i] = getPrimaryColorForHue(hue);
245         }
246         return colors;
247     }
248     public static int getNewProfileThemeHue(ArrayList<MobileLedgerProfile> profiles) {
249         if ((profiles == null) || (profiles.size() == 0))
250             return DEFAULT_HUE_DEG;
251
252         int chosenHue;
253
254         if (profiles.size() == 1) {
255             int opposite = profiles.get(0)
256                                    .getThemeHue() + 180;
257             opposite %= 360;
258             chosenHue = opposite;
259         }
260         else {
261             ArrayList<Integer> hues = new ArrayList<>();
262             for (MobileLedgerProfile p : profiles) {
263                 int hue = p.getThemeHue();
264                 if (hue == -1)
265                     hue = DEFAULT_HUE_DEG;
266                 hues.add(hue);
267             }
268             Collections.sort(hues);
269             if (BuildConfig.DEBUG) {
270                 StringBuilder huesSB = new StringBuilder();
271                 for (int h : hues) {
272                     if (huesSB.length() > 0)
273                         huesSB.append(", ");
274                     huesSB.append(String.valueOf(h));
275                 }
276                 debug("profiles", String.format("used hues: %s", huesSB.toString()));
277             }
278             hues.add(hues.get(0));
279
280             int lastHue = -1;
281             int largestInterval = 0;
282             ArrayList<Integer> largestIntervalStarts = new ArrayList<>();
283
284             for (int h : hues) {
285                 if (lastHue == -1) {
286                     lastHue = h;
287                     continue;
288                 }
289
290                 int interval;
291                 if (h > lastHue)
292                     interval = h - lastHue;     // 10 -> 20 is a step of 10
293                 else
294                     interval = h + (360 - lastHue);    // 350 -> 20 is a step of 30
295
296                 if (interval > largestInterval) {
297                     largestInterval = interval;
298                     largestIntervalStarts.clear();
299                     largestIntervalStarts.add(lastHue);
300                 }
301                 else if (interval == largestInterval) {
302                     largestIntervalStarts.add(lastHue);
303                 }
304
305                 lastHue = h;
306             }
307
308             final int chosenIndex = (int) (Math.random() * largestIntervalStarts.size());
309             int chosenIntervalStart = largestIntervalStarts.get(chosenIndex);
310
311             debug("profiles",
312                     String.format(Locale.US, "Choosing the middle colour between %d and %d",
313                             chosenIntervalStart, chosenIntervalStart + largestInterval));
314
315             if (largestInterval % 2 != 0)
316                 largestInterval++;    // round up the middle point
317
318             chosenHue = (chosenIntervalStart + (largestInterval / 2)) % 360;
319         }
320
321         final int mod = chosenHue % THEME_HUE_STEP_DEG;
322         if (mod != 0) {
323             if (mod > THEME_HUE_STEP_DEG / 2)
324                 chosenHue += (THEME_HUE_STEP_DEG - mod); // 13 += (5-3) = 15
325             else
326                 chosenHue -= mod;       // 12 -= 2 = 10
327         }
328
329         debug("profiles", String.format(Locale.US, "New profile hue: %d", chosenHue));
330
331         return chosenHue;
332     }
333 }