]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/Colors.java
multi-color progress indicators
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / utils / Colors.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.utils;
19
20 import android.app.Activity;
21 import android.content.res.ColorStateList;
22 import android.content.res.Resources;
23 import android.util.TypedValue;
24
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;
29
30 import java.util.Locale;
31
32 import androidx.annotation.ColorInt;
33 import androidx.annotation.ColorLong;
34 import androidx.annotation.NonNull;
35 import androidx.lifecycle.MutableLiveData;
36
37 import static java.lang.Math.abs;
38 import static net.ktnx.mobileledger.utils.Logger.debug;
39
40 public class Colors {
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
46     int accent;
47     @ColorInt
48     public static int tableRowLightBG;
49     @ColorInt
50     public static int tableRowDarkBG;
51     @ColorInt
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);
62         primary = tv.data;
63         theme.resolveAttribute(R.attr.textColor, tv, true);
64         defaultTextColor = tv.data;
65         theme.resolveAttribute(R.attr.colorAccent, tv, true);
66         accent = tv.data;
67
68         // trigger theme observers
69         themeWatch.postValue(themeWatch.getValue() + 1);
70     }
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");
75
76         @ColorLong long rgb = hsvTriplet(hue, saturation, value);
77
78         long a_bits = Math.round(255 * alpha);
79         return (a_bits << 24) | rgb;
80     }
81     public static @ColorInt
82     int hsvColor(float hue, float saturation, float value) {
83         return 0xff000000 | hsvTriplet(hue, saturation, value);
84     }
85     public static @ColorInt
86     int hslColor(float hueRatio, float saturation, float lightness) {
87         return 0xff000000 | hslTriplet(hueRatio, saturation, lightness);
88     }
89     public static @ColorInt
90     int hsvTriplet(float hue, float saturation, float value) {
91         @ColorLong long result;
92         int r, g, b;
93
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));
98
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);
104
105         switch (h) {
106             case 0:
107             case 6:
108                 return tupleToColor(value, t, p);
109             case 1:
110                 return tupleToColor(q, value, p);
111             case 2:
112                 return tupleToColor(p, value, t);
113             case 3:
114                 return tupleToColor(p, q, value);
115             case 4:
116                 return tupleToColor(t, p, value);
117             case 5:
118                 return tupleToColor(value, p, q);
119             default:
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));
123         }
124     }
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));
132         int r, g, b;
133         float m = lightness - c / 2f;
134
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);
141
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));
145     }
146
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;
153     }
154     public static @ColorInt
155     int getPrimaryColorForHue(int hueDegrees) {
156 //        int result = hsvColor(hueDegrees, 0.61f, 0.95f);
157         float y = hueDegrees - 60;
158         if (y < 0) y += 360;
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,
163                 result));
164         return result;
165     }
166     public static void setupTheme(Activity activity) {
167         MobileLedgerProfile profile = Data.profile.getValue();
168         setupTheme(activity, profile);
169     }
170     public static void setupTheme(Activity activity, MobileLedgerProfile profile) {
171         final int themeHue = (profile == null) ? -1 : profile.getThemeId();
172         setupTheme(activity, themeHue);
173     }
174     public static void setupTheme(Activity activity, int themeHue) {
175         int themeId = -1;
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);
180         }
181
182         if (themeId < 0) {
183             activity.setTheme(R.style.AppTheme_NoActionBar);
184             debug("profiles",
185                     String.format(Locale.ENGLISH, "Theme hue %d not supported, using the default",
186                             themeHue));
187         }
188         else {
189             activity.setTheme(themeId);
190         }
191
192         refreshColors(activity.getTheme());
193     }
194
195     public static @NonNull
196     ColorStateList getColorStateList() {
197         return getColorStateList(profileThemeId);
198     }
199     public static @NonNull
200     ColorStateList getColorStateList(int hue) {
201         return new ColorStateList(EMPTY_STATES, getColors(hue));
202     }
203     public static int[] getColors() {
204         return getColors(profileThemeId);
205     }
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);
210         }
211         return colors;
212     }
213 }