]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/Colors.java
41f356d9a2d2689b2f08f134b058d65244409e4c
[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.TypedValue;
23
24 import net.ktnx.mobileledger.R;
25 import net.ktnx.mobileledger.model.Data;
26 import net.ktnx.mobileledger.model.MobileLedgerProfile;
27
28 import androidx.annotation.ColorInt;
29 import androidx.annotation.ColorLong;
30 import androidx.lifecycle.MutableLiveData;
31
32 import static java.lang.Math.abs;
33 import static net.ktnx.mobileledger.utils.Logger.debug;
34
35 public class Colors {
36     public static final int DEFAULT_HUE_DEG = 261;
37     private static final float blueLightness = 0.665f;
38     private static final float yellowLightness = 0.350f;
39     public static @ColorInt
40     int accent;
41     @ColorInt
42     public static int tableRowLightBG;
43     @ColorInt
44     public static int tableRowDarkBG;
45     @ColorInt
46     public static int primary, defaultTextColor;
47     public static int profileThemeId = -1;
48     public static MutableLiveData<Integer> themeWatch = new MutableLiveData<>(0);
49     public static void refreshColors(Resources.Theme theme) {
50         TypedValue tv = new TypedValue();
51         theme.resolveAttribute(R.attr.table_row_dark_bg, tv, true);
52         tableRowDarkBG = tv.data;
53         theme.resolveAttribute(R.attr.table_row_light_bg, tv, true);
54         tableRowLightBG = tv.data;
55         theme.resolveAttribute(R.attr.colorPrimary, tv, true);
56         primary = tv.data;
57         theme.resolveAttribute(R.attr.textColor, tv, true);
58         defaultTextColor = tv.data;
59         theme.resolveAttribute(R.attr.colorAccent, tv, true);
60         accent = tv.data;
61
62         // trigger theme observers
63         themeWatch.postValue(themeWatch.getValue()+1);
64     }
65     public static @ColorLong
66     long hsvaColor(float hue, float saturation, float value, float alpha) {
67         if (alpha < 0 || alpha > 1)
68             throw new IllegalArgumentException("alpha must be between 0 and 1");
69
70         @ColorLong long rgb = hsvTriplet(hue, saturation, value);
71
72         long a_bits = Math.round(255 * alpha);
73         return (a_bits << 24) | rgb;
74     }
75     public static @ColorInt
76     int hsvColor(float hue, float saturation, float value) {
77         return 0xff000000 | hsvTriplet(hue, saturation, value);
78     }
79     public static @ColorInt
80     int hslColor(float hueRatio, float saturation, float lightness) {
81         return 0xff000000 | hslTriplet(hueRatio, saturation, lightness);
82     }
83     public static @ColorInt
84     int hsvTriplet(float hue, float saturation, float value) {
85         @ColorLong long result;
86         int r, g, b;
87
88         if ((hue < -0.00005) || (hue > 1.0000005) || (saturation < 0) || (saturation > 1) ||
89             (value < 0) || (value > 1)) throw new IllegalArgumentException(String.format(
90                 "hue, saturation, value and alpha must all be between 0 and 1. Arguments given: " +
91                 "hue=%1.5f, sat=%1.5f, val=%1.5f", hue, saturation, value));
92
93         int h = (int) (hue * 6);
94         float f = hue * 6 - h;
95         float p = value * (1 - saturation);
96         float q = value * (1 - f * saturation);
97         float t = value * (1 - (1 - f) * saturation);
98
99         switch (h) {
100             case 0:
101             case 6:
102                 return tupleToColor(value, t, p);
103             case 1:
104                 return tupleToColor(q, value, p);
105             case 2:
106                 return tupleToColor(p, value, t);
107             case 3:
108                 return tupleToColor(p, q, value);
109             case 4:
110                 return tupleToColor(t, p, value);
111             case 5:
112                 return tupleToColor(value, p, q);
113             default:
114                 throw new RuntimeException(String.format("Unexpected value for h (%d) while " +
115                                                          "converting hsv(%1.2f, %1.2f, %1.2f) to " +
116                                                          "rgb", h, hue, saturation, value));
117         }
118     }
119     public static @ColorInt
120     int hslTriplet(float hueRatio, float saturation, float lightness) {
121         @ColorLong long result;
122         float h = hueRatio * 6;
123         float c = (1 - abs(2f * lightness - 1)) * saturation;
124         float h_mod_2 = h % 2;
125         float x = c * (1 - Math.abs(h_mod_2 - 1));
126         int r, g, b;
127         float m = lightness - c / 2f;
128
129         if (h < 1 || h == 6) return tupleToColor(c + m, x + m, 0 + m);
130         if (h < 2) return tupleToColor(x + m, c + m, 0 + m);
131         if (h < 3) return tupleToColor(0 + m, c + m, x + m);
132         if (h < 4) return tupleToColor(0 + m, x + m, c + m);
133         if (h < 5) return tupleToColor(x + m, 0 + m, c + m);
134         if (h < 6) return tupleToColor(c + m, 0 + m, x + m);
135
136         throw new IllegalArgumentException(String.format(
137                 "Unexpected value for h (%1.3f) while converting hsl(%1.3f, %1.3f, %1.3f) to rgb",
138                 h, hueRatio, saturation, lightness));
139     }
140
141     public static @ColorInt
142     int tupleToColor(float r, float g, float b) {
143         int r_int = Math.round(255 * r);
144         int g_int = Math.round(255 * g);
145         int b_int = Math.round(255 * b);
146         return (r_int << 16) | (g_int << 8) | b_int;
147     }
148     public static @ColorInt
149     int getPrimaryColorForHue(int hueDegrees) {
150 //        int result = hsvColor(hueDegrees, 0.61f, 0.95f);
151         float y = hueDegrees - 60;
152         if (y < 0) y += 360;
153         float l = yellowLightness + (blueLightness - yellowLightness) *
154                                     (float) Math.cos(Math.toRadians(Math.abs(180 - y) / 2f));
155         int result = hslColor(hueDegrees/360f, 0.845f, l);
156         debug("colors", String.format("getPrimaryColorForHue(%d) = %x", hueDegrees, 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                 debug("profiles",
245                         String.format("Theme hue %d not supported, using the default", themeId));
246         }
247
248         refreshColors(activity.getTheme());
249     }
250
251 }