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