]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/Colors.java
Colors: also set defaultTextColor and accent
[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.support.annotation.ColorInt;
6 import android.support.annotation.ColorLong;
7 import android.util.Log;
8 import android.util.TypedValue;
9
10 import net.ktnx.mobileledger.R;
11 import net.ktnx.mobileledger.model.Data;
12 import net.ktnx.mobileledger.model.MobileLedgerProfile;
13
14 public class Colors {
15     public static final int DEFAULT_HUE_DEG = 261;
16     public static @ColorInt
17     int accent;
18     @ColorInt
19     public static int tableRowLightBG;
20     @ColorInt
21     public static int tableRowDarkBG;
22     @ColorInt
23     public static int primary, defaultTextColor;
24     public static int profileThemeId = -1;
25
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);
34         primary = tv.data;
35         theme.resolveAttribute(android.R.color.tab_indicator_text, tv, true);
36         defaultTextColor = tv.data;
37         theme.resolveAttribute(R.attr.colorAccent, tv, true);
38         accent = tv.data;
39
40         // trigger theme observers
41         themeWatch.notifyObservers();
42     }
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");
47
48         @ColorLong long rgb = hsvTriplet(hue, saturation, value);
49
50         long a_bits = Math.round(255 * alpha);
51         return (a_bits << 24) | rgb;
52     }
53     public static @ColorInt
54     int hsvColor(float hue, float saturation, float value) {
55         return 0xff000000 | hsvTriplet(hue, saturation, value);
56     }
57     public static @ColorInt
58     int hsvTriplet(float hue, float saturation, float value) {
59         @ColorLong long result;
60         int r, g, b;
61
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");
65
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);
71
72         switch (h) {
73             case 0:
74             case 6:
75                 return tupleToColor(value, t, p);
76             case 1:
77                 return tupleToColor(q, value, p);
78             case 2:
79                 return tupleToColor(p, value, t);
80             case 3:
81                 return tupleToColor(p, q, value);
82             case 4:
83                 return tupleToColor(t, p, value);
84             case 5:
85                 return tupleToColor(value, p, q);
86             default:
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));
90         }
91     }
92
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;
99     }
100     public static @ColorInt
101     int getPrimaryColorForHue(int degrees) {
102         return getPrimaryColorForHue(degrees / 360f);
103     }
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));
108         return result;
109     }
110     public static void setupTheme(Activity activity) {
111         MobileLedgerProfile profile = Data.profile.get();
112         if (profile != null) {
113             switch (Data.profile.get().getThemeId()) {
114                 case 0:
115                     activity.setTheme(R.style.AppTheme_NoActionBar_0);
116                     break;
117                 case 15:
118                     activity.setTheme(R.style.AppTheme_NoActionBar_15);
119                     break;
120                 case 30:
121                     activity.setTheme(R.style.AppTheme_NoActionBar_30);
122                     break;
123                 case 45:
124                     activity.setTheme(R.style.AppTheme_NoActionBar_45);
125                     break;
126                 case 60:
127                     activity.setTheme(R.style.AppTheme_NoActionBar_60);
128                     break;
129                 case 75:
130                     activity.setTheme(R.style.AppTheme_NoActionBar_75);
131                     break;
132                 case 90:
133                     activity.setTheme(R.style.AppTheme_NoActionBar_90);
134                     break;
135                 case 105:
136                     activity.setTheme(R.style.AppTheme_NoActionBar_105);
137                     break;
138                 case 120:
139                     activity.setTheme(R.style.AppTheme_NoActionBar_120);
140                     break;
141                 case 135:
142                     activity.setTheme(R.style.AppTheme_NoActionBar_135);
143                     break;
144                 case 150:
145                     activity.setTheme(R.style.AppTheme_NoActionBar_150);
146                     break;
147                 case 165:
148                     activity.setTheme(R.style.AppTheme_NoActionBar_165);
149                     break;
150                 case 180:
151                     activity.setTheme(R.style.AppTheme_NoActionBar_180);
152                     break;
153                 case 195:
154                     activity.setTheme(R.style.AppTheme_NoActionBar_195);
155                     break;
156                 case 210:
157                     activity.setTheme(R.style.AppTheme_NoActionBar_210);
158                     break;
159                 case 225:
160                     activity.setTheme(R.style.AppTheme_NoActionBar_225);
161                     break;
162                 case 240:
163                     activity.setTheme(R.style.AppTheme_NoActionBar_240);
164                     break;
165                 case 255:
166                     activity.setTheme(R.style.AppTheme_NoActionBar_255);
167                     break;
168                 case 270:
169                     activity.setTheme(R.style.AppTheme_NoActionBar_270);
170                     break;
171                 case 285:
172                     activity.setTheme(R.style.AppTheme_NoActionBar_285);
173                     break;
174                 case 300:
175                     activity.setTheme(R.style.AppTheme_NoActionBar_300);
176                     break;
177                 case 315:
178                     activity.setTheme(R.style.AppTheme_NoActionBar_315);
179                     break;
180                 case 330:
181                     activity.setTheme(R.style.AppTheme_NoActionBar_330);
182                     break;
183                 case 345:
184                     activity.setTheme(R.style.AppTheme_NoActionBar_345);
185                     break;
186                 default:
187                     activity.setTheme(R.style.AppTheme_NoActionBar);
188             }
189         }
190         else activity.setTheme(R.style.AppTheme_NoActionBar);
191
192         refreshColors(activity.getTheme());
193     }
194
195 }