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 private static final float blueLightness = 0.665f;
37 private static final float yellowLightness = 0.350f;
38 public static @ColorInt
41 public static int tableRowLightBG;
43 public static int tableRowDarkBG;
45 public static int primary, defaultTextColor;
46 public static int profileThemeId = -1;
47 public static ObservableValue<Integer> themeWatch = new ObservableValue<>(0);
48 public static void refreshColors(Resources.Theme theme) {
49 TypedValue tv = new TypedValue();
50 theme.resolveAttribute(R.attr.table_row_dark_bg, tv, true);
51 tableRowDarkBG = tv.data;
52 theme.resolveAttribute(R.attr.table_row_light_bg, tv, true);
53 tableRowLightBG = tv.data;
54 theme.resolveAttribute(R.attr.colorPrimary, tv, true);
56 theme.resolveAttribute(R.attr.textColor, tv, true);
57 defaultTextColor = tv.data;
58 theme.resolveAttribute(R.attr.colorAccent, tv, true);
61 // trigger theme observers
62 themeWatch.notifyObservers();
64 public static @ColorLong
65 long hsvaColor(float hue, float saturation, float value, float alpha) {
66 if (alpha < 0 || alpha > 1)
67 throw new IllegalArgumentException("alpha must be between 0 and 1");
69 @ColorLong long rgb = hsvTriplet(hue, saturation, value);
71 long a_bits = Math.round(255 * alpha);
72 return (a_bits << 24) | rgb;
74 public static @ColorInt
75 int hsvColor(float hue, float saturation, float value) {
76 return 0xff000000 | hsvTriplet(hue, saturation, value);
78 public static @ColorInt
79 int hslColor(float hueRatio, float saturation, float lightness) {
80 return 0xff000000 | hslTriplet(hueRatio, saturation, lightness);
82 public static @ColorInt
83 int hsvTriplet(float hue, float saturation, float value) {
84 @ColorLong long result;
87 if ((hue < -0.00005) || (hue > 1.0000005) || (saturation < 0) || (saturation > 1) ||
88 (value < 0) || (value > 1)) throw new IllegalArgumentException(String.format(
89 "hue, saturation, value and alpha must all be between 0 and 1. Arguments given: " +
90 "hue=%1.5f, sat=%1.5f, val=%1.5f", hue, saturation, value));
92 int h = (int) (hue * 6);
93 float f = hue * 6 - h;
94 float p = value * (1 - saturation);
95 float q = value * (1 - f * saturation);
96 float t = value * (1 - (1 - f) * saturation);
101 return tupleToColor(value, t, p);
103 return tupleToColor(q, value, p);
105 return tupleToColor(p, value, t);
107 return tupleToColor(p, q, value);
109 return tupleToColor(t, p, value);
111 return tupleToColor(value, p, q);
113 throw new RuntimeException(String.format("Unexpected value for h (%d) while " +
114 "converting hsv(%1.2f, %1.2f, %1.2f) to " +
115 "rgb", h, hue, saturation, value));
118 public static @ColorInt
119 int hslTriplet(float hueRatio, float saturation, float lightness) {
120 @ColorLong long result;
121 float h = hueRatio * 6;
122 float c = (1 - abs(2f * lightness - 1)) * saturation;
123 float h_mod_2 = h % 2;
124 float x = c * (1 - Math.abs(h_mod_2 - 1));
126 float m = lightness - c / 2f;
128 if (h < 1 || h == 6) return tupleToColor(c + m, x + m, 0 + m);
129 if (h < 2) return tupleToColor(x + m, c + m, 0 + m);
130 if (h < 3) return tupleToColor(0 + m, c + m, x + m);
131 if (h < 4) return tupleToColor(0 + m, x + m, c + m);
132 if (h < 5) return tupleToColor(x + m, 0 + m, c + m);
133 if (h < 6) return tupleToColor(c + m, 0 + m, x + m);
135 throw new IllegalArgumentException(String.format(
136 "Unexpected value for h (%1.3f) while converting hsl(%1.3f, %1.3f, %1.3f) to rgb",
137 h, hueRatio, saturation, lightness));
140 public static @ColorInt
141 int tupleToColor(float r, float g, float b) {
142 int r_int = Math.round(255 * r);
143 int g_int = Math.round(255 * g);
144 int b_int = Math.round(255 * b);
145 return (r_int << 16) | (g_int << 8) | b_int;
147 public static @ColorInt
148 int getPrimaryColorForHue(int hueDegrees) {
149 // int result = hsvColor(hueDegrees, 0.61f, 0.95f);
150 float y = hueDegrees - 60;
152 float l = yellowLightness + (blueLightness - yellowLightness) *
153 (float) Math.cos(Math.toRadians(Math.abs(180 - y) / 2f));
154 int result = hslColor(hueDegrees/360f, 0.845f, l);
155 Log.d("colors", String.format("getPrimaryColorForHue(%d) = %x", hueDegrees, result));
158 public static void setupTheme(Activity activity) {
159 MobileLedgerProfile profile = Data.profile.get();
160 setupTheme(activity, profile);
162 public static void setupTheme(Activity activity, MobileLedgerProfile profile) {
163 final int themeId = (profile == null) ? -1 : profile.getThemeId();
164 setupTheme(activity, themeId);
166 public static void setupTheme(Activity activity, int themeId) {
170 activity.setTheme(R.style.AppTheme_NoActionBar_0);
173 activity.setTheme(R.style.AppTheme_NoActionBar_15);
176 activity.setTheme(R.style.AppTheme_NoActionBar_30);
179 activity.setTheme(R.style.AppTheme_NoActionBar_45);
182 activity.setTheme(R.style.AppTheme_NoActionBar_60);
185 activity.setTheme(R.style.AppTheme_NoActionBar_75);
188 activity.setTheme(R.style.AppTheme_NoActionBar_90);
191 activity.setTheme(R.style.AppTheme_NoActionBar_105);
194 activity.setTheme(R.style.AppTheme_NoActionBar_120);
197 activity.setTheme(R.style.AppTheme_NoActionBar_135);
200 activity.setTheme(R.style.AppTheme_NoActionBar_150);
203 activity.setTheme(R.style.AppTheme_NoActionBar_165);
206 activity.setTheme(R.style.AppTheme_NoActionBar_180);
209 activity.setTheme(R.style.AppTheme_NoActionBar_195);
212 activity.setTheme(R.style.AppTheme_NoActionBar_210);
215 activity.setTheme(R.style.AppTheme_NoActionBar_225);
218 activity.setTheme(R.style.AppTheme_NoActionBar_240);
221 activity.setTheme(R.style.AppTheme_NoActionBar_255);
224 activity.setTheme(R.style.AppTheme_NoActionBar_270);
227 activity.setTheme(R.style.AppTheme_NoActionBar_285);
230 activity.setTheme(R.style.AppTheme_NoActionBar_300);
233 activity.setTheme(R.style.AppTheme_NoActionBar_315);
236 activity.setTheme(R.style.AppTheme_NoActionBar_330);
239 activity.setTheme(R.style.AppTheme_NoActionBar_345);
242 activity.setTheme(R.style.AppTheme_NoActionBar);
244 String.format("Theme hue %d not supported, using the default", themeId));
247 refreshColors(activity.getTheme());