New helper class for color stuff
[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
36         // trigger theme observers
37         themeWatch.notifyObservers();
38     }
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");
43
44         @ColorLong long rgb = hsvTriplet(hue, saturation, value);
45
46         long a_bits = Math.round(255 * alpha);
47         return (a_bits << 24) | rgb;
48     }
49     public static @ColorInt
50     int hsvColor(float hue, float saturation, float value) {
51         return 0xff000000 | hsvTriplet(hue, saturation, value);
52     }
53     public static @ColorInt
54     int hsvTriplet(float hue, float saturation, float value) {
55         @ColorLong long result;
56         int r, g, b;
57
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");
61
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);
67
68         switch (h) {
69             case 0:
70             case 6:
71                 return tupleToColor(value, t, p);
72             case 1:
73                 return tupleToColor(q, value, p);
74             case 2:
75                 return tupleToColor(p, value, t);
76             case 3:
77                 return tupleToColor(p, q, value);
78             case 4:
79                 return tupleToColor(t, p, value);
80             case 5:
81                 return tupleToColor(value, p, q);
82             default:
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));
86         }
87     }
88
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;
95     }
96     public static @ColorInt
97     int getPrimaryColorForHue(int degrees) {
98         return getPrimaryColorForHue(degrees / 360f);
99     }
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));
104         return result;
105     }
106     public static void setupTheme(Activity activity) {
107         MobileLedgerProfile profile = Data.profile.get();
108         if (profile != null) {
109             switch (Data.profile.get().getThemeId()) {
110                 case 0:
111                     activity.setTheme(R.style.AppTheme_NoActionBar_0);
112                     break;
113                 case 15:
114                     activity.setTheme(R.style.AppTheme_NoActionBar_15);
115                     break;
116                 case 30:
117                     activity.setTheme(R.style.AppTheme_NoActionBar_30);
118                     break;
119                 case 45:
120                     activity.setTheme(R.style.AppTheme_NoActionBar_45);
121                     break;
122                 case 60:
123                     activity.setTheme(R.style.AppTheme_NoActionBar_60);
124                     break;
125                 case 75:
126                     activity.setTheme(R.style.AppTheme_NoActionBar_75);
127                     break;
128                 case 90:
129                     activity.setTheme(R.style.AppTheme_NoActionBar_90);
130                     break;
131                 case 105:
132                     activity.setTheme(R.style.AppTheme_NoActionBar_105);
133                     break;
134                 case 120:
135                     activity.setTheme(R.style.AppTheme_NoActionBar_120);
136                     break;
137                 case 135:
138                     activity.setTheme(R.style.AppTheme_NoActionBar_135);
139                     break;
140                 case 150:
141                     activity.setTheme(R.style.AppTheme_NoActionBar_150);
142                     break;
143                 case 165:
144                     activity.setTheme(R.style.AppTheme_NoActionBar_165);
145                     break;
146                 case 180:
147                     activity.setTheme(R.style.AppTheme_NoActionBar_180);
148                     break;
149                 case 195:
150                     activity.setTheme(R.style.AppTheme_NoActionBar_195);
151                     break;
152                 case 210:
153                     activity.setTheme(R.style.AppTheme_NoActionBar_210);
154                     break;
155                 case 225:
156                     activity.setTheme(R.style.AppTheme_NoActionBar_225);
157                     break;
158                 case 240:
159                     activity.setTheme(R.style.AppTheme_NoActionBar_240);
160                     break;
161                 case 255:
162                     activity.setTheme(R.style.AppTheme_NoActionBar_255);
163                     break;
164                 case 270:
165                     activity.setTheme(R.style.AppTheme_NoActionBar_270);
166                     break;
167                 case 285:
168                     activity.setTheme(R.style.AppTheme_NoActionBar_285);
169                     break;
170                 case 300:
171                     activity.setTheme(R.style.AppTheme_NoActionBar_300);
172                     break;
173                 case 315:
174                     activity.setTheme(R.style.AppTheme_NoActionBar_315);
175                     break;
176                 case 330:
177                     activity.setTheme(R.style.AppTheme_NoActionBar_330);
178                     break;
179                 case 345:
180                     activity.setTheme(R.style.AppTheme_NoActionBar_345);
181                     break;
182                 default:
183                     activity.setTheme(R.style.AppTheme_NoActionBar);
184             }
185         }
186         else activity.setTheme(R.style.AppTheme_NoActionBar);
187
188         refreshColors(activity.getTheme());
189     }
190
191 }