]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/Colors.java
d820f2441d7e4b7f9562a1ea6c03e1847233a222
[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.R;
31 import net.ktnx.mobileledger.model.Data;
32 import net.ktnx.mobileledger.model.MobileLedgerProfile;
33 import net.ktnx.mobileledger.ui.HueRing;
34
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.Locale;
38
39 import static java.lang.Math.abs;
40 import static net.ktnx.mobileledger.utils.Logger.debug;
41
42 public class Colors {
43     public static final int DEFAULT_HUE_DEG = 261;
44     public static final int THEME_HUE_STEP_DEG = 5;
45     private static final float blueLightness = 0.665f;
46     private static final float yellowLightness = 0.350f;
47     private static final int[][] EMPTY_STATES = new int[][]{new int[0]};
48     public static @ColorInt
49     int accent;
50     @ColorInt
51     public static int tableRowLightBG;
52     @ColorInt
53     public static int tableRowDarkBG;
54     @ColorInt
55     public static int primary, defaultTextColor;
56     public static int profileThemeId = -1;
57     public static MutableLiveData<Integer> themeWatch = new MutableLiveData<>(0);
58     private static int[] themeIDs =
59             {R.style.AppTheme_NoActionBar_000, R.style.AppTheme_NoActionBar_005,
60              R.style.AppTheme_NoActionBar_010, R.style.AppTheme_NoActionBar_015,
61              R.style.AppTheme_NoActionBar_020, R.style.AppTheme_NoActionBar_025,
62              R.style.AppTheme_NoActionBar_030, R.style.AppTheme_NoActionBar_035,
63              R.style.AppTheme_NoActionBar_040, R.style.AppTheme_NoActionBar_045,
64              R.style.AppTheme_NoActionBar_050, R.style.AppTheme_NoActionBar_055,
65              R.style.AppTheme_NoActionBar_060, R.style.AppTheme_NoActionBar_065,
66              R.style.AppTheme_NoActionBar_070, R.style.AppTheme_NoActionBar_075,
67              R.style.AppTheme_NoActionBar_080, R.style.AppTheme_NoActionBar_085,
68              R.style.AppTheme_NoActionBar_090, R.style.AppTheme_NoActionBar_095,
69              R.style.AppTheme_NoActionBar_100, R.style.AppTheme_NoActionBar_105,
70              R.style.AppTheme_NoActionBar_110, R.style.AppTheme_NoActionBar_115,
71              R.style.AppTheme_NoActionBar_120, R.style.AppTheme_NoActionBar_125,
72              R.style.AppTheme_NoActionBar_130, R.style.AppTheme_NoActionBar_135,
73              R.style.AppTheme_NoActionBar_140, R.style.AppTheme_NoActionBar_145,
74              R.style.AppTheme_NoActionBar_150, R.style.AppTheme_NoActionBar_155,
75              R.style.AppTheme_NoActionBar_160, R.style.AppTheme_NoActionBar_165,
76              R.style.AppTheme_NoActionBar_170, R.style.AppTheme_NoActionBar_175,
77              R.style.AppTheme_NoActionBar_180, R.style.AppTheme_NoActionBar_185,
78              R.style.AppTheme_NoActionBar_190, R.style.AppTheme_NoActionBar_195,
79              R.style.AppTheme_NoActionBar_200, R.style.AppTheme_NoActionBar_205,
80              R.style.AppTheme_NoActionBar_210, R.style.AppTheme_NoActionBar_215,
81              R.style.AppTheme_NoActionBar_220, R.style.AppTheme_NoActionBar_225,
82              R.style.AppTheme_NoActionBar_230, R.style.AppTheme_NoActionBar_235,
83              R.style.AppTheme_NoActionBar_240, R.style.AppTheme_NoActionBar_245,
84              R.style.AppTheme_NoActionBar_250, R.style.AppTheme_NoActionBar_255,
85              R.style.AppTheme_NoActionBar_260, R.style.AppTheme_NoActionBar_265,
86              R.style.AppTheme_NoActionBar_270, R.style.AppTheme_NoActionBar_275,
87              R.style.AppTheme_NoActionBar_280, R.style.AppTheme_NoActionBar_285,
88              R.style.AppTheme_NoActionBar_290, R.style.AppTheme_NoActionBar_295,
89              R.style.AppTheme_NoActionBar_300, R.style.AppTheme_NoActionBar_305,
90              R.style.AppTheme_NoActionBar_310, R.style.AppTheme_NoActionBar_315,
91              R.style.AppTheme_NoActionBar_320, R.style.AppTheme_NoActionBar_325,
92              R.style.AppTheme_NoActionBar_330, R.style.AppTheme_NoActionBar_335,
93              R.style.AppTheme_NoActionBar_340, R.style.AppTheme_NoActionBar_345,
94              R.style.AppTheme_NoActionBar_350, R.style.AppTheme_NoActionBar_355,
95              };
96     public static void refreshColors(Resources.Theme theme) {
97         TypedValue tv = new TypedValue();
98         theme.resolveAttribute(R.attr.table_row_dark_bg, tv, true);
99         tableRowDarkBG = tv.data;
100         theme.resolveAttribute(R.attr.table_row_light_bg, tv, true);
101         tableRowLightBG = tv.data;
102         theme.resolveAttribute(R.attr.colorPrimary, tv, true);
103         primary = tv.data;
104         theme.resolveAttribute(R.attr.textColor, tv, true);
105         defaultTextColor = tv.data;
106         theme.resolveAttribute(R.attr.colorAccent, tv, true);
107         accent = tv.data;
108
109         // trigger theme observers
110         themeWatch.postValue(themeWatch.getValue() + 1);
111     }
112     public static @ColorInt
113     int hslColor(float hueRatio, float saturation, float lightness) {
114         return 0xff000000 | hslTriplet(hueRatio, saturation, lightness);
115     }
116     public static @ColorInt
117     int hslTriplet(float hueRatio, float saturation, float lightness) {
118         @ColorLong long result;
119         float h = hueRatio * 6;
120         float c = (1 - abs(2f * lightness - 1)) * saturation;
121         float h_mod_2 = h % 2;
122         float x = c * (1 - Math.abs(h_mod_2 - 1));
123         int r, g, b;
124         float m = lightness - c / 2f;
125
126         if (h < 1 || h == 6)
127             return tupleToColor(c + m, x + m, 0 + m);
128         if (h < 2)
129             return tupleToColor(x + m, c + m, 0 + m);
130         if (h < 3)
131             return tupleToColor(0 + m, c + m, x + m);
132         if (h < 4)
133             return tupleToColor(0 + m, x + m, c + m);
134         if (h < 5)
135             return tupleToColor(x + m, 0 + m, c + m);
136         if (h < 6)
137             return tupleToColor(c + m, 0 + m, x + m);
138
139         throw new IllegalArgumentException(String.format(
140                 "Unexpected value for h (%1.3f) while converting hsl(%1.3f, %1.3f, %1.3f) to rgb",
141                 h, hueRatio, saturation, lightness));
142     }
143
144     public static @ColorInt
145     int tupleToColor(float r, float g, float b) {
146         int r_int = Math.round(255 * r);
147         int g_int = Math.round(255 * g);
148         int b_int = Math.round(255 * b);
149         return (r_int << 16) | (g_int << 8) | b_int;
150     }
151     public static @ColorInt
152     int getPrimaryColorForHue(int hueDegrees) {
153 //        int result = hsvColor(hueDegrees, 0.61f, 0.95f);
154         float y = hueDegrees - 60;
155         if (y < 0)
156             y += 360;
157         float l = yellowLightness + (blueLightness - yellowLightness) *
158                                     (float) Math.cos(Math.toRadians(Math.abs(180 - y) / 2f));
159         int result = hslColor(hueDegrees / 360f, 0.845f, l);
160         debug("colors", String.format(Locale.ENGLISH, "getPrimaryColorForHue(%d) = %x", hueDegrees,
161                 result));
162         return result;
163     }
164     public static void setupTheme(Activity activity) {
165         MobileLedgerProfile profile = Data.profile.getValue();
166         setupTheme(activity, profile);
167     }
168     public static void setupTheme(Activity activity, MobileLedgerProfile profile) {
169         final int themeHue = (profile == null) ? -1 : profile.getThemeHue();
170         setupTheme(activity, themeHue);
171     }
172     public static void setupTheme(Activity activity, int themeHue) {
173         int themeId = -1;
174         if (themeHue == 360)
175             themeHue = 0;
176         if ((themeHue >= 0) && (themeHue < 360)) {
177             int index;
178             if ((themeHue % HueRing.hueStepDegrees) != 0) {
179                 Logger.warn("profiles",
180                         String.format(Locale.US, "Adjusting unexpected hue %d", themeHue));
181                 index = Math.round(1f * themeHue / HueRing.hueStepDegrees);
182             }
183             else
184                 index = themeHue / HueRing.hueStepDegrees;
185
186             themeId = themeIDs[index];
187         }
188
189         if (themeId < 0) {
190             activity.setTheme(R.style.AppTheme_NoActionBar);
191             debug("profiles",
192                     String.format(Locale.ENGLISH, "Theme hue %d not supported, using the default",
193                             themeHue));
194         }
195         else {
196             activity.setTheme(themeId);
197         }
198
199         refreshColors(activity.getTheme());
200     }
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, getColors(hue));
209     }
210     public static int[] getColors() {
211         return getColors(profileThemeId);
212     }
213     public static int[] getColors(int hue) {
214         int[] colors = new int[]{0, 0, 0, 0, 0, 0};
215         for (int i = 0; i < 6; i++, hue = (hue + 60) % 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             hues.add(hues.get(0));
242
243             int lastHue = -1;
244             int largestInterval = 0;
245             ArrayList<Integer> largestIntervalStarts = new ArrayList<>();
246
247             for (int h : hues) {
248                 if (lastHue == -1) {
249                     lastHue = h;
250                     continue;
251                 }
252
253                 int interval;
254                 if (h > lastHue)
255                     interval = h - lastHue;     // 10 -> 20 is a step of 10
256                 else
257                     interval = h + (360 - lastHue);    // 350 -> 20 is a step of 30
258
259                 if (interval > largestInterval) {
260                     largestInterval = interval;
261                     largestIntervalStarts.clear();
262                     largestIntervalStarts.add(lastHue);
263                 }
264                 else if (interval == largestInterval) {
265                     largestIntervalStarts.add(lastHue);
266                 }
267
268                 lastHue = h;
269             }
270
271             final int chosenIndex = (int) (Math.random() * largestIntervalStarts.size());
272             int chosenIntervalStart = largestIntervalStarts.get(chosenIndex);
273
274             if (largestInterval % 2 != 0)
275                 largestInterval++;    // round up the middle point
276
277             chosenHue = (chosenIntervalStart + (largestInterval / 2)) % 360;
278         }
279
280         final int mod = chosenHue % THEME_HUE_STEP_DEG;
281         if (mod != 0) {
282             if (mod > THEME_HUE_STEP_DEG / 2)
283                 chosenHue += (THEME_HUE_STEP_DEG - mod); // 13 += (5-3) = 15
284             else
285                 chosenHue -= mod;       // 12 -= 2 = 10
286         }
287
288         return chosenHue;
289     }
290 }