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