1 package net.ktnx.mobileledger.utils;
3 import android.app.Activity;
4 import android.content.res.Resources;
5 import androidx.annotation.ColorInt;
6 import androidx.annotation.ColorLong;
7 import android.util.Log;
8 import android.util.TypedValue;
10 import net.ktnx.mobileledger.R;
11 import net.ktnx.mobileledger.model.Data;
12 import net.ktnx.mobileledger.model.MobileLedgerProfile;
15 public static final int DEFAULT_HUE_DEG = 261;
16 public static @ColorInt
19 public static int tableRowLightBG;
21 public static int tableRowDarkBG;
23 public static int primary, defaultTextColor;
24 public static int profileThemeId = -1;
26 public static ObservableValue<Integer> themeWatch = new ObservableValue<>(0);
27 public static void refreshColors(Resources.Theme theme) {
28 TypedValue tv = new TypedValue();
29 theme.resolveAttribute(R.attr.table_row_dark_bg, tv, true);
30 tableRowDarkBG = tv.data;
31 theme.resolveAttribute(R.attr.table_row_light_bg, tv, true);
32 tableRowLightBG = tv.data;
33 theme.resolveAttribute(R.attr.colorPrimary, tv, true);
35 theme.resolveAttribute(android.R.color.tab_indicator_text, tv, true);
36 defaultTextColor = tv.data;
37 theme.resolveAttribute(R.attr.colorAccent, tv, true);
40 // trigger theme observers
41 themeWatch.notifyObservers();
43 public static @ColorLong
44 long hsvaColor(float hue, float saturation, float value, float alpha) {
45 if (alpha < 0 || alpha > 1)
46 throw new IllegalArgumentException("alpha must be between 0 and 1");
48 @ColorLong long rgb = hsvTriplet(hue, saturation, value);
50 long a_bits = Math.round(255 * alpha);
51 return (a_bits << 24) | rgb;
53 public static @ColorInt
54 int hsvColor(float hue, float saturation, float value) {
55 return 0xff000000 | hsvTriplet(hue, saturation, value);
57 public static @ColorInt
58 int hsvTriplet(float hue, float saturation, float value) {
59 @ColorLong long result;
62 if ((hue < 0) || (hue > 1) || (saturation < 0) || (saturation > 1) || (value < 0) ||
63 (value > 1)) throw new IllegalArgumentException(
64 "hue, saturation, value and alpha must all be between 0 and 1");
66 int h = (int) (hue * 6);
67 float f = hue * 6 - h;
68 float p = value * (1 - saturation);
69 float q = value * (1 - f * saturation);
70 float t = value * (1 - (1 - f) * saturation);
75 return tupleToColor(value, t, p);
77 return tupleToColor(q, value, p);
79 return tupleToColor(p, value, t);
81 return tupleToColor(p, q, value);
83 return tupleToColor(t, p, value);
85 return tupleToColor(value, p, q);
87 throw new RuntimeException(String.format("Unexpected value for h (%d) while " +
88 "converting hsv(%1.2f, %1.2f, %1.2f) to " +
89 "rgb", h, hue, saturation, value));
93 public static @ColorInt
94 int tupleToColor(float r, float g, float b) {
95 int r_int = Math.round(255 * r);
96 int g_int = Math.round(255 * g);
97 int b_int = Math.round(255 * b);
98 return (r_int << 16) | (g_int << 8) | b_int;
100 public static @ColorInt
101 int getPrimaryColorForHue(int degrees) {
102 return getPrimaryColorForHue(degrees / 360f);
104 public static @ColorInt
105 int getPrimaryColorForHue(float hue) {
106 int result = hsvColor(hue, 0.61f, 0.95f);
107 Log.d("colors", String.format("getPrimaryColorForHue(%1.2f) = %x", hue, result));
110 public static void setupTheme(Activity activity) {
111 MobileLedgerProfile profile = Data.profile.get();
112 if (profile != null) {
113 switch (Data.profile.get().getThemeId()) {
115 activity.setTheme(R.style.AppTheme_NoActionBar_0);
118 activity.setTheme(R.style.AppTheme_NoActionBar_15);
121 activity.setTheme(R.style.AppTheme_NoActionBar_30);
124 activity.setTheme(R.style.AppTheme_NoActionBar_45);
127 activity.setTheme(R.style.AppTheme_NoActionBar_60);
130 activity.setTheme(R.style.AppTheme_NoActionBar_75);
133 activity.setTheme(R.style.AppTheme_NoActionBar_90);
136 activity.setTheme(R.style.AppTheme_NoActionBar_105);
139 activity.setTheme(R.style.AppTheme_NoActionBar_120);
142 activity.setTheme(R.style.AppTheme_NoActionBar_135);
145 activity.setTheme(R.style.AppTheme_NoActionBar_150);
148 activity.setTheme(R.style.AppTheme_NoActionBar_165);
151 activity.setTheme(R.style.AppTheme_NoActionBar_180);
154 activity.setTheme(R.style.AppTheme_NoActionBar_195);
157 activity.setTheme(R.style.AppTheme_NoActionBar_210);
160 activity.setTheme(R.style.AppTheme_NoActionBar_225);
163 activity.setTheme(R.style.AppTheme_NoActionBar_240);
166 activity.setTheme(R.style.AppTheme_NoActionBar_255);
169 activity.setTheme(R.style.AppTheme_NoActionBar_270);
172 activity.setTheme(R.style.AppTheme_NoActionBar_285);
175 activity.setTheme(R.style.AppTheme_NoActionBar_300);
178 activity.setTheme(R.style.AppTheme_NoActionBar_315);
181 activity.setTheme(R.style.AppTheme_NoActionBar_330);
184 activity.setTheme(R.style.AppTheme_NoActionBar_345);
187 activity.setTheme(R.style.AppTheme_NoActionBar);
190 else activity.setTheme(R.style.AppTheme_NoActionBar);
192 refreshColors(activity.getTheme());