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 androidx.annotation.ColorInt;
29 import androidx.annotation.ColorLong;
30 import androidx.lifecycle.MutableLiveData;
32 import static java.lang.Math.abs;
33 import static net.ktnx.mobileledger.utils.Logger.debug;
36 public static final int DEFAULT_HUE_DEG = 261;
37 private static final float blueLightness = 0.665f;
38 private static final float yellowLightness = 0.350f;
39 public static @ColorInt
42 public static int tableRowLightBG;
44 public static int tableRowDarkBG;
46 public static int primary, defaultTextColor;
47 public static int profileThemeId = -1;
48 public static MutableLiveData<Integer> themeWatch = new MutableLiveData<>(0);
49 public static void refreshColors(Resources.Theme theme) {
50 TypedValue tv = new TypedValue();
51 theme.resolveAttribute(R.attr.table_row_dark_bg, tv, true);
52 tableRowDarkBG = tv.data;
53 theme.resolveAttribute(R.attr.table_row_light_bg, tv, true);
54 tableRowLightBG = tv.data;
55 theme.resolveAttribute(R.attr.colorPrimary, tv, true);
57 theme.resolveAttribute(R.attr.textColor, tv, true);
58 defaultTextColor = tv.data;
59 theme.resolveAttribute(R.attr.colorAccent, tv, true);
62 // trigger theme observers
63 themeWatch.postValue(themeWatch.getValue()+1);
65 public static @ColorLong
66 long hsvaColor(float hue, float saturation, float value, float alpha) {
67 if (alpha < 0 || alpha > 1)
68 throw new IllegalArgumentException("alpha must be between 0 and 1");
70 @ColorLong long rgb = hsvTriplet(hue, saturation, value);
72 long a_bits = Math.round(255 * alpha);
73 return (a_bits << 24) | rgb;
75 public static @ColorInt
76 int hsvColor(float hue, float saturation, float value) {
77 return 0xff000000 | hsvTriplet(hue, saturation, value);
79 public static @ColorInt
80 int hslColor(float hueRatio, float saturation, float lightness) {
81 return 0xff000000 | hslTriplet(hueRatio, saturation, lightness);
83 public static @ColorInt
84 int hsvTriplet(float hue, float saturation, float value) {
85 @ColorLong long result;
88 if ((hue < -0.00005) || (hue > 1.0000005) || (saturation < 0) || (saturation > 1) ||
89 (value < 0) || (value > 1)) throw new IllegalArgumentException(String.format(
90 "hue, saturation, value and alpha must all be between 0 and 1. Arguments given: " +
91 "hue=%1.5f, sat=%1.5f, val=%1.5f", hue, saturation, value));
93 int h = (int) (hue * 6);
94 float f = hue * 6 - h;
95 float p = value * (1 - saturation);
96 float q = value * (1 - f * saturation);
97 float t = value * (1 - (1 - f) * saturation);
102 return tupleToColor(value, t, p);
104 return tupleToColor(q, value, p);
106 return tupleToColor(p, value, t);
108 return tupleToColor(p, q, value);
110 return tupleToColor(t, p, value);
112 return tupleToColor(value, p, q);
114 throw new RuntimeException(String.format("Unexpected value for h (%d) while " +
115 "converting hsv(%1.2f, %1.2f, %1.2f) to " +
116 "rgb", h, hue, saturation, value));
119 public static @ColorInt
120 int hslTriplet(float hueRatio, float saturation, float lightness) {
121 @ColorLong long result;
122 float h = hueRatio * 6;
123 float c = (1 - abs(2f * lightness - 1)) * saturation;
124 float h_mod_2 = h % 2;
125 float x = c * (1 - Math.abs(h_mod_2 - 1));
127 float m = lightness - c / 2f;
129 if (h < 1 || h == 6) return tupleToColor(c + m, x + m, 0 + m);
130 if (h < 2) return tupleToColor(x + m, c + m, 0 + m);
131 if (h < 3) return tupleToColor(0 + m, c + m, x + m);
132 if (h < 4) return tupleToColor(0 + m, x + m, c + m);
133 if (h < 5) return tupleToColor(x + m, 0 + m, c + m);
134 if (h < 6) return tupleToColor(c + m, 0 + m, x + m);
136 throw new IllegalArgumentException(String.format(
137 "Unexpected value for h (%1.3f) while converting hsl(%1.3f, %1.3f, %1.3f) to rgb",
138 h, hueRatio, saturation, lightness));
141 public static @ColorInt
142 int tupleToColor(float r, float g, float b) {
143 int r_int = Math.round(255 * r);
144 int g_int = Math.round(255 * g);
145 int b_int = Math.round(255 * b);
146 return (r_int << 16) | (g_int << 8) | b_int;
148 public static @ColorInt
149 int getPrimaryColorForHue(int hueDegrees) {
150 // int result = hsvColor(hueDegrees, 0.61f, 0.95f);
151 float y = hueDegrees - 60;
153 float l = yellowLightness + (blueLightness - yellowLightness) *
154 (float) Math.cos(Math.toRadians(Math.abs(180 - y) / 2f));
155 int result = hslColor(hueDegrees/360f, 0.845f, l);
156 debug("colors", String.format("getPrimaryColorForHue(%d) = %x", hueDegrees, 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 final int themeId = (profile == null) ? -1 : profile.getThemeId();
165 setupTheme(activity, themeId);
167 public static void setupTheme(Activity activity, int themeId) {
171 activity.setTheme(R.style.AppTheme_NoActionBar_0);
174 activity.setTheme(R.style.AppTheme_NoActionBar_15);
177 activity.setTheme(R.style.AppTheme_NoActionBar_30);
180 activity.setTheme(R.style.AppTheme_NoActionBar_45);
183 activity.setTheme(R.style.AppTheme_NoActionBar_60);
186 activity.setTheme(R.style.AppTheme_NoActionBar_75);
189 activity.setTheme(R.style.AppTheme_NoActionBar_90);
192 activity.setTheme(R.style.AppTheme_NoActionBar_105);
195 activity.setTheme(R.style.AppTheme_NoActionBar_120);
198 activity.setTheme(R.style.AppTheme_NoActionBar_135);
201 activity.setTheme(R.style.AppTheme_NoActionBar_150);
204 activity.setTheme(R.style.AppTheme_NoActionBar_165);
207 activity.setTheme(R.style.AppTheme_NoActionBar_180);
210 activity.setTheme(R.style.AppTheme_NoActionBar_195);
213 activity.setTheme(R.style.AppTheme_NoActionBar_210);
216 activity.setTheme(R.style.AppTheme_NoActionBar_225);
219 activity.setTheme(R.style.AppTheme_NoActionBar_240);
222 activity.setTheme(R.style.AppTheme_NoActionBar_255);
225 activity.setTheme(R.style.AppTheme_NoActionBar_270);
228 activity.setTheme(R.style.AppTheme_NoActionBar_285);
231 activity.setTheme(R.style.AppTheme_NoActionBar_300);
234 activity.setTheme(R.style.AppTheme_NoActionBar_315);
237 activity.setTheme(R.style.AppTheme_NoActionBar_330);
240 activity.setTheme(R.style.AppTheme_NoActionBar_345);
243 activity.setTheme(R.style.AppTheme_NoActionBar);
245 String.format("Theme hue %d not supported, using the default", themeId));
248 refreshColors(activity.getTheme());