mirror of https://github.com/tasks/tasks
Apply primary colors programmatically
parent
31483a0aba
commit
5847170a8c
@ -0,0 +1,109 @@
|
||||
package org.tasks.themes;
|
||||
|
||||
import android.graphics.Color;
|
||||
|
||||
public class ColorUtil {
|
||||
|
||||
/**
|
||||
* https://stackoverflow.com/a/40964456
|
||||
* Darkens a given color
|
||||
* @param base base color
|
||||
* @param amount amount between 0 and 100
|
||||
* @return darken color
|
||||
*/
|
||||
static int darken(int base, int amount) {
|
||||
if (base == 0 || base == -1) {
|
||||
return base;
|
||||
}
|
||||
|
||||
float[] hsv = new float[3];
|
||||
Color.colorToHSV(base, hsv);
|
||||
float[] hsl = hsv2hsl(hsv);
|
||||
hsl[2] -= amount / 100f;
|
||||
if (hsl[2] < 0)
|
||||
hsl[2] = 0f;
|
||||
hsv = hsl2hsv(hsl);
|
||||
return Color.HSVToColor(hsv);
|
||||
}
|
||||
|
||||
/**
|
||||
* lightens a given color
|
||||
* @param base base color
|
||||
* @param amount amount between 0 and 100
|
||||
* @return lightened
|
||||
*/
|
||||
public static int lighten(int base, int amount) {
|
||||
float[] hsv = new float[3];
|
||||
Color.colorToHSV(base, hsv);
|
||||
float[] hsl = hsv2hsl(hsv);
|
||||
hsl[2] += amount / 100f;
|
||||
if (hsl[2] > 1)
|
||||
hsl[2] = 1f;
|
||||
hsv = hsl2hsv(hsl);
|
||||
return Color.HSVToColor(hsv);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts HSV (Hue, Saturation, Value) color to HSL (Hue, Saturation, Lightness)
|
||||
* Credit goes to xpansive
|
||||
* https://gist.github.com/xpansive/1337890
|
||||
* @param hsv HSV color array
|
||||
* @return hsl
|
||||
*/
|
||||
private static float[] hsv2hsl(float[] hsv) {
|
||||
float hue = hsv[0];
|
||||
float sat = hsv[1];
|
||||
float val = hsv[2];
|
||||
|
||||
//Saturation is very different between the two color spaces
|
||||
//If (2-sat)*val < 1 set it to sat*val/((2-sat)*val)
|
||||
//Otherwise sat*val/(2-(2-sat)*val)
|
||||
//Conditional is not operating with hue, it is reassigned!
|
||||
// sat*val/((hue=(2-sat)*val)<1?hue:2-hue)
|
||||
float nhue = (2f - sat) * val;
|
||||
float nsat = sat * val / (nhue < 1f ? nhue : 2f - nhue);
|
||||
if (nsat > 1f)
|
||||
nsat = 1f;
|
||||
|
||||
return new float[]{
|
||||
//[hue, saturation, lightness]
|
||||
//Range should be between 0 - 1
|
||||
hue, //Hue stays the same
|
||||
|
||||
// check nhue and nsat logic
|
||||
nsat,
|
||||
|
||||
nhue / 2f //Lightness is (2-sat)*val/2
|
||||
//See reassignment of hue above
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses hsv2hsl
|
||||
* Credit goes to xpansive
|
||||
* https://gist.github.com/xpansive/1337890
|
||||
* @param hsl HSL color array
|
||||
* @return hsv color array
|
||||
*/
|
||||
private static float[] hsl2hsv(float[] hsl) {
|
||||
float hue = hsl[0];
|
||||
float sat = hsl[1];
|
||||
float light = hsl[2];
|
||||
|
||||
sat *= light < .5 ? light : 1 - light;
|
||||
|
||||
return new float[]{
|
||||
//[hue, saturation, value]
|
||||
//Range should be between 0 - 1
|
||||
|
||||
hue, //Hue stays the same
|
||||
2f * sat / (light + sat), //Saturation
|
||||
light + sat //Value
|
||||
};
|
||||
}
|
||||
|
||||
public static String colorToHex(int color) {
|
||||
return String.format("#%06X", (0xFFFFFF & color));
|
||||
}
|
||||
}
|
@ -1,96 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Jared Rummler <jared.rummler@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.tasks.ui;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.util.TypedValue;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.SubMenu;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import org.tasks.R;
|
||||
|
||||
/**
|
||||
* Helper class to set the color and transparency for menu icons in an ActionBar/Toolbar. Example
|
||||
* usage:
|
||||
*
|
||||
* <pre>
|
||||
* <code>
|
||||
* public boolean onCreateOptionsMenu(Menu menu) {
|
||||
* ...
|
||||
* int color = getResources().getColor(R.color.your_awesome_color);
|
||||
* int alpha = 204; // 80% alpha
|
||||
* MenuColorizer.colorMenu(this, menu, color, alpha);
|
||||
* ...
|
||||
* }
|
||||
* </code>
|
||||
* </pre>
|
||||
*
|
||||
* @author Jared Rummler <jared.rummler@gmail.com>
|
||||
* @since Dec 11, 2014
|
||||
*/
|
||||
public class MenuColorizer {
|
||||
|
||||
private MenuColorizer() {}
|
||||
|
||||
public static void colorToolbar(Context context, Toolbar toolbar) {
|
||||
TypedValue typedValue = new TypedValue();
|
||||
context.getTheme().resolveAttribute(R.attr.colorOnPrimary, typedValue, true);
|
||||
colorToolbar(toolbar, typedValue.data);
|
||||
}
|
||||
|
||||
public static void colorToolbar(Toolbar toolbar, int color) {
|
||||
toolbar.setNavigationIcon(colorDrawable(toolbar.getNavigationIcon(), color));
|
||||
toolbar.setTitleTextColor(color);
|
||||
colorMenu(toolbar.getMenu(), color);
|
||||
}
|
||||
|
||||
public static void colorMenu(Context context, Menu menu) {
|
||||
TypedValue typedValue = new TypedValue();
|
||||
context.getTheme().resolveAttribute(R.attr.colorOnPrimary, typedValue, true);
|
||||
colorMenu(menu, typedValue.data);
|
||||
}
|
||||
|
||||
/** Sets a color filter on all menu icons, including the overflow button (if it exists) */
|
||||
private static void colorMenu(final Menu menu, final int color) {
|
||||
for (int i = 0, size = menu.size(); i < size; i++) {
|
||||
final MenuItem menuItem = menu.getItem(i);
|
||||
colorMenuItem(menuItem, color);
|
||||
if (menuItem.hasSubMenu()) {
|
||||
final SubMenu subMenu = menuItem.getSubMenu();
|
||||
for (int j = 0; j < subMenu.size(); j++) {
|
||||
colorMenuItem(subMenu.getItem(j), color);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Sets a color filter on a {@link MenuItem} */
|
||||
private static void colorMenuItem(final MenuItem menuItem, final int color) {
|
||||
colorDrawable(menuItem.getIcon(), color);
|
||||
}
|
||||
|
||||
private static Drawable colorDrawable(Drawable drawable, int color) {
|
||||
if (drawable != null) {
|
||||
drawable.mutate();
|
||||
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
|
||||
}
|
||||
return drawable;
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="AmberAccent" parent="BlackTint">
|
||||
<item name="colorAccent">@color/amber_a400</item>
|
||||
</style>
|
||||
|
||||
<style name="BlueAccent" parent="WhiteTint">
|
||||
<item name="colorAccent">@color/blue_a400</item>
|
||||
</style>
|
||||
|
||||
<style name="BlueGreyAccent" parent="WhiteTint">
|
||||
<item name="colorAccent">@color/blue_grey_400</item>
|
||||
</style>
|
||||
|
||||
<style name="CyanAccent" parent="BlackTint">
|
||||
<item name="colorAccent">@color/cyan_a400</item>
|
||||
</style>
|
||||
|
||||
<style name="DeepOrangeAccent" parent="WhiteTint">
|
||||
<item name="colorAccent">@color/deep_orange_a400</item>
|
||||
</style>
|
||||
|
||||
<style name="DeepPurpleAccent" parent="WhiteTint">
|
||||
<item name="colorAccent">@color/deep_purple_a400</item>
|
||||
</style>
|
||||
|
||||
<style name="GreenAccent" parent="BlackTint">
|
||||
<item name="colorAccent">@color/green_a400</item>
|
||||
</style>
|
||||
|
||||
<style name="IndigoAccent" parent="WhiteTint">
|
||||
<item name="colorAccent">@color/indigo_a400</item>
|
||||
</style>
|
||||
|
||||
<style name="LightBlueAccent" parent="BlackTint">
|
||||
<item name="colorAccent">@color/light_blue_a400</item>
|
||||
</style>
|
||||
|
||||
<style name="LightGreenAccent" parent="BlackTint">
|
||||
<item name="colorAccent">@color/light_green_a400</item>
|
||||
</style>
|
||||
|
||||
<style name="LimeAccent" parent="BlackTint">
|
||||
<item name="colorAccent">@color/lime_a400</item>
|
||||
</style>
|
||||
|
||||
<style name="OrangeAccent" parent="BlackTint">
|
||||
<item name="colorAccent">@color/orange_a400</item>
|
||||
</style>
|
||||
|
||||
<style name="PinkAccent" parent="WhiteTint">
|
||||
<item name="colorAccent">@color/pink_a400</item>
|
||||
</style>
|
||||
|
||||
<style name="PurpleAccent" parent="WhiteTint">
|
||||
<item name="colorAccent">@color/purple_a400</item>
|
||||
</style>
|
||||
|
||||
<style name="RedAccent" parent="WhiteTint">
|
||||
<item name="colorAccent">@color/red_a400</item>
|
||||
</style>
|
||||
|
||||
<style name="TealAccent" parent="BlackTint">
|
||||
<item name="colorAccent">@color/teal_a400</item>
|
||||
</style>
|
||||
|
||||
<style name="YellowAccent" parent="BlackTint">
|
||||
<item name="colorAccent">@color/yellow_a400</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="Amber">
|
||||
<item name="colorPrimary">@color/amber_500</item>
|
||||
<item name="colorPrimaryVariant">@color/amber_700</item>
|
||||
</style>
|
||||
|
||||
<style name="AmberAccent" parent="BlackTint">
|
||||
<item name="colorAccent">@color/amber_a400</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="Blue">
|
||||
<item name="colorPrimary">@color/blue_500</item>
|
||||
<item name="colorPrimaryVariant">@color/blue_700</item>
|
||||
</style>
|
||||
|
||||
<style name="BlueAccent" parent="WhiteTint">
|
||||
<item name="colorAccent">@color/blue_a400</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="BlueGrey">
|
||||
<item name="colorPrimary">@color/blue_grey_500</item>
|
||||
<item name="colorPrimaryVariant">@color/blue_grey_700</item>
|
||||
</style>
|
||||
|
||||
<style name="BlueGreyAccent" parent="WhiteTint">
|
||||
<item name="colorAccent">@color/blue_grey_400</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -1,9 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="Brown">
|
||||
<item name="colorPrimary">@color/brown_500</item>
|
||||
<item name="colorPrimaryVariant">@color/brown_700</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="Cyan">
|
||||
<item name="colorPrimary">@color/cyan_500</item>
|
||||
<item name="colorPrimaryVariant">@color/cyan_700</item>
|
||||
</style>
|
||||
|
||||
<style name="CyanAccent" parent="BlackTint">
|
||||
<item name="colorAccent">@color/cyan_a400</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -1,9 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="DarkGrey">
|
||||
<item name="colorPrimary">@color/grey_900</item>
|
||||
<item name="colorPrimaryVariant">@color/grey_statusbar</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="DeepOrange">
|
||||
<item name="colorPrimary">@color/deep_orange_500</item>
|
||||
<item name="colorPrimaryVariant">@color/deep_orange_700</item>
|
||||
</style>
|
||||
|
||||
<style name="DeepOrangeAccent" parent="WhiteTint">
|
||||
<item name="colorAccent">@color/deep_orange_a400</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="DeepPurple">
|
||||
<item name="colorPrimary">@color/deep_purple_500</item>
|
||||
<item name="colorPrimaryVariant">@color/deep_purple_700</item>
|
||||
</style>
|
||||
|
||||
<style name="DeepPurpleAccent" parent="WhiteTint">
|
||||
<item name="colorAccent">@color/deep_purple_a400</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="Green">
|
||||
<item name="colorPrimary">@color/green_500</item>
|
||||
<item name="colorPrimaryVariant">@color/green_700</item>
|
||||
</style>
|
||||
|
||||
<style name="GreenAccent" parent="BlackTint">
|
||||
<item name="colorAccent">@color/green_a400</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -1,9 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="Grey">
|
||||
<item name="colorPrimary">@color/grey_500</item>
|
||||
<item name="colorPrimaryVariant">@color/grey_700</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="Indigo">
|
||||
<item name="colorPrimary">@color/indigo_500</item>
|
||||
<item name="colorPrimaryVariant">@color/indigo_700</item>
|
||||
</style>
|
||||
|
||||
<style name="IndigoAccent" parent="WhiteTint">
|
||||
<item name="colorAccent">@color/indigo_a400</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="LightBlue">
|
||||
<item name="colorPrimary">@color/light_blue_500</item>
|
||||
<item name="colorPrimaryVariant">@color/light_blue_700</item>
|
||||
</style>
|
||||
|
||||
<style name="LightBlueAccent" parent="BlackTint">
|
||||
<item name="colorAccent">@color/light_blue_a400</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="LightGreen">
|
||||
<item name="colorPrimary">@color/light_green_500</item>
|
||||
<item name="colorPrimaryVariant">@color/light_green_700</item>
|
||||
</style>
|
||||
|
||||
<style name="LightGreenAccent" parent="BlackTint">
|
||||
<item name="colorAccent">@color/light_green_a400</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="Lime">
|
||||
<item name="colorPrimary">@color/lime_500</item>
|
||||
<item name="colorPrimaryVariant">@color/lime_700</item>
|
||||
</style>
|
||||
|
||||
<style name="LimeAccent" parent="BlackTint">
|
||||
<item name="colorAccent">@color/lime_a400</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="Orange">
|
||||
<item name="colorPrimary">@color/orange_500</item>
|
||||
<item name="colorPrimaryVariant">@color/orange_700</item>
|
||||
</style>
|
||||
|
||||
<style name="OrangeAccent" parent="BlackTint">
|
||||
<item name="colorAccent">@color/orange_a400</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="Pink">
|
||||
<item name="colorPrimary">@color/pink_500</item>
|
||||
<item name="colorPrimaryVariant">@color/pink_700</item>
|
||||
</style>
|
||||
|
||||
<style name="PinkAccent" parent="WhiteTint">
|
||||
<item name="colorAccent">@color/pink_a400</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="Purple">
|
||||
<item name="colorPrimary">@color/purple_500</item>
|
||||
<item name="colorPrimaryVariant">@color/purple_700</item>
|
||||
</style>
|
||||
|
||||
<style name="PurpleAccent" parent="WhiteTint">
|
||||
<item name="colorAccent">@color/purple_a400</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="Red">
|
||||
<item name="colorPrimary">@color/red_500</item>
|
||||
<item name="colorPrimaryVariant">@color/red_700</item>
|
||||
</style>
|
||||
|
||||
<style name="RedAccent" parent="WhiteTint">
|
||||
<item name="colorAccent">@color/red_a400</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="Teal">
|
||||
<item name="colorPrimary">@color/teal_500</item>
|
||||
<item name="colorPrimaryVariant">@color/teal_700</item>
|
||||
</style>
|
||||
|
||||
<style name="TealAccent" parent="BlackTint">
|
||||
<item name="colorAccent">@color/teal_a400</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -1,9 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="White">
|
||||
<item name="colorPrimary">@color/white_100</item>
|
||||
<item name="colorPrimaryVariant">@color/white_100</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="Yellow">
|
||||
<item name="colorPrimary">@color/yellow_500</item>
|
||||
<item name="colorPrimaryVariant">@color/yellow_700</item>
|
||||
</style>
|
||||
|
||||
<style name="YellowAccent" parent="BlackTint">
|
||||
<item name="colorAccent">@color/yellow_a400</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
Loading…
Reference in New Issue