Calculate toolbar text contrast

pull/935/head
Alex Baker 6 years ago
parent 50f0c5bedd
commit 573cbca918

@ -64,12 +64,12 @@ public class ThemeCache {
Resources.Theme theme = new ContextThemeWrapper(context, ThemeColor.COLORS[i]).getTheme(); Resources.Theme theme = new ContextThemeWrapper(context, ThemeColor.COLORS[i]).getTheme();
colors.add( colors.add(
new ThemeColor( new ThemeColor(
context,
colorNames[i], colorNames[i],
i, i,
resolveAttribute(theme, R.attr.colorPrimary), resolveAttribute(theme, R.attr.colorPrimary),
resolveAttribute(theme, R.attr.colorPrimaryVariant), resolveAttribute(theme, R.attr.colorPrimaryVariant),
resolveAttribute(theme, R.attr.colorOnPrimary), resolveAttribute(theme, R.attr.colorOnPrimary)));
resolveBoolean(theme, R.attr.dark_status_bar)));
} }
String[] accentNames = resources.getStringArray(R.array.accents); String[] accentNames = resources.getStringArray(R.array.accents);
for (int i = 0; i < ThemeAccent.ACCENTS.length; i++) { for (int i = 0; i < ThemeAccent.ACCENTS.length; i++) {
@ -88,12 +88,12 @@ public class ThemeCache {
} }
untaggedColor = untaggedColor =
new ThemeColor( new ThemeColor(
context,
null, null,
19, 19,
getColor(context, R.color.tag_color_none_background), getColor(context, R.color.tag_color_none_background),
0, 0,
getColor(context, R.color.black_87), getColor(context, R.color.black_87));
false);
} }
private static int resolveAttribute(Resources.Theme theme, int attribute) { private static int resolveAttribute(Resources.Theme theme, int attribute) {
@ -122,12 +122,6 @@ public class ThemeCache {
return untaggedColor; return untaggedColor;
} }
private boolean resolveBoolean(Resources.Theme theme, int attribute) {
TypedValue typedValue = new TypedValue();
theme.resolveAttribute(attribute, typedValue, false);
return typedValue.data != 0;
}
public List<ThemeAccent> getAccents() { public List<ThemeAccent> getAccents() {
return copyOf(accents); return copyOf(accents);
} }

@ -6,6 +6,7 @@ import static com.todoroo.andlib.utility.AndroidUtilities.atLeastOreo;
import android.app.Activity; import android.app.Activity;
import android.app.ActivityManager; import android.app.ActivityManager;
import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Build.VERSION_CODES; import android.os.Build.VERSION_CODES;
import android.os.Parcel; import android.os.Parcel;
@ -13,6 +14,7 @@ import android.os.Parcelable;
import android.view.View; import android.view.View;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.core.graphics.ColorUtils;
import androidx.drawerlayout.widget.DrawerLayout; import androidx.drawerlayout.widget.DrawerLayout;
import org.tasks.R; import org.tasks.R;
import org.tasks.dialogs.ColorPickerDialog; import org.tasks.dialogs.ColorPickerDialog;
@ -114,19 +116,22 @@ public class ThemeColor implements ColorPickerDialog.Pickable {
private final boolean isDark; private final boolean isDark;
public ThemeColor( public ThemeColor(
Context context,
String name, String name,
int index, int index,
int colorPrimary, int colorPrimary,
int colorPrimaryVariant, int colorPrimaryVariant,
int actionBarTint, int actionBarTint) {
boolean isDark) {
this.name = name; this.name = name;
this.index = index; this.index = index;
this.actionBarTint = actionBarTint; this.actionBarTint = actionBarTint;
this.style = COLORS[index]; this.style = COLORS[index];
this.colorPrimary = colorPrimary; this.colorPrimary = colorPrimary;
this.colorPrimaryVariant = colorPrimaryVariant; this.colorPrimaryVariant = colorPrimaryVariant;
this.isDark = isDark; double contrast =
ColorUtils.calculateContrast(
context.getResources().getColor(R.color.white_100), colorPrimary);
this.isDark = contrast < 3;
} }
private ThemeColor(Parcel source) { private ThemeColor(Parcel source) {

@ -42,7 +42,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@null" android:background="@null"
android:textColor="?attr/colorPrimary"/> android:textColor="@color/text_primary"/>
</LinearLayout> </LinearLayout>

@ -7,7 +7,6 @@
<!-- theme attributes --> <!-- theme attributes -->
<attr format="reference" name="overlay_theme"/> <attr format="reference" name="overlay_theme"/>
<attr format="boolean" name="dark_status_bar"/>
<attr format="dimension" name="card_elevation"/> <attr format="dimension" name="card_elevation"/>
<attr format="integer" name="horizontal_divider"/> <attr format="integer" name="horizontal_divider"/>
<attr format="color" name="colorOnPrimaryHint"/> <attr format="color" name="colorOnPrimaryHint"/>

@ -27,7 +27,6 @@
</style> </style>
<style name="WhiteToolbarTheme"> <style name="WhiteToolbarTheme">
<item name="dark_status_bar">false</item>
<item name="overlay_theme">@style/WhiteToolbarOverlay</item> <item name="overlay_theme">@style/WhiteToolbarOverlay</item>
<item name="colorOnPrimary">@color/white_100</item> <item name="colorOnPrimary">@color/white_100</item>
<item name="colorOnPrimaryHint">@color/white_70</item> <item name="colorOnPrimaryHint">@color/white_70</item>
@ -35,7 +34,6 @@
</style> </style>
<style name="BlackToolbarTheme"> <style name="BlackToolbarTheme">
<item name="dark_status_bar">true</item>
<item name="overlay_theme">@style/BlackToolbarOverlay</item> <item name="overlay_theme">@style/BlackToolbarOverlay</item>
<item name="colorOnPrimary">@color/black_87</item> <item name="colorOnPrimary">@color/black_87</item>
<item name="colorOnPrimaryHint">@color/black_54</item> <item name="colorOnPrimaryHint">@color/black_54</item>

Loading…
Cancel
Save