1 package net.ktnx.mobileledger.utils;
3 import android.app.Activity;
4 import android.content.res.Resources;
5 import android.util.Log;
6 import android.util.TypedValue;
8 import net.ktnx.mobileledger.R;
9 import net.ktnx.mobileledger.model.Data;
10 import net.ktnx.mobileledger.model.MobileLedgerProfile;
12 import androidx.annotation.ColorInt;
13 import androidx.annotation.ColorLong;
15 import static java.lang.Math.abs;
18 public static final int DEFAULT_HUE_DEG = 261;
19 public static @ColorInt
22 public static int tableRowLightBG;
24 public static int tableRowDarkBG;
26 public static int primary, defaultTextColor;
27 public static int profileThemeId = -1;
29 public static ObservableValue<Integer> themeWatch = new ObservableValue<>(0);
30 public static void refreshColors(Resources.Theme theme) {
31 TypedValue tv = new TypedValue();
32 theme.resolveAttribute(R.attr.table_row_dark_bg, tv, true);
33 tableRowDarkBG = tv.data;
34 theme.resolveAttribute(R.attr.table_row_light_bg, tv, true);
35 tableRowLightBG = tv.data;
36 theme.resolveAttribute(R.attr.colorPrimary, tv, true);
38 theme.resolveAttribute(R.attr.textColor, tv, true);
39 defaultTextColor = tv.data;
40 theme.resolveAttribute(R.attr.colorAccent, tv, true);
43 // trigger theme observers
44 themeWatch.notifyObservers();
46 public static @ColorLong
47 long hsvaColor(float hue, float saturation, float value, float alpha) {
48 if (alpha < 0 || alpha > 1)
49 throw new IllegalArgumentException("alpha must be between 0 and 1");
51 @ColorLong long rgb = hsvTriplet(hue, saturation, value);
53 long a_bits = Math.round(255 * alpha);
54 return (a_bits << 24) | rgb;
56 public static @ColorInt
57 int hsvColor(float hue, float saturation, float value) {
58 return 0xff000000 | hsvTriplet(hue, saturation, value);
60 public static @ColorInt
61 int hslColor(float hue, float saturation, float lightness) {
62 return 0xff000000 | hslTriplet(hue, saturation, lightness);
64 public static @ColorInt
65 int hsvTriplet(float hue, float saturation, float value) {
66 @ColorLong long result;
69 if ((hue < -0.00005) || (hue > 1.0000005) || (saturation < 0) || (saturation > 1) ||
70 (value < 0) || (value > 1)) throw new IllegalArgumentException(String.format(
71 "hue, saturation, value and alpha must all be between 0 and 1. Arguments given: " +
72 "hue=%1.5f, sat=%1.5f, val=%1.5f", hue, saturation, value));
74 int h = (int) (hue * 6);
75 float f = hue * 6 - h;
76 float p = value * (1 - saturation);
77 float q = value * (1 - f * saturation);
78 float t = value * (1 - (1 - f) * saturation);
83 return tupleToColor(value, t, p);
85 return tupleToColor(q, value, p);
87 return tupleToColor(p, value, t);
89 return tupleToColor(p, q, value);
91 return tupleToColor(t, p, value);
93 return tupleToColor(value, p, q);
95 throw new RuntimeException(String.format("Unexpected value for h (%d) while " +
96 "converting hsv(%1.2f, %1.2f, %1.2f) to " +
97 "rgb", h, hue, saturation, value));
100 public static @ColorInt
101 int hslTriplet(float hue, float saturation, float lightness) {
102 @ColorLong long result;
104 float c = (1 - abs(2f * lightness - 1)) * saturation;
105 float h_mod_2 = h % 2;
106 float x = c * (1 - Math.abs(h_mod_2 - 1));
108 float m = lightness - c / 2f;
110 if (h < 1 || h == 6) return tupleToColor(c + m, x + m, 0 + m);
111 if (h < 2) return tupleToColor(x + m, c + m, 0 + m);
112 if (h < 3) return tupleToColor(0 + m, c + m, x + m);
113 if (h < 4) return tupleToColor(0 + m, x + m, c + m);
114 if (h < 5) return tupleToColor(x + m, 0 + m, c + m);
115 if (h < 6) return tupleToColor(c + m, 0 + m, x + m);
117 throw new IllegalArgumentException(String.format(
118 "Unexpected value for h (%d) while converting hsl(%1.2f, %1.2f, %1.2f) to rgb", h,
119 hue, saturation, lightness));
122 public static @ColorInt
123 int tupleToColor(float r, float g, float b) {
124 int r_int = Math.round(255 * r);
125 int g_int = Math.round(255 * g);
126 int b_int = Math.round(255 * b);
127 return (r_int << 16) | (g_int << 8) | b_int;
129 public static @ColorInt
130 int getPrimaryColorForHue(int degrees) {
131 // 0/360f becomes -0.000something for some reason
132 if (degrees == 0) return getPrimaryColorForHue(0f);
133 return getPrimaryColorForHue(degrees / 360f);
135 public static @ColorInt
136 int getPrimaryColorForHue(float hue) {
137 // int result = hsvColor(hue, 0.61f, 0.95f);
138 int result = hslColor(hue, 0.60f, 0.60f);
139 Log.d("colors", String.format("getPrimaryColorForHue(%1.2f) = %x", hue, result));
142 public static void setupTheme(Activity activity) {
143 MobileLedgerProfile profile = Data.profile.get();
144 if (profile != null) {
145 switch (Data.profile.get().getThemeId()) {
147 activity.setTheme(R.style.AppTheme_NoActionBar_0);
150 activity.setTheme(R.style.AppTheme_NoActionBar_15);
153 activity.setTheme(R.style.AppTheme_NoActionBar_30);
156 activity.setTheme(R.style.AppTheme_NoActionBar_45);
159 activity.setTheme(R.style.AppTheme_NoActionBar_60);
162 activity.setTheme(R.style.AppTheme_NoActionBar_75);
165 activity.setTheme(R.style.AppTheme_NoActionBar_90);
168 activity.setTheme(R.style.AppTheme_NoActionBar_105);
171 activity.setTheme(R.style.AppTheme_NoActionBar_120);
174 activity.setTheme(R.style.AppTheme_NoActionBar_135);
177 activity.setTheme(R.style.AppTheme_NoActionBar_150);
180 activity.setTheme(R.style.AppTheme_NoActionBar_165);
183 activity.setTheme(R.style.AppTheme_NoActionBar_180);
186 activity.setTheme(R.style.AppTheme_NoActionBar_195);
189 activity.setTheme(R.style.AppTheme_NoActionBar_210);
192 activity.setTheme(R.style.AppTheme_NoActionBar_225);
195 activity.setTheme(R.style.AppTheme_NoActionBar_240);
198 activity.setTheme(R.style.AppTheme_NoActionBar_255);
201 activity.setTheme(R.style.AppTheme_NoActionBar_270);
204 activity.setTheme(R.style.AppTheme_NoActionBar_285);
207 activity.setTheme(R.style.AppTheme_NoActionBar_300);
210 activity.setTheme(R.style.AppTheme_NoActionBar_315);
213 activity.setTheme(R.style.AppTheme_NoActionBar_330);
216 activity.setTheme(R.style.AppTheme_NoActionBar_345);
219 activity.setTheme(R.style.AppTheme_NoActionBar);
222 else activity.setTheme(R.style.AppTheme_NoActionBar);
224 refreshColors(activity.getTheme());