]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/Colors.java
fb4298b25d93772f3826f2065cbc73dd74b4ba21
[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.Resources;
22 import android.util.TypedValue;
23
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;
28
29 import java.util.Locale;
30
31 import androidx.annotation.ColorInt;
32 import androidx.annotation.ColorLong;
33 import androidx.lifecycle.MutableLiveData;
34
35 import static java.lang.Math.abs;
36 import static net.ktnx.mobileledger.utils.Logger.debug;
37
38 public class Colors {
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
43     int accent;
44     @ColorInt
45     public static int tableRowLightBG;
46     @ColorInt
47     public static int tableRowDarkBG;
48     @ColorInt
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);
59         primary = tv.data;
60         theme.resolveAttribute(R.attr.textColor, tv, true);
61         defaultTextColor = tv.data;
62         theme.resolveAttribute(R.attr.colorAccent, tv, true);
63         accent = tv.data;
64
65         // trigger theme observers
66         themeWatch.postValue(themeWatch.getValue() + 1);
67     }
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");
72
73         @ColorLong long rgb = hsvTriplet(hue, saturation, value);
74
75         long a_bits = Math.round(255 * alpha);
76         return (a_bits << 24) | rgb;
77     }
78     public static @ColorInt
79     int hsvColor(float hue, float saturation, float value) {
80         return 0xff000000 | hsvTriplet(hue, saturation, value);
81     }
82     public static @ColorInt
83     int hslColor(float hueRatio, float saturation, float lightness) {
84         return 0xff000000 | hslTriplet(hueRatio, saturation, lightness);
85     }
86     public static @ColorInt
87     int hsvTriplet(float hue, float saturation, float value) {
88         @ColorLong long result;
89         int r, g, b;
90
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));
95
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);
101
102         switch (h) {
103             case 0:
104             case 6:
105                 return tupleToColor(value, t, p);
106             case 1:
107                 return tupleToColor(q, value, p);
108             case 2:
109                 return tupleToColor(p, value, t);
110             case 3:
111                 return tupleToColor(p, q, value);
112             case 4:
113                 return tupleToColor(t, p, value);
114             case 5:
115                 return tupleToColor(value, p, q);
116             default:
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));
120         }
121     }
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));
129         int r, g, b;
130         float m = lightness - c / 2f;
131
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);
138
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));
142     }
143
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;
150     }
151     public static @ColorInt
152     int getPrimaryColorForHue(int hueDegrees) {
153 //        int result = hsvColor(hueDegrees, 0.61f, 0.95f);
154         float y = hueDegrees - 60;
155         if (y < 0) y += 360;
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,
160                 result));
161         return result;
162     }
163     public static void setupTheme(Activity activity) {
164         MobileLedgerProfile profile = Data.profile.getValue();
165         setupTheme(activity, profile);
166     }
167     public static void setupTheme(Activity activity, MobileLedgerProfile profile) {
168         final int themeHue = (profile == null) ? -1 : profile.getThemeId();
169         setupTheme(activity, themeHue);
170     }
171     public static void setupTheme(Activity activity, int themeHue) {
172         int themeId = -1;
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);
177         }
178
179         if (themeId < 0) {
180             activity.setTheme(R.style.AppTheme_NoActionBar);
181             debug("profiles",
182                     String.format(Locale.ENGLISH, "Theme hue %d not supported, using the default",
183                             themeHue));
184         }
185         else {
186             activity.setTheme(themeId);
187         }
188
189         refreshColors(activity.getTheme());
190     }
191
192 }