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.Locale;
37 import static java.lang.Math.abs;
38 import static net.ktnx.mobileledger.utils.Logger.debug;
41 public static final int DEFAULT_HUE_DEG = 261;
42 private static final float blueLightness = 0.665f;
43 private static final float yellowLightness = 0.350f;
44 private static final int[][] EMPTY_STATES = new int[][]{new int[0]};
45 public static @ColorInt
48 public static int tableRowLightBG;
50 public static int tableRowDarkBG;
52 public static int primary, defaultTextColor;
53 public static int profileThemeId = -1;
54 public static MutableLiveData<Integer> themeWatch = new MutableLiveData<>(0);
55 private static int[] themeIDs =
56 {R.style.AppTheme_NoActionBar_000, R.style.AppTheme_NoActionBar_005,
57 R.style.AppTheme_NoActionBar_010, R.style.AppTheme_NoActionBar_015,
58 R.style.AppTheme_NoActionBar_020, R.style.AppTheme_NoActionBar_025,
59 R.style.AppTheme_NoActionBar_030, R.style.AppTheme_NoActionBar_035,
60 R.style.AppTheme_NoActionBar_040, R.style.AppTheme_NoActionBar_045,
61 R.style.AppTheme_NoActionBar_050, R.style.AppTheme_NoActionBar_055,
62 R.style.AppTheme_NoActionBar_060, R.style.AppTheme_NoActionBar_065,
63 R.style.AppTheme_NoActionBar_070, R.style.AppTheme_NoActionBar_075,
64 R.style.AppTheme_NoActionBar_080, R.style.AppTheme_NoActionBar_085,
65 R.style.AppTheme_NoActionBar_090, R.style.AppTheme_NoActionBar_095,
66 R.style.AppTheme_NoActionBar_100, R.style.AppTheme_NoActionBar_105,
67 R.style.AppTheme_NoActionBar_110, R.style.AppTheme_NoActionBar_115,
68 R.style.AppTheme_NoActionBar_120, R.style.AppTheme_NoActionBar_125,
69 R.style.AppTheme_NoActionBar_130, R.style.AppTheme_NoActionBar_135,
70 R.style.AppTheme_NoActionBar_140, R.style.AppTheme_NoActionBar_145,
71 R.style.AppTheme_NoActionBar_150, R.style.AppTheme_NoActionBar_155,
72 R.style.AppTheme_NoActionBar_160, R.style.AppTheme_NoActionBar_165,
73 R.style.AppTheme_NoActionBar_170, R.style.AppTheme_NoActionBar_175,
74 R.style.AppTheme_NoActionBar_180, R.style.AppTheme_NoActionBar_185,
75 R.style.AppTheme_NoActionBar_190, R.style.AppTheme_NoActionBar_195,
76 R.style.AppTheme_NoActionBar_200, R.style.AppTheme_NoActionBar_205,
77 R.style.AppTheme_NoActionBar_210, R.style.AppTheme_NoActionBar_215,
78 R.style.AppTheme_NoActionBar_220, R.style.AppTheme_NoActionBar_225,
79 R.style.AppTheme_NoActionBar_230, R.style.AppTheme_NoActionBar_235,
80 R.style.AppTheme_NoActionBar_240, R.style.AppTheme_NoActionBar_245,
81 R.style.AppTheme_NoActionBar_250, R.style.AppTheme_NoActionBar_255,
82 R.style.AppTheme_NoActionBar_260, R.style.AppTheme_NoActionBar_265,
83 R.style.AppTheme_NoActionBar_270, R.style.AppTheme_NoActionBar_275,
84 R.style.AppTheme_NoActionBar_280, R.style.AppTheme_NoActionBar_285,
85 R.style.AppTheme_NoActionBar_290, R.style.AppTheme_NoActionBar_295,
86 R.style.AppTheme_NoActionBar_300, R.style.AppTheme_NoActionBar_305,
87 R.style.AppTheme_NoActionBar_310, R.style.AppTheme_NoActionBar_315,
88 R.style.AppTheme_NoActionBar_320, R.style.AppTheme_NoActionBar_325,
89 R.style.AppTheme_NoActionBar_330, R.style.AppTheme_NoActionBar_335,
90 R.style.AppTheme_NoActionBar_340, R.style.AppTheme_NoActionBar_345,
91 R.style.AppTheme_NoActionBar_350, R.style.AppTheme_NoActionBar_355,
93 public static void refreshColors(Resources.Theme theme) {
94 TypedValue tv = new TypedValue();
95 theme.resolveAttribute(R.attr.table_row_dark_bg, tv, true);
96 tableRowDarkBG = tv.data;
97 theme.resolveAttribute(R.attr.table_row_light_bg, tv, true);
98 tableRowLightBG = tv.data;
99 theme.resolveAttribute(R.attr.colorPrimary, tv, true);
101 theme.resolveAttribute(R.attr.textColor, tv, true);
102 defaultTextColor = tv.data;
103 theme.resolveAttribute(R.attr.colorAccent, tv, true);
106 // trigger theme observers
107 themeWatch.postValue(themeWatch.getValue() + 1);
109 public static @ColorLong
110 long hsvaColor(float hue, float saturation, float value, float alpha) {
111 if (alpha < 0 || alpha > 1)
112 throw new IllegalArgumentException("alpha must be between 0 and 1");
114 @ColorLong long rgb = hsvTriplet(hue, saturation, value);
116 long a_bits = Math.round(255 * alpha);
117 return (a_bits << 24) | rgb;
119 public static @ColorInt
120 int hsvColor(float hue, float saturation, float value) {
121 return 0xff000000 | hsvTriplet(hue, saturation, value);
123 public static @ColorInt
124 int hslColor(float hueRatio, float saturation, float lightness) {
125 return 0xff000000 | hslTriplet(hueRatio, saturation, lightness);
127 public static @ColorInt
128 int hsvTriplet(float hue, float saturation, float value) {
129 @ColorLong long result;
132 if ((hue < -0.00005) || (hue > 1.0000005) || (saturation < 0) || (saturation > 1) ||
133 (value < 0) || (value > 1)) throw new IllegalArgumentException(String.format(
134 "hue, saturation, value and alpha must all be between 0 and 1. Arguments given: " +
135 "hue=%1.5f, sat=%1.5f, val=%1.5f", hue, saturation, value));
137 int h = (int) (hue * 6);
138 float f = hue * 6 - h;
139 float p = value * (1 - saturation);
140 float q = value * (1 - f * saturation);
141 float t = value * (1 - (1 - f) * saturation);
146 return tupleToColor(value, t, p);
148 return tupleToColor(q, value, p);
150 return tupleToColor(p, value, t);
152 return tupleToColor(p, q, value);
154 return tupleToColor(t, p, value);
156 return tupleToColor(value, p, q);
158 throw new RuntimeException(String.format("Unexpected value for h (%d) while " +
159 "converting hsv(%1.2f, %1.2f, %1.2f) to " +
160 "rgb", h, hue, saturation, value));
163 public static @ColorInt
164 int hslTriplet(float hueRatio, float saturation, float lightness) {
165 @ColorLong long result;
166 float h = hueRatio * 6;
167 float c = (1 - abs(2f * lightness - 1)) * saturation;
168 float h_mod_2 = h % 2;
169 float x = c * (1 - Math.abs(h_mod_2 - 1));
171 float m = lightness - c / 2f;
173 if (h < 1 || h == 6) return tupleToColor(c + m, x + m, 0 + m);
174 if (h < 2) return tupleToColor(x + m, c + m, 0 + m);
175 if (h < 3) return tupleToColor(0 + m, c + m, x + m);
176 if (h < 4) return tupleToColor(0 + m, x + m, c + m);
177 if (h < 5) return tupleToColor(x + m, 0 + m, c + m);
178 if (h < 6) return tupleToColor(c + m, 0 + m, x + m);
180 throw new IllegalArgumentException(String.format(
181 "Unexpected value for h (%1.3f) while converting hsl(%1.3f, %1.3f, %1.3f) to rgb",
182 h, hueRatio, saturation, lightness));
185 public static @ColorInt
186 int tupleToColor(float r, float g, float b) {
187 int r_int = Math.round(255 * r);
188 int g_int = Math.round(255 * g);
189 int b_int = Math.round(255 * b);
190 return (r_int << 16) | (g_int << 8) | b_int;
192 public static @ColorInt
193 int getPrimaryColorForHue(int hueDegrees) {
194 // int result = hsvColor(hueDegrees, 0.61f, 0.95f);
195 float y = hueDegrees - 60;
197 float l = yellowLightness + (blueLightness - yellowLightness) *
198 (float) Math.cos(Math.toRadians(Math.abs(180 - y) / 2f));
199 int result = hslColor(hueDegrees / 360f, 0.845f, l);
200 debug("colors", String.format(Locale.ENGLISH, "getPrimaryColorForHue(%d) = %x", hueDegrees,
204 public static void setupTheme(Activity activity) {
205 MobileLedgerProfile profile = Data.profile.getValue();
206 setupTheme(activity, profile);
208 public static void setupTheme(Activity activity, MobileLedgerProfile profile) {
209 final int themeHue = (profile == null) ? -1 : profile.getThemeId();
210 setupTheme(activity, themeHue);
212 public static void setupTheme(Activity activity, int themeHue) {
214 // Relies that theme resource IDs are sequential numbers
215 if (themeHue == 360) themeHue = 0;
216 if ((themeHue >= 0) && (themeHue < 360) && ((themeHue % HueRing.hueStepDegrees) == 0)) {
217 themeId = themeIDs[themeHue / HueRing.hueStepDegrees];
221 activity.setTheme(R.style.AppTheme_NoActionBar);
223 String.format(Locale.ENGLISH, "Theme hue %d not supported, using the default",
227 activity.setTheme(themeId);
230 refreshColors(activity.getTheme());
233 public static @NonNull
234 ColorStateList getColorStateList() {
235 return getColorStateList(profileThemeId);
237 public static @NonNull
238 ColorStateList getColorStateList(int hue) {
239 return new ColorStateList(EMPTY_STATES, getColors(hue));
241 public static int[] getColors() {
242 return getColors(profileThemeId);
244 public static int[] getColors(int hue) {
245 int[] colors = new int[]{0, 0, 0, 0, 0, 0};
246 for (int i = 0; i < 6; i++, hue = (hue + 60) % 360) {
247 colors[i] = getPrimaryColorForHue(hue);