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