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