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;
27 import net.ktnx.mobileledger.ui.HueRing;
29 import java.util.Locale;
31 import androidx.annotation.ColorInt;
32 import androidx.annotation.ColorLong;
33 import androidx.lifecycle.MutableLiveData;
35 import static java.lang.Math.abs;
36 import static net.ktnx.mobileledger.utils.Logger.debug;
39 public static final int DEFAULT_HUE_DEG = 261;
40 private static final float blueLightness = 0.665f;
41 private static final float yellowLightness = 0.350f;
42 public static @ColorInt
45 public static int tableRowLightBG;
47 public static int tableRowDarkBG;
49 public static int primary, defaultTextColor;
50 public static int profileThemeId = -1;
51 public static MutableLiveData<Integer> themeWatch = new MutableLiveData<>(0);
52 public static void refreshColors(Resources.Theme theme) {
53 TypedValue tv = new TypedValue();
54 theme.resolveAttribute(R.attr.table_row_dark_bg, tv, true);
55 tableRowDarkBG = tv.data;
56 theme.resolveAttribute(R.attr.table_row_light_bg, tv, true);
57 tableRowLightBG = tv.data;
58 theme.resolveAttribute(R.attr.colorPrimary, tv, true);
60 theme.resolveAttribute(R.attr.textColor, tv, true);
61 defaultTextColor = tv.data;
62 theme.resolveAttribute(R.attr.colorAccent, tv, true);
65 // trigger theme observers
66 themeWatch.postValue(themeWatch.getValue() + 1);
68 public static @ColorLong
69 long hsvaColor(float hue, float saturation, float value, float alpha) {
70 if (alpha < 0 || alpha > 1)
71 throw new IllegalArgumentException("alpha must be between 0 and 1");
73 @ColorLong long rgb = hsvTriplet(hue, saturation, value);
75 long a_bits = Math.round(255 * alpha);
76 return (a_bits << 24) | rgb;
78 public static @ColorInt
79 int hsvColor(float hue, float saturation, float value) {
80 return 0xff000000 | hsvTriplet(hue, saturation, value);
82 public static @ColorInt
83 int hslColor(float hueRatio, float saturation, float lightness) {
84 return 0xff000000 | hslTriplet(hueRatio, saturation, lightness);
86 public static @ColorInt
87 int hsvTriplet(float hue, float saturation, float value) {
88 @ColorLong long result;
91 if ((hue < -0.00005) || (hue > 1.0000005) || (saturation < 0) || (saturation > 1) ||
92 (value < 0) || (value > 1)) throw new IllegalArgumentException(String.format(
93 "hue, saturation, value and alpha must all be between 0 and 1. Arguments given: " +
94 "hue=%1.5f, sat=%1.5f, val=%1.5f", hue, saturation, value));
96 int h = (int) (hue * 6);
97 float f = hue * 6 - h;
98 float p = value * (1 - saturation);
99 float q = value * (1 - f * saturation);
100 float t = value * (1 - (1 - f) * saturation);
105 return tupleToColor(value, t, p);
107 return tupleToColor(q, value, p);
109 return tupleToColor(p, value, t);
111 return tupleToColor(p, q, value);
113 return tupleToColor(t, p, value);
115 return tupleToColor(value, p, q);
117 throw new RuntimeException(String.format("Unexpected value for h (%d) while " +
118 "converting hsv(%1.2f, %1.2f, %1.2f) to " +
119 "rgb", h, hue, saturation, value));
122 public static @ColorInt
123 int hslTriplet(float hueRatio, float saturation, float lightness) {
124 @ColorLong long result;
125 float h = hueRatio * 6;
126 float c = (1 - abs(2f * lightness - 1)) * saturation;
127 float h_mod_2 = h % 2;
128 float x = c * (1 - Math.abs(h_mod_2 - 1));
130 float m = lightness - c / 2f;
132 if (h < 1 || h == 6) return tupleToColor(c + m, x + m, 0 + m);
133 if (h < 2) return tupleToColor(x + m, c + m, 0 + m);
134 if (h < 3) return tupleToColor(0 + m, c + m, x + m);
135 if (h < 4) return tupleToColor(0 + m, x + m, c + m);
136 if (h < 5) return tupleToColor(x + m, 0 + m, c + m);
137 if (h < 6) return tupleToColor(c + m, 0 + m, x + m);
139 throw new IllegalArgumentException(String.format(
140 "Unexpected value for h (%1.3f) while converting hsl(%1.3f, %1.3f, %1.3f) to rgb",
141 h, hueRatio, saturation, lightness));
144 public static @ColorInt
145 int tupleToColor(float r, float g, float b) {
146 int r_int = Math.round(255 * r);
147 int g_int = Math.round(255 * g);
148 int b_int = Math.round(255 * b);
149 return (r_int << 16) | (g_int << 8) | b_int;
151 public static @ColorInt
152 int getPrimaryColorForHue(int hueDegrees) {
153 // int result = hsvColor(hueDegrees, 0.61f, 0.95f);
154 float y = hueDegrees - 60;
156 float l = yellowLightness + (blueLightness - yellowLightness) *
157 (float) Math.cos(Math.toRadians(Math.abs(180 - y) / 2f));
158 int result = hslColor(hueDegrees / 360f, 0.845f, l);
159 debug("colors", String.format(Locale.ENGLISH, "getPrimaryColorForHue(%d) = %x", hueDegrees,
163 public static void setupTheme(Activity activity) {
164 MobileLedgerProfile profile = Data.profile.getValue();
165 setupTheme(activity, profile);
167 public static void setupTheme(Activity activity, MobileLedgerProfile profile) {
168 final int themeHue = (profile == null) ? -1 : profile.getThemeId();
169 setupTheme(activity, themeHue);
171 public static void setupTheme(Activity activity, int themeHue) {
173 // Relies that theme resource IDs are sequential numbers
174 if (themeHue == 360) themeHue = 0;
175 if ((themeHue >= 0) && (themeHue < 360) && ((themeHue % HueRing.hueStepDegrees) == 0)) {
176 themeId = R.style.AppTheme_NoActionBar_000 + (themeHue / HueRing.hueStepDegrees);
180 activity.setTheme(R.style.AppTheme_NoActionBar);
182 String.format(Locale.ENGLISH, "Theme hue %d not supported, using the default",
186 activity.setTheme(themeId);
189 refreshColors(activity.getTheme());