-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adapt colors based on chosen medicine background color
- Loading branch information
Showing
4 changed files
with
62 additions
and
8 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
app/src/main/java/com/futsch1/medtimer/helpers/ViewColorHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.futsch1.medtimer.helpers; | ||
|
||
import android.content.Context; | ||
import android.util.TypedValue; | ||
import android.widget.Button; | ||
import android.widget.TextView; | ||
|
||
import androidx.core.graphics.ColorUtils; | ||
|
||
import com.google.android.material.card.MaterialCardView; | ||
|
||
public class ViewColorHelper { | ||
public static void setCardBackground(MaterialCardView cardView, TextView[] textViews, int backgroundColor) { | ||
int defaultTextViewColor = getThemeColor(cardView.getContext(), com.google.android.material.R.attr.colorOnSurfaceVariant); | ||
double contrastTextView = ColorUtils.calculateContrast(defaultTextViewColor, backgroundColor); | ||
int cardDefaultBackground = getThemeColor(cardView.getContext(), com.google.android.material.R.attr.colorSurface); | ||
double contrastBackground = ColorUtils.calculateContrast(cardDefaultBackground, backgroundColor); | ||
setTextColor(textViews, contrastTextView < contrastBackground ? cardDefaultBackground : defaultTextViewColor); | ||
cardView.setCardBackgroundColor(backgroundColor); | ||
} | ||
|
||
private static int getThemeColor(Context context, int attribute) { | ||
TypedValue themeColor = new TypedValue(); | ||
context.getTheme().resolveAttribute(attribute, themeColor, true); | ||
return themeColor.data; | ||
} | ||
|
||
private static void setTextColor(TextView[] textViews, int color) { | ||
for (TextView textView : textViews) { | ||
textView.setTextColor(color); | ||
} | ||
} | ||
|
||
public static void setButtonBackground(Button button, int backgroundColor) { | ||
int primaryColor = getThemeColor(button.getContext(), com.google.android.material.R.attr.colorPrimary); | ||
int onPrimaryColor = getThemeColor(button.getContext(), com.google.android.material.R.attr.colorOnPrimary); | ||
double primaryContrast = ColorUtils.calculateContrast(primaryColor, backgroundColor); | ||
double onPrimaryContrast = ColorUtils.calculateContrast(onPrimaryColor, backgroundColor); | ||
setTextColor(button, primaryContrast > onPrimaryContrast ? primaryColor : onPrimaryColor); | ||
button.setBackgroundColor(backgroundColor); | ||
} | ||
|
||
private static void setTextColor(Button button, int buttonTextColor) { | ||
button.setTextColor(buttonTextColor); | ||
} | ||
|
||
public static void setDefaultColors(MaterialCardView cardView, TextView[] textViews) { | ||
int defaultTextViewColor = getThemeColor(cardView.getContext(), com.google.android.material.R.attr.colorOnSurfaceVariant); | ||
int cardDefaultBackground = getThemeColor(cardView.getContext(), com.google.android.material.R.attr.colorSurface); | ||
cardView.setCardBackgroundColor(cardDefaultBackground); | ||
setTextColor(textViews, defaultTextViewColor); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters