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