]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/Colors.java
setupTheme routine with a profile argument
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / utils / Colors.java
1 package net.ktnx.mobileledger.utils;
2
3 import android.app.Activity;
4 import android.content.res.Resources;
5 import android.util.Log;
6 import android.util.TypedValue;
7
8 import net.ktnx.mobileledger.R;
9 import net.ktnx.mobileledger.model.Data;
10 import net.ktnx.mobileledger.model.MobileLedgerProfile;
11
12 import androidx.annotation.ColorInt;
13 import androidx.annotation.ColorLong;
14
15 import static java.lang.Math.abs;
16
17 public class Colors {
18     public static final int DEFAULT_HUE_DEG = 261;
19     public static @ColorInt
20     int accent;
21     @ColorInt
22     public static int tableRowLightBG;
23     @ColorInt
24     public static int tableRowDarkBG;
25     @ColorInt
26     public static int primary, defaultTextColor;
27     public static int profileThemeId = -1;
28
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);
37         primary = tv.data;
38         theme.resolveAttribute(R.attr.textColor, tv, true);
39         defaultTextColor = tv.data;
40         theme.resolveAttribute(R.attr.colorAccent, tv, true);
41         accent = tv.data;
42
43         // trigger theme observers
44         themeWatch.notifyObservers();
45     }
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");
50
51         @ColorLong long rgb = hsvTriplet(hue, saturation, value);
52
53         long a_bits = Math.round(255 * alpha);
54         return (a_bits << 24) | rgb;
55     }
56     public static @ColorInt
57     int hsvColor(float hue, float saturation, float value) {
58         return 0xff000000 | hsvTriplet(hue, saturation, value);
59     }
60     public static @ColorInt
61     int hslColor(float hue, float saturation, float lightness) {
62         return 0xff000000 | hslTriplet(hue, saturation, lightness);
63     }
64     public static @ColorInt
65     int hsvTriplet(float hue, float saturation, float value) {
66         @ColorLong long result;
67         int r, g, b;
68
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));
73
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);
79
80         switch (h) {
81             case 0:
82             case 6:
83                 return tupleToColor(value, t, p);
84             case 1:
85                 return tupleToColor(q, value, p);
86             case 2:
87                 return tupleToColor(p, value, t);
88             case 3:
89                 return tupleToColor(p, q, value);
90             case 4:
91                 return tupleToColor(t, p, value);
92             case 5:
93                 return tupleToColor(value, p, q);
94             default:
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));
98         }
99     }
100     public static @ColorInt
101     int hslTriplet(float hue, float saturation, float lightness) {
102         @ColorLong long result;
103         float h = hue * 6;
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));
107         int r, g, b;
108         float m = lightness - c / 2f;
109
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);
116
117         throw new IllegalArgumentException(String.format(
118                 "Unexpected value for h (%1.3f) while converting hsl(%1.3f, %1.3f, %1.3f) to rgb",
119                 h, hue, saturation, lightness));
120     }
121
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;
128     }
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);
134     }
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));
140         return result;
141     }
142     public static void setupTheme(Activity activity) {
143         MobileLedgerProfile profile = Data.profile.get();
144         setupTheme(activity, profile);
145     }
146     public static void setupTheme(Activity activity, MobileLedgerProfile profile) {
147         if (profile != null) {
148             final int themeId = profile.getThemeId();
149             switch (themeId) {
150                 case 0:
151                     activity.setTheme(R.style.AppTheme_NoActionBar_0);
152                     break;
153                 case 15:
154                     activity.setTheme(R.style.AppTheme_NoActionBar_15);
155                     break;
156                 case 30:
157                     activity.setTheme(R.style.AppTheme_NoActionBar_30);
158                     break;
159                 case 45:
160                     activity.setTheme(R.style.AppTheme_NoActionBar_45);
161                     break;
162                 case 60:
163                     activity.setTheme(R.style.AppTheme_NoActionBar_60);
164                     break;
165                 case 75:
166                     activity.setTheme(R.style.AppTheme_NoActionBar_75);
167                     break;
168                 case 90:
169                     activity.setTheme(R.style.AppTheme_NoActionBar_90);
170                     break;
171                 case 105:
172                     activity.setTheme(R.style.AppTheme_NoActionBar_105);
173                     break;
174                 case 120:
175                     activity.setTheme(R.style.AppTheme_NoActionBar_120);
176                     break;
177                 case 135:
178                     activity.setTheme(R.style.AppTheme_NoActionBar_135);
179                     break;
180                 case 150:
181                     activity.setTheme(R.style.AppTheme_NoActionBar_150);
182                     break;
183                 case 165:
184                     activity.setTheme(R.style.AppTheme_NoActionBar_165);
185                     break;
186                 case 180:
187                     activity.setTheme(R.style.AppTheme_NoActionBar_180);
188                     break;
189                 case 195:
190                     activity.setTheme(R.style.AppTheme_NoActionBar_195);
191                     break;
192                 case 210:
193                     activity.setTheme(R.style.AppTheme_NoActionBar_210);
194                     break;
195                 case 225:
196                     activity.setTheme(R.style.AppTheme_NoActionBar_225);
197                     break;
198                 case 240:
199                     activity.setTheme(R.style.AppTheme_NoActionBar_240);
200                     break;
201                 case 255:
202                     activity.setTheme(R.style.AppTheme_NoActionBar_255);
203                     break;
204                 case 270:
205                     activity.setTheme(R.style.AppTheme_NoActionBar_270);
206                     break;
207                 case 285:
208                     activity.setTheme(R.style.AppTheme_NoActionBar_285);
209                     break;
210                 case 300:
211                     activity.setTheme(R.style.AppTheme_NoActionBar_300);
212                     break;
213                 case 315:
214                     activity.setTheme(R.style.AppTheme_NoActionBar_315);
215                     break;
216                 case 330:
217                     activity.setTheme(R.style.AppTheme_NoActionBar_330);
218                     break;
219                 case 345:
220                     activity.setTheme(R.style.AppTheme_NoActionBar_345);
221                     break;
222                 default:
223                     activity.setTheme(R.style.AppTheme_NoActionBar);
224                     Log.d("profiles", String.format("Theme hue %d not supported, using the default",
225                             themeId));
226             }
227         }
228         else {
229             Log.d("profiles", "No profile given, using default theme");
230             activity.setTheme(R.style.AppTheme_NoActionBar);
231         }
232
233         refreshColors(activity.getTheme());
234     }
235
236 }