]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/Colors.java
845cda3657675ba57d204edd48d28f7dee4f5153
[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, defaultTextColorDisabled;
58     public static int profileThemeId = -1;
59     public static MutableLiveData<Integer> themeWatch = new MutableLiveData<>(0);
60     public static int errorTextColor;
61     private static int SWIPE_COLOR_COUNT = 6;
62     private static int[] themeIDs =
63             {R.style.AppTheme_000, R.style.AppTheme_005,
64              R.style.AppTheme_010, R.style.AppTheme_015,
65              R.style.AppTheme_020, R.style.AppTheme_025,
66              R.style.AppTheme_030, R.style.AppTheme_035,
67              R.style.AppTheme_040, R.style.AppTheme_045,
68              R.style.AppTheme_050, R.style.AppTheme_055,
69              R.style.AppTheme_060, R.style.AppTheme_065,
70              R.style.AppTheme_070, R.style.AppTheme_075,
71              R.style.AppTheme_080, R.style.AppTheme_085,
72              R.style.AppTheme_090, R.style.AppTheme_095,
73              R.style.AppTheme_100, R.style.AppTheme_105,
74              R.style.AppTheme_110, R.style.AppTheme_115,
75              R.style.AppTheme_120, R.style.AppTheme_125,
76              R.style.AppTheme_130, R.style.AppTheme_135,
77              R.style.AppTheme_140, R.style.AppTheme_145,
78              R.style.AppTheme_150, R.style.AppTheme_155,
79              R.style.AppTheme_160, R.style.AppTheme_165,
80              R.style.AppTheme_170, R.style.AppTheme_175,
81              R.style.AppTheme_180, R.style.AppTheme_185,
82              R.style.AppTheme_190, R.style.AppTheme_195,
83              R.style.AppTheme_200, R.style.AppTheme_205,
84              R.style.AppTheme_210, R.style.AppTheme_215,
85              R.style.AppTheme_220, R.style.AppTheme_225,
86              R.style.AppTheme_230, R.style.AppTheme_235,
87              R.style.AppTheme_240, R.style.AppTheme_245,
88              R.style.AppTheme_250, R.style.AppTheme_255,
89              R.style.AppTheme_260, R.style.AppTheme_265,
90              R.style.AppTheme_270, R.style.AppTheme_275,
91              R.style.AppTheme_280, R.style.AppTheme_285,
92              R.style.AppTheme_290, R.style.AppTheme_295,
93              R.style.AppTheme_300, R.style.AppTheme_305,
94              R.style.AppTheme_310, R.style.AppTheme_315,
95              R.style.AppTheme_320, R.style.AppTheme_325,
96              R.style.AppTheme_330, R.style.AppTheme_335,
97              R.style.AppTheme_340, R.style.AppTheme_345,
98              R.style.AppTheme_350, R.style.AppTheme_355,
99              };
100     public static void refreshColors(Resources.Theme theme) {
101         TypedValue tv = new TypedValue();
102         theme.resolveAttribute(R.attr.table_row_dark_bg, tv, true);
103         tableRowDarkBG = tv.data;
104         theme.resolveAttribute(R.attr.table_row_light_bg, tv, true);
105         tableRowLightBG = tv.data;
106         theme.resolveAttribute(R.attr.colorPrimary, tv, true);
107         primary = tv.data;
108         theme.resolveAttribute(R.attr.textColor, tv, true);
109         defaultTextColor = tv.data;
110         defaultTextColorDisabled = 0x7f000000 | 0x00ffffff & defaultTextColor;
111         theme.resolveAttribute(R.attr.colorAccent, tv, true);
112         accent = tv.data;
113         theme.resolveAttribute(R.attr.errorTextColor, tv, true);
114         errorTextColor = tv.data;
115
116         // trigger theme observers
117         themeWatch.postValue(themeWatch.getValue() + 1);
118     }
119     public static @ColorInt
120     int hslColor(float hueRatio, float saturation, float lightness) {
121         return 0xff000000 | hslTriplet(hueRatio, saturation, lightness);
122     }
123     public static @ColorInt
124     int hslTriplet(float hueRatio, float saturation, float lightness) {
125         @ColorLong long result;
126         float h = hueRatio * 6;
127         float c = (1 - abs(2f * lightness - 1)) * saturation;
128         float h_mod_2 = h % 2;
129         float x = c * (1 - Math.abs(h_mod_2 - 1));
130         int r, g, b;
131         float m = lightness - c / 2f;
132
133         if (h < 1 || h == 6)
134             return tupleToColor(c + m, x + m, 0 + m);
135         if (h < 2)
136             return tupleToColor(x + m, c + m, 0 + m);
137         if (h < 3)
138             return tupleToColor(0 + m, c + m, x + m);
139         if (h < 4)
140             return tupleToColor(0 + m, x + m, c + m);
141         if (h < 5)
142             return tupleToColor(x + m, 0 + m, c + m);
143         if (h < 6)
144             return tupleToColor(c + m, 0 + m, x + m);
145
146         throw new IllegalArgumentException(String.format(
147                 "Unexpected value for h (%1.3f) while converting hsl(%1.3f, %1.3f, %1.3f) to rgb",
148                 h, hueRatio, saturation, lightness));
149     }
150     public static @ColorInt
151     int tupleToColor(float r, float g, float b) {
152         int r_int = Math.round(255 * r);
153         int g_int = Math.round(255 * g);
154         int b_int = Math.round(255 * b);
155         return (r_int << 16) | (g_int << 8) | b_int;
156     }
157     public static float baseHueLightness(int baseHueDegrees) {
158         switch (baseHueDegrees % 360) {
159             case 0:
160                 return 0.450f;   // red
161             case 60:
162                 return 0.400f;  // yellow
163             case 120:
164                 return 0.400f;  // green
165             case 180:
166                 return 0.400f;  // cyan
167             case 240:
168                 return 0.750f;  // blue
169             case 300:
170                 return 0.500f;   // magenta
171             default:
172                 throw new IllegalStateException(
173                         String.format(Locale.US, "baseHueLightness called with invalid value %d",
174                                 baseHueDegrees));
175         }
176     }
177     public static float hueLightness(int hueDegrees) {
178         int mod = hueDegrees % baseHueStep;
179         int x0 = hueDegrees - mod;
180         int x1 = x0 + baseHueStep;
181
182         float y0 = baseHueLightness(x0);
183         float y1 = baseHueLightness(x1);
184
185         return y0 + (hueDegrees - x0) * (y1 - y0) / (x1 - x0);
186     }
187     public static @ColorInt
188     int getPrimaryColorForHue(int hueDegrees) {
189         int result = hslColor(hueDegrees / 360f, 0.845f, hueLightness(hueDegrees));
190         debug("colors", String.format(Locale.ENGLISH, "getPrimaryColorForHue(%d) = %x", hueDegrees,
191                 result));
192         return result;
193     }
194     public static void setupTheme(Activity activity) {
195         MobileLedgerProfile profile = Data.profile.getValue();
196         setupTheme(activity, profile);
197     }
198     public static void setupTheme(Activity activity, MobileLedgerProfile profile) {
199         final int themeHue = (profile == null) ? -1 : profile.getThemeHue();
200         setupTheme(activity, themeHue);
201     }
202     public static int getThemeIdForHue(int themeHue) {
203         int themeId = -1;
204         if (themeHue == 360)
205             themeHue = 0;
206         if ((themeHue >= 0) && (themeHue < 360) && (themeHue != DEFAULT_HUE_DEG)) {
207             int index;
208             if ((themeHue % HueRing.hueStepDegrees) != 0) {
209                 Logger.warn("profiles",
210                         String.format(Locale.US, "Adjusting unexpected hue %d", themeHue));
211                 index = Math.round(1f * themeHue / HueRing.hueStepDegrees);
212             }
213             else
214                 index = themeHue / HueRing.hueStepDegrees;
215
216             themeId = themeIDs[index];
217         }
218
219         if (themeId < 0) {
220             themeId = R.style.AppTheme;
221             debug("profiles",
222                     String.format(Locale.ENGLISH, "Theme hue %d not supported, using the default",
223                             themeHue));
224         }
225
226         return themeId;
227     }
228     public static void setupTheme(Activity activity, int themeHue) {
229         int themeId = getThemeIdForHue(themeHue);
230         activity.setTheme(themeId);
231
232         refreshColors(activity.getTheme());
233     }
234
235     public static @NonNull
236     ColorStateList getColorStateList() {
237         return getColorStateList(profileThemeId);
238     }
239     public static @NonNull
240     ColorStateList getColorStateList(int hue) {
241         return new ColorStateList(EMPTY_STATES, getSwipeCircleColors(hue));
242     }
243     public static int[] getSwipeCircleColors() {
244         return getSwipeCircleColors(profileThemeId);
245     }
246     public static int[] getSwipeCircleColors(int hue) {
247         int[] colors = new int[SWIPE_COLOR_COUNT];
248         for (int i = 0; i < SWIPE_COLOR_COUNT; i++, hue = (hue + 360 / SWIPE_COLOR_COUNT) % 360) {
249             colors[i] = getPrimaryColorForHue(hue);
250         }
251         return colors;
252     }
253     public static int getNewProfileThemeHue(ArrayList<MobileLedgerProfile> profiles) {
254         if ((profiles == null) || (profiles.size() == 0))
255             return DEFAULT_HUE_DEG;
256
257         int chosenHue;
258
259         if (profiles.size() == 1) {
260             int opposite = profiles.get(0)
261                                    .getThemeHue() + 180;
262             opposite %= 360;
263             chosenHue = opposite;
264         }
265         else {
266             ArrayList<Integer> hues = new ArrayList<>();
267             for (MobileLedgerProfile p : profiles) {
268                 int hue = p.getThemeHue();
269                 if (hue == -1)
270                     hue = DEFAULT_HUE_DEG;
271                 hues.add(hue);
272             }
273             Collections.sort(hues);
274             if (BuildConfig.DEBUG) {
275                 StringBuilder huesSB = new StringBuilder();
276                 for (int h : hues) {
277                     if (huesSB.length() > 0)
278                         huesSB.append(", ");
279                     huesSB.append(String.valueOf(h));
280                 }
281                 debug("profiles", String.format("used hues: %s", huesSB.toString()));
282             }
283             hues.add(hues.get(0));
284
285             int lastHue = -1;
286             int largestInterval = 0;
287             ArrayList<Integer> largestIntervalStarts = new ArrayList<>();
288
289             for (int h : hues) {
290                 if (lastHue == -1) {
291                     lastHue = h;
292                     continue;
293                 }
294
295                 int interval;
296                 if (h > lastHue)
297                     interval = h - lastHue;     // 10 -> 20 is a step of 10
298                 else
299                     interval = h + (360 - lastHue);    // 350 -> 20 is a step of 30
300
301                 if (interval > largestInterval) {
302                     largestInterval = interval;
303                     largestIntervalStarts.clear();
304                     largestIntervalStarts.add(lastHue);
305                 }
306                 else if (interval == largestInterval) {
307                     largestIntervalStarts.add(lastHue);
308                 }
309
310                 lastHue = h;
311             }
312
313             final int chosenIndex = (int) (Math.random() * largestIntervalStarts.size());
314             int chosenIntervalStart = largestIntervalStarts.get(chosenIndex);
315
316             debug("profiles",
317                     String.format(Locale.US, "Choosing the middle colour between %d and %d",
318                             chosenIntervalStart, chosenIntervalStart + largestInterval));
319
320             if (largestInterval % 2 != 0)
321                 largestInterval++;    // round up the middle point
322
323             chosenHue = (chosenIntervalStart + (largestInterval / 2)) % 360;
324         }
325
326         final int mod = chosenHue % THEME_HUE_STEP_DEG;
327         if (mod != 0) {
328             if (mod > THEME_HUE_STEP_DEG / 2)
329                 chosenHue += (THEME_HUE_STEP_DEG - mod); // 13 += (5-3) = 15
330             else
331                 chosenHue -= mod;       // 12 -= 2 = 10
332         }
333
334         debug("profiles", String.format(Locale.US, "New profile hue: %d", chosenHue));
335
336         return chosenHue;
337     }
338 }