]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/Colors.java
single observer instances, single place for reloading account/transaction lists
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / utils / Colors.java
1 /*
2  * Copyright © 2019 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.utils;
19
20 import android.app.Activity;
21 import android.content.res.Resources;
22 import android.util.Log;
23 import android.util.TypedValue;
24
25 import net.ktnx.mobileledger.R;
26 import net.ktnx.mobileledger.model.Data;
27 import net.ktnx.mobileledger.model.MobileLedgerProfile;
28
29 import androidx.annotation.ColorInt;
30 import androidx.annotation.ColorLong;
31
32 import static java.lang.Math.abs;
33
34 public class Colors {
35     public static final int DEFAULT_HUE_DEG = 261;
36     public static @ColorInt
37     int accent;
38     @ColorInt
39     public static int tableRowLightBG;
40     @ColorInt
41     public static int tableRowDarkBG;
42     @ColorInt
43     public static int primary, defaultTextColor;
44     public static int profileThemeId = -1;
45
46     public static ObservableValue<Integer> themeWatch = new ObservableValue<>(0);
47     public static void refreshColors(Resources.Theme theme) {
48         TypedValue tv = new TypedValue();
49         theme.resolveAttribute(R.attr.table_row_dark_bg, tv, true);
50         tableRowDarkBG = tv.data;
51         theme.resolveAttribute(R.attr.table_row_light_bg, tv, true);
52         tableRowLightBG = tv.data;
53         theme.resolveAttribute(R.attr.colorPrimary, tv, true);
54         primary = tv.data;
55         theme.resolveAttribute(R.attr.textColor, tv, true);
56         defaultTextColor = tv.data;
57         theme.resolveAttribute(R.attr.colorAccent, tv, true);
58         accent = tv.data;
59
60         // trigger theme observers
61         themeWatch.notifyObservers();
62     }
63     public static @ColorLong
64     long hsvaColor(float hue, float saturation, float value, float alpha) {
65         if (alpha < 0 || alpha > 1)
66             throw new IllegalArgumentException("alpha must be between 0 and 1");
67
68         @ColorLong long rgb = hsvTriplet(hue, saturation, value);
69
70         long a_bits = Math.round(255 * alpha);
71         return (a_bits << 24) | rgb;
72     }
73     public static @ColorInt
74     int hsvColor(float hue, float saturation, float value) {
75         return 0xff000000 | hsvTriplet(hue, saturation, value);
76     }
77     public static @ColorInt
78     int hslColor(float hue, float saturation, float lightness) {
79         return 0xff000000 | hslTriplet(hue, saturation, lightness);
80     }
81     public static @ColorInt
82     int hsvTriplet(float hue, float saturation, float value) {
83         @ColorLong long result;
84         int r, g, b;
85
86         if ((hue < -0.00005) || (hue > 1.0000005) || (saturation < 0) || (saturation > 1) ||
87             (value < 0) || (value > 1)) throw new IllegalArgumentException(String.format(
88                 "hue, saturation, value and alpha must all be between 0 and 1. Arguments given: " +
89                 "hue=%1.5f, sat=%1.5f, val=%1.5f", hue, saturation, value));
90
91         int h = (int) (hue * 6);
92         float f = hue * 6 - h;
93         float p = value * (1 - saturation);
94         float q = value * (1 - f * saturation);
95         float t = value * (1 - (1 - f) * saturation);
96
97         switch (h) {
98             case 0:
99             case 6:
100                 return tupleToColor(value, t, p);
101             case 1:
102                 return tupleToColor(q, value, p);
103             case 2:
104                 return tupleToColor(p, value, t);
105             case 3:
106                 return tupleToColor(p, q, value);
107             case 4:
108                 return tupleToColor(t, p, value);
109             case 5:
110                 return tupleToColor(value, p, q);
111             default:
112                 throw new RuntimeException(String.format("Unexpected value for h (%d) while " +
113                                                          "converting hsv(%1.2f, %1.2f, %1.2f) to " +
114                                                          "rgb", h, hue, saturation, value));
115         }
116     }
117     public static @ColorInt
118     int hslTriplet(float hue, float saturation, float lightness) {
119         @ColorLong long result;
120         float h = hue * 6;
121         float c = (1 - abs(2f * lightness - 1)) * saturation;
122         float h_mod_2 = h % 2;
123         float x = c * (1 - Math.abs(h_mod_2 - 1));
124         int r, g, b;
125         float m = lightness - c / 2f;
126
127         if (h < 1 || h == 6) return tupleToColor(c + m, x + m, 0 + m);
128         if (h < 2) return tupleToColor(x + m, c + m, 0 + m);
129         if (h < 3) return tupleToColor(0 + m, c + m, x + m);
130         if (h < 4) return tupleToColor(0 + m, x + m, c + m);
131         if (h < 5) return tupleToColor(x + m, 0 + m, c + m);
132         if (h < 6) return tupleToColor(c + m, 0 + m, x + m);
133
134         throw new IllegalArgumentException(String.format(
135                 "Unexpected value for h (%1.3f) while converting hsl(%1.3f, %1.3f, %1.3f) to rgb",
136                 h, hue, saturation, lightness));
137     }
138
139     public static @ColorInt
140     int tupleToColor(float r, float g, float b) {
141         int r_int = Math.round(255 * r);
142         int g_int = Math.round(255 * g);
143         int b_int = Math.round(255 * b);
144         return (r_int << 16) | (g_int << 8) | b_int;
145     }
146     public static @ColorInt
147     int getPrimaryColorForHue(int degrees) {
148         // 0/360f becomes -0.000something for some reason
149         if (degrees == 0) return getPrimaryColorForHue(0f);
150         return getPrimaryColorForHue(degrees / 360f);
151     }
152     public static @ColorInt
153     int getPrimaryColorForHue(float hue) {
154 //        int result = hsvColor(hue, 0.61f, 0.95f);
155         int result = hslColor(hue, 0.60f, 0.60f);
156         Log.d("colors", String.format("getPrimaryColorForHue(%1.2f) = %x", hue, result));
157         return result;
158     }
159     public static void setupTheme(Activity activity) {
160         MobileLedgerProfile profile = Data.profile.get();
161         setupTheme(activity, profile);
162     }
163     public static void setupTheme(Activity activity, MobileLedgerProfile profile) {
164         final int themeId = (profile == null) ? -1 : profile.getThemeId();
165         setupTheme(activity, themeId);
166     }
167     public static void setupTheme(Activity activity, int themeId) {
168         switch (themeId) {
169             case 0:
170             case 360:
171                 activity.setTheme(R.style.AppTheme_NoActionBar_0);
172                 break;
173             case 15:
174                 activity.setTheme(R.style.AppTheme_NoActionBar_15);
175                 break;
176             case 30:
177                 activity.setTheme(R.style.AppTheme_NoActionBar_30);
178                 break;
179             case 45:
180                 activity.setTheme(R.style.AppTheme_NoActionBar_45);
181                 break;
182             case 60:
183                 activity.setTheme(R.style.AppTheme_NoActionBar_60);
184                 break;
185             case 75:
186                 activity.setTheme(R.style.AppTheme_NoActionBar_75);
187                 break;
188             case 90:
189                 activity.setTheme(R.style.AppTheme_NoActionBar_90);
190                 break;
191             case 105:
192                 activity.setTheme(R.style.AppTheme_NoActionBar_105);
193                 break;
194             case 120:
195                 activity.setTheme(R.style.AppTheme_NoActionBar_120);
196                 break;
197             case 135:
198                 activity.setTheme(R.style.AppTheme_NoActionBar_135);
199                 break;
200             case 150:
201                 activity.setTheme(R.style.AppTheme_NoActionBar_150);
202                 break;
203             case 165:
204                 activity.setTheme(R.style.AppTheme_NoActionBar_165);
205                 break;
206             case 180:
207                 activity.setTheme(R.style.AppTheme_NoActionBar_180);
208                 break;
209             case 195:
210                 activity.setTheme(R.style.AppTheme_NoActionBar_195);
211                 break;
212             case 210:
213                 activity.setTheme(R.style.AppTheme_NoActionBar_210);
214                 break;
215             case 225:
216                 activity.setTheme(R.style.AppTheme_NoActionBar_225);
217                 break;
218             case 240:
219                 activity.setTheme(R.style.AppTheme_NoActionBar_240);
220                 break;
221             case 255:
222                 activity.setTheme(R.style.AppTheme_NoActionBar_255);
223                 break;
224             case 270:
225                 activity.setTheme(R.style.AppTheme_NoActionBar_270);
226                 break;
227             case 285:
228                 activity.setTheme(R.style.AppTheme_NoActionBar_285);
229                 break;
230             case 300:
231                 activity.setTheme(R.style.AppTheme_NoActionBar_300);
232                 break;
233             case 315:
234                 activity.setTheme(R.style.AppTheme_NoActionBar_315);
235                 break;
236             case 330:
237                 activity.setTheme(R.style.AppTheme_NoActionBar_330);
238                 break;
239             case 345:
240                 activity.setTheme(R.style.AppTheme_NoActionBar_345);
241                 break;
242             default:
243                 activity.setTheme(R.style.AppTheme_NoActionBar);
244                 Log.d("profiles",
245                         String.format("Theme hue %d not supported, using the default", themeId));
246         }
247
248         refreshColors(activity.getTheme());
249     }
250
251 }