1 package net.ktnx.mobileledger.utils;
3 import android.app.Activity;
4 import android.content.res.Resources;
5 import android.support.annotation.ColorInt;
6 import android.support.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);
36 // trigger theme observers
37 themeWatch.notifyObservers();
39 public static @ColorLong
40 long hsvaColor(float hue, float saturation, float value, float alpha) {
41 if (alpha < 0 || alpha > 1)
42 throw new IllegalArgumentException("alpha must be between 0 and 1");
44 @ColorLong long rgb = hsvTriplet(hue, saturation, value);
46 long a_bits = Math.round(255 * alpha);
47 return (a_bits << 24) | rgb;
49 public static @ColorInt
50 int hsvColor(float hue, float saturation, float value) {
51 return 0xff000000 | hsvTriplet(hue, saturation, value);
53 public static @ColorInt
54 int hsvTriplet(float hue, float saturation, float value) {
55 @ColorLong long result;
58 if ((hue < 0) || (hue > 1) || (saturation < 0) || (saturation > 1) || (value < 0) ||
59 (value > 1)) throw new IllegalArgumentException(
60 "hue, saturation, value and alpha must all be between 0 and 1");
62 int h = (int) (hue * 6);
63 float f = hue * 6 - h;
64 float p = value * (1 - saturation);
65 float q = value * (1 - f * saturation);
66 float t = value * (1 - (1 - f) * saturation);
71 return tupleToColor(value, t, p);
73 return tupleToColor(q, value, p);
75 return tupleToColor(p, value, t);
77 return tupleToColor(p, q, value);
79 return tupleToColor(t, p, value);
81 return tupleToColor(value, p, q);
83 throw new RuntimeException(String.format("Unexpected value for h (%d) while " +
84 "converting hsv(%1.2f, %1.2f, %1.2f) to " +
85 "rgb", h, hue, saturation, value));
89 public static @ColorInt
90 int tupleToColor(float r, float g, float b) {
91 int r_int = Math.round(255 * r);
92 int g_int = Math.round(255 * g);
93 int b_int = Math.round(255 * b);
94 return (r_int << 16) | (g_int << 8) | b_int;
96 public static @ColorInt
97 int getPrimaryColorForHue(int degrees) {
98 return getPrimaryColorForHue(degrees / 360f);
100 public static @ColorInt
101 int getPrimaryColorForHue(float hue) {
102 int result = hsvColor(hue, 0.61f, 0.95f);
103 Log.d("colors", String.format("getPrimaryColorForHue(%1.2f) = %x", hue, result));
106 public static void setupTheme(Activity activity) {
107 MobileLedgerProfile profile = Data.profile.get();
108 if (profile != null) {
109 switch (Data.profile.get().getThemeId()) {
111 activity.setTheme(R.style.AppTheme_NoActionBar_0);
114 activity.setTheme(R.style.AppTheme_NoActionBar_15);
117 activity.setTheme(R.style.AppTheme_NoActionBar_30);
120 activity.setTheme(R.style.AppTheme_NoActionBar_45);
123 activity.setTheme(R.style.AppTheme_NoActionBar_60);
126 activity.setTheme(R.style.AppTheme_NoActionBar_75);
129 activity.setTheme(R.style.AppTheme_NoActionBar_90);
132 activity.setTheme(R.style.AppTheme_NoActionBar_105);
135 activity.setTheme(R.style.AppTheme_NoActionBar_120);
138 activity.setTheme(R.style.AppTheme_NoActionBar_135);
141 activity.setTheme(R.style.AppTheme_NoActionBar_150);
144 activity.setTheme(R.style.AppTheme_NoActionBar_165);
147 activity.setTheme(R.style.AppTheme_NoActionBar_180);
150 activity.setTheme(R.style.AppTheme_NoActionBar_195);
153 activity.setTheme(R.style.AppTheme_NoActionBar_210);
156 activity.setTheme(R.style.AppTheme_NoActionBar_225);
159 activity.setTheme(R.style.AppTheme_NoActionBar_240);
162 activity.setTheme(R.style.AppTheme_NoActionBar_255);
165 activity.setTheme(R.style.AppTheme_NoActionBar_270);
168 activity.setTheme(R.style.AppTheme_NoActionBar_285);
171 activity.setTheme(R.style.AppTheme_NoActionBar_300);
174 activity.setTheme(R.style.AppTheme_NoActionBar_315);
177 activity.setTheme(R.style.AppTheme_NoActionBar_330);
180 activity.setTheme(R.style.AppTheme_NoActionBar_345);
183 activity.setTheme(R.style.AppTheme_NoActionBar);
186 else activity.setTheme(R.style.AppTheme_NoActionBar);
188 refreshColors(activity.getTheme());