]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/Colors.java
8e526c1fb6879f59fb565404639313d6b7e3d2b2
[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         if (profile != null) {
165             final int themeId = profile.getThemeId();
166             switch (themeId) {
167                 case 0:
168                 case 360:
169                     activity.setTheme(R.style.AppTheme_NoActionBar_0);
170                     break;
171                 case 15:
172                     activity.setTheme(R.style.AppTheme_NoActionBar_15);
173                     break;
174                 case 30:
175                     activity.setTheme(R.style.AppTheme_NoActionBar_30);
176                     break;
177                 case 45:
178                     activity.setTheme(R.style.AppTheme_NoActionBar_45);
179                     break;
180                 case 60:
181                     activity.setTheme(R.style.AppTheme_NoActionBar_60);
182                     break;
183                 case 75:
184                     activity.setTheme(R.style.AppTheme_NoActionBar_75);
185                     break;
186                 case 90:
187                     activity.setTheme(R.style.AppTheme_NoActionBar_90);
188                     break;
189                 case 105:
190                     activity.setTheme(R.style.AppTheme_NoActionBar_105);
191                     break;
192                 case 120:
193                     activity.setTheme(R.style.AppTheme_NoActionBar_120);
194                     break;
195                 case 135:
196                     activity.setTheme(R.style.AppTheme_NoActionBar_135);
197                     break;
198                 case 150:
199                     activity.setTheme(R.style.AppTheme_NoActionBar_150);
200                     break;
201                 case 165:
202                     activity.setTheme(R.style.AppTheme_NoActionBar_165);
203                     break;
204                 case 180:
205                     activity.setTheme(R.style.AppTheme_NoActionBar_180);
206                     break;
207                 case 195:
208                     activity.setTheme(R.style.AppTheme_NoActionBar_195);
209                     break;
210                 case 210:
211                     activity.setTheme(R.style.AppTheme_NoActionBar_210);
212                     break;
213                 case 225:
214                     activity.setTheme(R.style.AppTheme_NoActionBar_225);
215                     break;
216                 case 240:
217                     activity.setTheme(R.style.AppTheme_NoActionBar_240);
218                     break;
219                 case 255:
220                     activity.setTheme(R.style.AppTheme_NoActionBar_255);
221                     break;
222                 case 270:
223                     activity.setTheme(R.style.AppTheme_NoActionBar_270);
224                     break;
225                 case 285:
226                     activity.setTheme(R.style.AppTheme_NoActionBar_285);
227                     break;
228                 case 300:
229                     activity.setTheme(R.style.AppTheme_NoActionBar_300);
230                     break;
231                 case 315:
232                     activity.setTheme(R.style.AppTheme_NoActionBar_315);
233                     break;
234                 case 330:
235                     activity.setTheme(R.style.AppTheme_NoActionBar_330);
236                     break;
237                 case 345:
238                     activity.setTheme(R.style.AppTheme_NoActionBar_345);
239                     break;
240                 default:
241                     activity.setTheme(R.style.AppTheme_NoActionBar);
242                     Log.d("profiles", String.format("Theme hue %d not supported, using the default",
243                             themeId));
244             }
245         }
246         else {
247             Log.d("profiles", "No profile given, using default theme");
248             activity.setTheme(R.style.AppTheme_NoActionBar);
249         }
250
251         refreshColors(activity.getTheme());
252     }
253
254 }