]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/Colors.java
rework theme color spectrum to match the holy grail violet and get a darker yellow...
[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     private static final float blueLightness = 0.665f;
37     private static final float yellowLightness = 0.350f;
38     public static @ColorInt
39     int accent;
40     @ColorInt
41     public static int tableRowLightBG;
42     @ColorInt
43     public static int tableRowDarkBG;
44     @ColorInt
45     public static int primary, defaultTextColor;
46     public static int profileThemeId = -1;
47     public static ObservableValue<Integer> themeWatch = new ObservableValue<>(0);
48     public static void refreshColors(Resources.Theme theme) {
49         TypedValue tv = new TypedValue();
50         theme.resolveAttribute(R.attr.table_row_dark_bg, tv, true);
51         tableRowDarkBG = tv.data;
52         theme.resolveAttribute(R.attr.table_row_light_bg, tv, true);
53         tableRowLightBG = tv.data;
54         theme.resolveAttribute(R.attr.colorPrimary, tv, true);
55         primary = tv.data;
56         theme.resolveAttribute(R.attr.textColor, tv, true);
57         defaultTextColor = tv.data;
58         theme.resolveAttribute(R.attr.colorAccent, tv, true);
59         accent = tv.data;
60
61         // trigger theme observers
62         themeWatch.notifyObservers();
63     }
64     public static @ColorLong
65     long hsvaColor(float hue, float saturation, float value, float alpha) {
66         if (alpha < 0 || alpha > 1)
67             throw new IllegalArgumentException("alpha must be between 0 and 1");
68
69         @ColorLong long rgb = hsvTriplet(hue, saturation, value);
70
71         long a_bits = Math.round(255 * alpha);
72         return (a_bits << 24) | rgb;
73     }
74     public static @ColorInt
75     int hsvColor(float hue, float saturation, float value) {
76         return 0xff000000 | hsvTriplet(hue, saturation, value);
77     }
78     public static @ColorInt
79     int hslColor(float hueRatio, float saturation, float lightness) {
80         return 0xff000000 | hslTriplet(hueRatio, saturation, lightness);
81     }
82     public static @ColorInt
83     int hsvTriplet(float hue, float saturation, float value) {
84         @ColorLong long result;
85         int r, g, b;
86
87         if ((hue < -0.00005) || (hue > 1.0000005) || (saturation < 0) || (saturation > 1) ||
88             (value < 0) || (value > 1)) throw new IllegalArgumentException(String.format(
89                 "hue, saturation, value and alpha must all be between 0 and 1. Arguments given: " +
90                 "hue=%1.5f, sat=%1.5f, val=%1.5f", hue, saturation, value));
91
92         int h = (int) (hue * 6);
93         float f = hue * 6 - h;
94         float p = value * (1 - saturation);
95         float q = value * (1 - f * saturation);
96         float t = value * (1 - (1 - f) * saturation);
97
98         switch (h) {
99             case 0:
100             case 6:
101                 return tupleToColor(value, t, p);
102             case 1:
103                 return tupleToColor(q, value, p);
104             case 2:
105                 return tupleToColor(p, value, t);
106             case 3:
107                 return tupleToColor(p, q, value);
108             case 4:
109                 return tupleToColor(t, p, value);
110             case 5:
111                 return tupleToColor(value, p, q);
112             default:
113                 throw new RuntimeException(String.format("Unexpected value for h (%d) while " +
114                                                          "converting hsv(%1.2f, %1.2f, %1.2f) to " +
115                                                          "rgb", h, hue, saturation, value));
116         }
117     }
118     public static @ColorInt
119     int hslTriplet(float hueRatio, float saturation, float lightness) {
120         @ColorLong long result;
121         float h = hueRatio * 6;
122         float c = (1 - abs(2f * lightness - 1)) * saturation;
123         float h_mod_2 = h % 2;
124         float x = c * (1 - Math.abs(h_mod_2 - 1));
125         int r, g, b;
126         float m = lightness - c / 2f;
127
128         if (h < 1 || h == 6) return tupleToColor(c + m, x + m, 0 + m);
129         if (h < 2) return tupleToColor(x + m, c + m, 0 + m);
130         if (h < 3) return tupleToColor(0 + m, c + m, x + m);
131         if (h < 4) return tupleToColor(0 + m, x + m, c + m);
132         if (h < 5) return tupleToColor(x + m, 0 + m, c + m);
133         if (h < 6) return tupleToColor(c + m, 0 + m, x + m);
134
135         throw new IllegalArgumentException(String.format(
136                 "Unexpected value for h (%1.3f) while converting hsl(%1.3f, %1.3f, %1.3f) to rgb",
137                 h, hueRatio, saturation, lightness));
138     }
139
140     public static @ColorInt
141     int tupleToColor(float r, float g, float b) {
142         int r_int = Math.round(255 * r);
143         int g_int = Math.round(255 * g);
144         int b_int = Math.round(255 * b);
145         return (r_int << 16) | (g_int << 8) | b_int;
146     }
147     public static @ColorInt
148     int getPrimaryColorForHue(int hueDegrees) {
149 //        int result = hsvColor(hueDegrees, 0.61f, 0.95f);
150         float y = hueDegrees - 60;
151         if (y < 0) y += 360;
152         float l = yellowLightness + (blueLightness - yellowLightness) *
153                                     (float) Math.cos(Math.toRadians(Math.abs(180 - y) / 2f));
154         int result = hslColor(hueDegrees/360f, 0.845f, l);
155         Log.d("colors", String.format("getPrimaryColorForHue(%d) = %x", hueDegrees, result));
156         return result;
157     }
158     public static void setupTheme(Activity activity) {
159         MobileLedgerProfile profile = Data.profile.get();
160         setupTheme(activity, profile);
161     }
162     public static void setupTheme(Activity activity, MobileLedgerProfile profile) {
163         final int themeId = (profile == null) ? -1 : profile.getThemeId();
164         setupTheme(activity, themeId);
165     }
166     public static void setupTheme(Activity activity, int themeId) {
167         switch (themeId) {
168             case 0:
169             case 360:
170                 activity.setTheme(R.style.AppTheme_NoActionBar_0);
171                 break;
172             case 15:
173                 activity.setTheme(R.style.AppTheme_NoActionBar_15);
174                 break;
175             case 30:
176                 activity.setTheme(R.style.AppTheme_NoActionBar_30);
177                 break;
178             case 45:
179                 activity.setTheme(R.style.AppTheme_NoActionBar_45);
180                 break;
181             case 60:
182                 activity.setTheme(R.style.AppTheme_NoActionBar_60);
183                 break;
184             case 75:
185                 activity.setTheme(R.style.AppTheme_NoActionBar_75);
186                 break;
187             case 90:
188                 activity.setTheme(R.style.AppTheme_NoActionBar_90);
189                 break;
190             case 105:
191                 activity.setTheme(R.style.AppTheme_NoActionBar_105);
192                 break;
193             case 120:
194                 activity.setTheme(R.style.AppTheme_NoActionBar_120);
195                 break;
196             case 135:
197                 activity.setTheme(R.style.AppTheme_NoActionBar_135);
198                 break;
199             case 150:
200                 activity.setTheme(R.style.AppTheme_NoActionBar_150);
201                 break;
202             case 165:
203                 activity.setTheme(R.style.AppTheme_NoActionBar_165);
204                 break;
205             case 180:
206                 activity.setTheme(R.style.AppTheme_NoActionBar_180);
207                 break;
208             case 195:
209                 activity.setTheme(R.style.AppTheme_NoActionBar_195);
210                 break;
211             case 210:
212                 activity.setTheme(R.style.AppTheme_NoActionBar_210);
213                 break;
214             case 225:
215                 activity.setTheme(R.style.AppTheme_NoActionBar_225);
216                 break;
217             case 240:
218                 activity.setTheme(R.style.AppTheme_NoActionBar_240);
219                 break;
220             case 255:
221                 activity.setTheme(R.style.AppTheme_NoActionBar_255);
222                 break;
223             case 270:
224                 activity.setTheme(R.style.AppTheme_NoActionBar_270);
225                 break;
226             case 285:
227                 activity.setTheme(R.style.AppTheme_NoActionBar_285);
228                 break;
229             case 300:
230                 activity.setTheme(R.style.AppTheme_NoActionBar_300);
231                 break;
232             case 315:
233                 activity.setTheme(R.style.AppTheme_NoActionBar_315);
234                 break;
235             case 330:
236                 activity.setTheme(R.style.AppTheme_NoActionBar_330);
237                 break;
238             case 345:
239                 activity.setTheme(R.style.AppTheme_NoActionBar_345);
240                 break;
241             default:
242                 activity.setTheme(R.style.AppTheme_NoActionBar);
243                 Log.d("profiles",
244                         String.format("Theme hue %d not supported, using the default", themeId));
245         }
246
247         refreshColors(activity.getTheme());
248     }
249
250 }