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.
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.
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/>.
18 package net.ktnx.mobileledger.utils;
20 import android.app.Activity;
21 import android.content.res.Resources;
22 import android.util.Log;
23 import android.util.TypedValue;
25 import net.ktnx.mobileledger.R;
26 import net.ktnx.mobileledger.model.Data;
27 import net.ktnx.mobileledger.model.MobileLedgerProfile;
29 import androidx.annotation.ColorInt;
30 import androidx.annotation.ColorLong;
32 import static java.lang.Math.abs;
35 public static final int DEFAULT_HUE_DEG = 261;
36 public static @ColorInt
39 public static int tableRowLightBG;
41 public static int tableRowDarkBG;
43 public static int primary, defaultTextColor;
44 public static int profileThemeId = -1;
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);
55 theme.resolveAttribute(R.attr.textColor, tv, true);
56 defaultTextColor = tv.data;
57 theme.resolveAttribute(R.attr.colorAccent, tv, true);
60 // trigger theme observers
61 themeWatch.notifyObservers();
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");
68 @ColorLong long rgb = hsvTriplet(hue, saturation, value);
70 long a_bits = Math.round(255 * alpha);
71 return (a_bits << 24) | rgb;
73 public static @ColorInt
74 int hsvColor(float hue, float saturation, float value) {
75 return 0xff000000 | hsvTriplet(hue, saturation, value);
77 public static @ColorInt
78 int hslColor(float hue, float saturation, float lightness) {
79 return 0xff000000 | hslTriplet(hue, saturation, lightness);
81 public static @ColorInt
82 int hsvTriplet(float hue, float saturation, float value) {
83 @ColorLong long result;
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));
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);
100 return tupleToColor(value, t, p);
102 return tupleToColor(q, value, p);
104 return tupleToColor(p, value, t);
106 return tupleToColor(p, q, value);
108 return tupleToColor(t, p, value);
110 return tupleToColor(value, p, q);
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));
117 public static @ColorInt
118 int hslTriplet(float hue, float saturation, float lightness) {
119 @ColorLong long result;
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));
125 float m = lightness - c / 2f;
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);
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));
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;
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);
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));
159 public static void setupTheme(Activity activity) {
160 MobileLedgerProfile profile = Data.profile.get();
161 setupTheme(activity, profile);
163 public static void setupTheme(Activity activity, MobileLedgerProfile profile) {
164 if (profile != null) {
165 final int themeId = profile.getThemeId();
169 activity.setTheme(R.style.AppTheme_NoActionBar_0);
172 activity.setTheme(R.style.AppTheme_NoActionBar_15);
175 activity.setTheme(R.style.AppTheme_NoActionBar_30);
178 activity.setTheme(R.style.AppTheme_NoActionBar_45);
181 activity.setTheme(R.style.AppTheme_NoActionBar_60);
184 activity.setTheme(R.style.AppTheme_NoActionBar_75);
187 activity.setTheme(R.style.AppTheme_NoActionBar_90);
190 activity.setTheme(R.style.AppTheme_NoActionBar_105);
193 activity.setTheme(R.style.AppTheme_NoActionBar_120);
196 activity.setTheme(R.style.AppTheme_NoActionBar_135);
199 activity.setTheme(R.style.AppTheme_NoActionBar_150);
202 activity.setTheme(R.style.AppTheme_NoActionBar_165);
205 activity.setTheme(R.style.AppTheme_NoActionBar_180);
208 activity.setTheme(R.style.AppTheme_NoActionBar_195);
211 activity.setTheme(R.style.AppTheme_NoActionBar_210);
214 activity.setTheme(R.style.AppTheme_NoActionBar_225);
217 activity.setTheme(R.style.AppTheme_NoActionBar_240);
220 activity.setTheme(R.style.AppTheme_NoActionBar_255);
223 activity.setTheme(R.style.AppTheme_NoActionBar_270);
226 activity.setTheme(R.style.AppTheme_NoActionBar_285);
229 activity.setTheme(R.style.AppTheme_NoActionBar_300);
232 activity.setTheme(R.style.AppTheme_NoActionBar_315);
235 activity.setTheme(R.style.AppTheme_NoActionBar_330);
238 activity.setTheme(R.style.AppTheme_NoActionBar_345);
241 activity.setTheme(R.style.AppTheme_NoActionBar);
242 Log.d("profiles", String.format("Theme hue %d not supported, using the default",
247 Log.d("profiles", "No profile given, using default theme");
248 activity.setTheme(R.style.AppTheme_NoActionBar);
251 refreshColors(activity.getTheme());