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.
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.
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/>.
18 package net.ktnx.mobileledger.utils;
20 import android.app.Activity;
21 import android.content.res.ColorStateList;
22 import android.content.res.Resources;
23 import android.util.TypedValue;
25 import androidx.annotation.ColorInt;
26 import androidx.annotation.ColorLong;
27 import androidx.annotation.NonNull;
28 import androidx.lifecycle.MutableLiveData;
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;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.Locale;
39 import static java.lang.Math.abs;
40 import static net.ktnx.mobileledger.utils.Logger.debug;
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
51 public static int tableRowLightBG;
53 public static int tableRowDarkBG;
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,
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);
104 theme.resolveAttribute(R.attr.textColor, tv, true);
105 defaultTextColor = tv.data;
106 theme.resolveAttribute(R.attr.colorAccent, tv, true);
109 // trigger theme observers
110 themeWatch.postValue(themeWatch.getValue() + 1);
112 public static @ColorLong
113 long hsvaColor(float hue, float saturation, float value, float alpha) {
114 if (alpha < 0 || alpha > 1)
115 throw new IllegalArgumentException("alpha must be between 0 and 1");
117 @ColorLong long rgb = hsvTriplet(hue, saturation, value);
119 long a_bits = Math.round(255 * alpha);
120 return (a_bits << 24) | rgb;
122 public static @ColorInt
123 int hsvColor(float hue, float saturation, float value) {
124 return 0xff000000 | hsvTriplet(hue, saturation, value);
126 public static @ColorInt
127 int hslColor(float hueRatio, float saturation, float lightness) {
128 return 0xff000000 | hslTriplet(hueRatio, saturation, lightness);
130 public static @ColorInt
131 int hsvTriplet(float hue, float saturation, float value) {
132 @ColorLong long result;
135 if ((hue < -0.00005) || (hue > 1.0000005) || (saturation < 0) || (saturation > 1) ||
136 (value < 0) || (value > 1)) throw new IllegalArgumentException(String.format(
137 "hue, saturation, value and alpha must all be between 0 and 1. Arguments given: " +
138 "hue=%1.5f, sat=%1.5f, val=%1.5f", hue, saturation, value));
140 int h = (int) (hue * 6);
141 float f = hue * 6 - h;
142 float p = value * (1 - saturation);
143 float q = value * (1 - f * saturation);
144 float t = value * (1 - (1 - f) * saturation);
149 return tupleToColor(value, t, p);
151 return tupleToColor(q, value, p);
153 return tupleToColor(p, value, t);
155 return tupleToColor(p, q, value);
157 return tupleToColor(t, p, value);
159 return tupleToColor(value, p, q);
161 throw new RuntimeException(String.format("Unexpected value for h (%d) while " +
162 "converting hsv(%1.2f, %1.2f, %1.2f) to " +
163 "rgb", h, hue, saturation, value));
166 public static @ColorInt
167 int hslTriplet(float hueRatio, float saturation, float lightness) {
168 @ColorLong long result;
169 float h = hueRatio * 6;
170 float c = (1 - abs(2f * lightness - 1)) * saturation;
171 float h_mod_2 = h % 2;
172 float x = c * (1 - Math.abs(h_mod_2 - 1));
174 float m = lightness - c / 2f;
176 if (h < 1 || h == 6) return tupleToColor(c + m, x + m, 0 + m);
177 if (h < 2) return tupleToColor(x + m, c + m, 0 + m);
178 if (h < 3) return tupleToColor(0 + m, c + m, x + m);
179 if (h < 4) return tupleToColor(0 + m, x + m, c + m);
180 if (h < 5) return tupleToColor(x + m, 0 + m, c + m);
181 if (h < 6) return tupleToColor(c + m, 0 + m, x + m);
183 throw new IllegalArgumentException(String.format(
184 "Unexpected value for h (%1.3f) while converting hsl(%1.3f, %1.3f, %1.3f) to rgb",
185 h, hueRatio, saturation, lightness));
188 public static @ColorInt
189 int tupleToColor(float r, float g, float b) {
190 int r_int = Math.round(255 * r);
191 int g_int = Math.round(255 * g);
192 int b_int = Math.round(255 * b);
193 return (r_int << 16) | (g_int << 8) | b_int;
195 public static @ColorInt
196 int getPrimaryColorForHue(int hueDegrees) {
197 // int result = hsvColor(hueDegrees, 0.61f, 0.95f);
198 float y = hueDegrees - 60;
200 float l = yellowLightness + (blueLightness - yellowLightness) *
201 (float) Math.cos(Math.toRadians(Math.abs(180 - y) / 2f));
202 int result = hslColor(hueDegrees / 360f, 0.845f, l);
203 debug("colors", String.format(Locale.ENGLISH, "getPrimaryColorForHue(%d) = %x", hueDegrees,
207 public static void setupTheme(Activity activity) {
208 MobileLedgerProfile profile = Data.profile.getValue();
209 setupTheme(activity, profile);
211 public static void setupTheme(Activity activity, MobileLedgerProfile profile) {
212 final int themeHue = (profile == null) ? -1 : profile.getThemeHue();
213 setupTheme(activity, themeHue);
215 public static void setupTheme(Activity activity, int themeHue) {
217 // Relies that theme resource IDs are sequential numbers
218 if (themeHue == 360) themeHue = 0;
219 if ((themeHue >= 0) && (themeHue < 360) && ((themeHue % HueRing.hueStepDegrees) == 0)) {
220 themeId = themeIDs[themeHue / HueRing.hueStepDegrees];
224 activity.setTheme(R.style.AppTheme_NoActionBar);
226 String.format(Locale.ENGLISH, "Theme hue %d not supported, using the default",
230 activity.setTheme(themeId);
233 refreshColors(activity.getTheme());
236 public static @NonNull
237 ColorStateList getColorStateList() {
238 return getColorStateList(profileThemeId);
240 public static @NonNull
241 ColorStateList getColorStateList(int hue) {
242 return new ColorStateList(EMPTY_STATES, getColors(hue));
244 public static int[] getColors() {
245 return getColors(profileThemeId);
247 public static int[] getColors(int hue) {
248 int[] colors = new int[]{0, 0, 0, 0, 0, 0};
249 for (int i = 0; i < 6; i++, hue = (hue + 60) % 360) {
250 colors[i] = getPrimaryColorForHue(hue);
254 public static int getNewProfileThemeHue(ArrayList<MobileLedgerProfile> profiles) {
255 if ((profiles == null) || (profiles.size() == 0))
256 return DEFAULT_HUE_DEG;
258 if (profiles.size() == 1) {
259 int opposite = profiles.get(0)
260 .getThemeHue() + 180;
265 ArrayList<Integer> hues = new ArrayList<>();
266 for (MobileLedgerProfile p : profiles) {
267 int hue = p.getThemeHue();
269 hue = DEFAULT_HUE_DEG;
272 Collections.sort(hues);
273 hues.add(hues.get(0));
276 int largestInterval = 0;
277 ArrayList<Integer> largestIntervalStarts = new ArrayList<>();
287 interval = h - lastHue; // 10 -> 20 is a step of 10
289 interval = h + (360 - lastHue); // 350 -> 20 is a step of 30
291 if (interval > largestInterval) {
292 largestInterval = interval;
293 largestIntervalStarts.clear();
294 largestIntervalStarts.add(lastHue);
296 else if (interval == largestInterval) {
297 largestIntervalStarts.add(lastHue);
303 final int chosenIndex = (int) (Math.random() * largestIntervalStarts.size());
304 int chosenIntervalStart = largestIntervalStarts.get(chosenIndex);
306 if (largestInterval % 2 != 0)
307 largestInterval++; // round up the middle point
308 int chosenHue = (chosenIntervalStart + (largestInterval / 2)) % 360;
310 final int mod = chosenHue % THEME_HUE_STEP_DEG;
312 if (mod > THEME_HUE_STEP_DEG / 2)
313 chosenHue += (THEME_HUE_STEP_DEG - mod); // 13 += (5-3) = 15
315 chosenHue -= mod; // 12 -= 2 = 10