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.TypedValue;
24 import net.ktnx.mobileledger.R;
25 import net.ktnx.mobileledger.model.Data;
26 import net.ktnx.mobileledger.model.MobileLedgerProfile;
28 import java.util.Locale;
30 import androidx.annotation.ColorInt;
31 import androidx.annotation.ColorLong;
32 import androidx.lifecycle.MutableLiveData;
34 import static java.lang.Math.abs;
35 import static net.ktnx.mobileledger.utils.Logger.debug;
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
44 public static int tableRowLightBG;
46 public static int tableRowDarkBG;
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);
59 theme.resolveAttribute(R.attr.textColor, tv, true);
60 defaultTextColor = tv.data;
61 theme.resolveAttribute(R.attr.colorAccent, tv, true);
64 // trigger theme observers
65 themeWatch.postValue(themeWatch.getValue() + 1);
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");
72 @ColorLong long rgb = hsvTriplet(hue, saturation, value);
74 long a_bits = Math.round(255 * alpha);
75 return (a_bits << 24) | rgb;
77 public static @ColorInt
78 int hsvColor(float hue, float saturation, float value) {
79 return 0xff000000 | hsvTriplet(hue, saturation, value);
81 public static @ColorInt
82 int hslColor(float hueRatio, float saturation, float lightness) {
83 return 0xff000000 | hslTriplet(hueRatio, saturation, lightness);
85 public static @ColorInt
86 int hsvTriplet(float hue, float saturation, float value) {
87 @ColorLong long result;
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));
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);
104 return tupleToColor(value, t, p);
106 return tupleToColor(q, value, p);
108 return tupleToColor(p, value, t);
110 return tupleToColor(p, q, value);
112 return tupleToColor(t, p, value);
114 return tupleToColor(value, p, q);
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));
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));
129 float m = lightness - c / 2f;
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);
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));
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;
150 public static @ColorInt
151 int getPrimaryColorForHue(int hueDegrees) {
152 // int result = hsvColor(hueDegrees, 0.61f, 0.95f);
153 float y = hueDegrees - 60;
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,
162 public static void setupTheme(Activity activity) {
163 MobileLedgerProfile profile = Data.profile.getValue();
164 setupTheme(activity, profile);
166 public static void setupTheme(Activity activity, MobileLedgerProfile profile) {
167 final int themeId = (profile == null) ? -1 : profile.getThemeId();
168 setupTheme(activity, themeId);
170 public static void setupTheme(Activity activity, int themeId) {
174 activity.setTheme(R.style.AppTheme_NoActionBar_0);
177 activity.setTheme(R.style.AppTheme_NoActionBar_15);
180 activity.setTheme(R.style.AppTheme_NoActionBar_30);
183 activity.setTheme(R.style.AppTheme_NoActionBar_45);
186 activity.setTheme(R.style.AppTheme_NoActionBar_60);
189 activity.setTheme(R.style.AppTheme_NoActionBar_75);
192 activity.setTheme(R.style.AppTheme_NoActionBar_90);
195 activity.setTheme(R.style.AppTheme_NoActionBar_105);
198 activity.setTheme(R.style.AppTheme_NoActionBar_120);
201 activity.setTheme(R.style.AppTheme_NoActionBar_135);
204 activity.setTheme(R.style.AppTheme_NoActionBar_150);
207 activity.setTheme(R.style.AppTheme_NoActionBar_165);
210 activity.setTheme(R.style.AppTheme_NoActionBar_180);
213 activity.setTheme(R.style.AppTheme_NoActionBar_195);
216 activity.setTheme(R.style.AppTheme_NoActionBar_210);
219 activity.setTheme(R.style.AppTheme_NoActionBar_225);
222 activity.setTheme(R.style.AppTheme_NoActionBar_240);
225 activity.setTheme(R.style.AppTheme_NoActionBar_255);
228 activity.setTheme(R.style.AppTheme_NoActionBar_270);
231 activity.setTheme(R.style.AppTheme_NoActionBar_285);
234 activity.setTheme(R.style.AppTheme_NoActionBar_300);
237 activity.setTheme(R.style.AppTheme_NoActionBar_315);
240 activity.setTheme(R.style.AppTheme_NoActionBar_330);
243 activity.setTheme(R.style.AppTheme_NoActionBar_345);
246 activity.setTheme(R.style.AppTheme_NoActionBar);
247 debug("profiles", String.format(Locale.ENGLISH,
248 "Theme hue %d not supported, using the default", themeId));
251 refreshColors(activity.getTheme());