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.ColorStateList;
22 import android.content.res.Resources;
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;
28 import net.ktnx.mobileledger.ui.HueRing;
30 import java.util.Locale;
32 import androidx.annotation.ColorInt;
33 import androidx.annotation.ColorLong;
34 import androidx.annotation.NonNull;
35 import androidx.lifecycle.MutableLiveData;
37 import static java.lang.Math.abs;
38 import static net.ktnx.mobileledger.utils.Logger.debug;
41 public static final int DEFAULT_HUE_DEG = 261;
42 private static final float blueLightness = 0.665f;
43 private static final float yellowLightness = 0.350f;
44 private static final int[][] EMPTY_STATES = new int[][]{new int[0]};
45 public static @ColorInt
48 public static int tableRowLightBG;
50 public static int tableRowDarkBG;
52 public static int primary, defaultTextColor;
53 public static int profileThemeId = -1;
54 public static MutableLiveData<Integer> themeWatch = new MutableLiveData<>(0);
55 public static void refreshColors(Resources.Theme theme) {
56 TypedValue tv = new TypedValue();
57 theme.resolveAttribute(R.attr.table_row_dark_bg, tv, true);
58 tableRowDarkBG = tv.data;
59 theme.resolveAttribute(R.attr.table_row_light_bg, tv, true);
60 tableRowLightBG = tv.data;
61 theme.resolveAttribute(R.attr.colorPrimary, tv, true);
63 theme.resolveAttribute(R.attr.textColor, tv, true);
64 defaultTextColor = tv.data;
65 theme.resolveAttribute(R.attr.colorAccent, tv, true);
68 // trigger theme observers
69 themeWatch.postValue(themeWatch.getValue() + 1);
71 public static @ColorLong
72 long hsvaColor(float hue, float saturation, float value, float alpha) {
73 if (alpha < 0 || alpha > 1)
74 throw new IllegalArgumentException("alpha must be between 0 and 1");
76 @ColorLong long rgb = hsvTriplet(hue, saturation, value);
78 long a_bits = Math.round(255 * alpha);
79 return (a_bits << 24) | rgb;
81 public static @ColorInt
82 int hsvColor(float hue, float saturation, float value) {
83 return 0xff000000 | hsvTriplet(hue, saturation, value);
85 public static @ColorInt
86 int hslColor(float hueRatio, float saturation, float lightness) {
87 return 0xff000000 | hslTriplet(hueRatio, saturation, lightness);
89 public static @ColorInt
90 int hsvTriplet(float hue, float saturation, float value) {
91 @ColorLong long result;
94 if ((hue < -0.00005) || (hue > 1.0000005) || (saturation < 0) || (saturation > 1) ||
95 (value < 0) || (value > 1)) throw new IllegalArgumentException(String.format(
96 "hue, saturation, value and alpha must all be between 0 and 1. Arguments given: " +
97 "hue=%1.5f, sat=%1.5f, val=%1.5f", hue, saturation, value));
99 int h = (int) (hue * 6);
100 float f = hue * 6 - h;
101 float p = value * (1 - saturation);
102 float q = value * (1 - f * saturation);
103 float t = value * (1 - (1 - f) * saturation);
108 return tupleToColor(value, t, p);
110 return tupleToColor(q, value, p);
112 return tupleToColor(p, value, t);
114 return tupleToColor(p, q, value);
116 return tupleToColor(t, p, value);
118 return tupleToColor(value, p, q);
120 throw new RuntimeException(String.format("Unexpected value for h (%d) while " +
121 "converting hsv(%1.2f, %1.2f, %1.2f) to " +
122 "rgb", h, hue, saturation, value));
125 public static @ColorInt
126 int hslTriplet(float hueRatio, float saturation, float lightness) {
127 @ColorLong long result;
128 float h = hueRatio * 6;
129 float c = (1 - abs(2f * lightness - 1)) * saturation;
130 float h_mod_2 = h % 2;
131 float x = c * (1 - Math.abs(h_mod_2 - 1));
133 float m = lightness - c / 2f;
135 if (h < 1 || h == 6) return tupleToColor(c + m, x + m, 0 + m);
136 if (h < 2) return tupleToColor(x + m, c + m, 0 + m);
137 if (h < 3) return tupleToColor(0 + m, c + m, x + m);
138 if (h < 4) return tupleToColor(0 + m, x + m, c + m);
139 if (h < 5) return tupleToColor(x + m, 0 + m, c + m);
140 if (h < 6) return tupleToColor(c + m, 0 + m, x + m);
142 throw new IllegalArgumentException(String.format(
143 "Unexpected value for h (%1.3f) while converting hsl(%1.3f, %1.3f, %1.3f) to rgb",
144 h, hueRatio, saturation, lightness));
147 public static @ColorInt
148 int tupleToColor(float r, float g, float b) {
149 int r_int = Math.round(255 * r);
150 int g_int = Math.round(255 * g);
151 int b_int = Math.round(255 * b);
152 return (r_int << 16) | (g_int << 8) | b_int;
154 public static @ColorInt
155 int getPrimaryColorForHue(int hueDegrees) {
156 // int result = hsvColor(hueDegrees, 0.61f, 0.95f);
157 float y = hueDegrees - 60;
159 float l = yellowLightness + (blueLightness - yellowLightness) *
160 (float) Math.cos(Math.toRadians(Math.abs(180 - y) / 2f));
161 int result = hslColor(hueDegrees / 360f, 0.845f, l);
162 debug("colors", String.format(Locale.ENGLISH, "getPrimaryColorForHue(%d) = %x", hueDegrees,
166 public static void setupTheme(Activity activity) {
167 MobileLedgerProfile profile = Data.profile.getValue();
168 setupTheme(activity, profile);
170 public static void setupTheme(Activity activity, MobileLedgerProfile profile) {
171 final int themeHue = (profile == null) ? -1 : profile.getThemeId();
172 setupTheme(activity, themeHue);
174 public static void setupTheme(Activity activity, int themeHue) {
176 // Relies that theme resource IDs are sequential numbers
177 if (themeHue == 360) themeHue = 0;
178 if ((themeHue >= 0) && (themeHue < 360) && ((themeHue % HueRing.hueStepDegrees) == 0)) {
179 themeId = R.style.AppTheme_NoActionBar_000 + (themeHue / HueRing.hueStepDegrees);
183 activity.setTheme(R.style.AppTheme_NoActionBar);
185 String.format(Locale.ENGLISH, "Theme hue %d not supported, using the default",
189 activity.setTheme(themeId);
192 refreshColors(activity.getTheme());
195 public static @NonNull
196 ColorStateList getColorStateList() {
197 return getColorStateList(profileThemeId);
199 public static @NonNull
200 ColorStateList getColorStateList(int hue) {
201 return new ColorStateList(EMPTY_STATES, getColors(hue));
203 public static int[] getColors() {
204 return getColors(profileThemeId);
206 public static int[] getColors(int hue) {
207 int[] colors = new int[]{0, 0, 0, 0, 0, 0};
208 for (int i = 0; i < 6; i++, hue = (hue + 60) % 360) {
209 colors[i] = getPrimaryColorForHue(hue);