From 1e545ce56f8b695535f1a7e777a339af894e85e5 Mon Sep 17 00:00:00 2001 From: Futsch1 Date: Mon, 19 Feb 2024 12:42:12 +0100 Subject: [PATCH] Adapt colors based on chosen medicine background color --- .../medtimer/helpers/ViewColorHelper.java | 53 +++++++++++++++++++ .../medtimer/medicine/EditMedicine.java | 5 +- .../medtimer/medicine/MedicineViewHolder.java | 6 +-- .../overview/LatestRemindersViewHolder.java | 6 +-- 4 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/com/futsch1/medtimer/helpers/ViewColorHelper.java diff --git a/app/src/main/java/com/futsch1/medtimer/helpers/ViewColorHelper.java b/app/src/main/java/com/futsch1/medtimer/helpers/ViewColorHelper.java new file mode 100644 index 00000000..b00ec9bb --- /dev/null +++ b/app/src/main/java/com/futsch1/medtimer/helpers/ViewColorHelper.java @@ -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); + } +} diff --git a/app/src/main/java/com/futsch1/medtimer/medicine/EditMedicine.java b/app/src/main/java/com/futsch1/medtimer/medicine/EditMedicine.java index 20905d2c..58f1f680 100644 --- a/app/src/main/java/com/futsch1/medtimer/medicine/EditMedicine.java +++ b/app/src/main/java/com/futsch1/medtimer/medicine/EditMedicine.java @@ -35,6 +35,7 @@ import com.futsch1.medtimer.database.Medicine; import com.futsch1.medtimer.database.Reminder; import com.futsch1.medtimer.helpers.SwipeHelper; +import com.futsch1.medtimer.helpers.ViewColorHelper; import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton; import com.google.android.material.switchmaterial.SwitchMaterial; import com.google.android.material.textfield.TextInputEditText; @@ -86,14 +87,14 @@ protected void onCreate(Bundle savedInstanceState) { color = getIntent().getIntExtra(EXTRA_COLOR, Color.DKGRAY); colorButton = findViewById(R.id.selectColor); - colorButton.setBackgroundColor(color); + ViewColorHelper.setButtonBackground(colorButton, color); colorButton.setOnClickListener(v -> { ColorPickerDialog.Builder builder = new ColorPickerDialog.Builder(this) .setTitle(R.string.color) .setPositiveButton(getString(R.string.confirm), (ColorEnvelopeListener) (envelope, fromUser) -> { color = envelope.getColor(); - colorButton.setBackgroundColor(color); + ViewColorHelper.setButtonBackground(colorButton, color); Toast.makeText(this, R.string.change_color_toast, Toast.LENGTH_LONG).show(); }) .setNegativeButton(getString(R.string.cancel), diff --git a/app/src/main/java/com/futsch1/medtimer/medicine/MedicineViewHolder.java b/app/src/main/java/com/futsch1/medtimer/medicine/MedicineViewHolder.java index d3bd8e97..ce93b74f 100644 --- a/app/src/main/java/com/futsch1/medtimer/medicine/MedicineViewHolder.java +++ b/app/src/main/java/com/futsch1/medtimer/medicine/MedicineViewHolder.java @@ -20,6 +20,7 @@ import com.futsch1.medtimer.R; import com.futsch1.medtimer.database.MedicineWithReminders; import com.futsch1.medtimer.helpers.TimeHelper; +import com.futsch1.medtimer.helpers.ViewColorHelper; import com.google.android.material.card.MaterialCardView; import java.util.ArrayList; @@ -81,10 +82,9 @@ public void bind(MedicineWithReminders medicineWithReminders, DeleteCallback del itemView.setOnClickListener(view -> startEditActivity(medicineWithReminders)); if (medicineWithReminders.medicine.useColor) { - ((MaterialCardView) itemView).setCardBackgroundColor(medicineWithReminders.medicine.color); + ViewColorHelper.setCardBackground((MaterialCardView) itemView, new TextView[]{medicineNameView, remindersSummaryView}, medicineWithReminders.medicine.color); } else { - int defaultColor = new MaterialCardView(itemView.getContext()).getCardBackgroundColor().getDefaultColor(); - ((MaterialCardView) itemView).setCardBackgroundColor(defaultColor); + ViewColorHelper.setDefaultColors((MaterialCardView) itemView, new TextView[]{medicineNameView, remindersSummaryView}); } } diff --git a/app/src/main/java/com/futsch1/medtimer/overview/LatestRemindersViewHolder.java b/app/src/main/java/com/futsch1/medtimer/overview/LatestRemindersViewHolder.java index 379afd3b..5880bdd7 100644 --- a/app/src/main/java/com/futsch1/medtimer/overview/LatestRemindersViewHolder.java +++ b/app/src/main/java/com/futsch1/medtimer/overview/LatestRemindersViewHolder.java @@ -10,6 +10,7 @@ import com.futsch1.medtimer.R; import com.futsch1.medtimer.database.ReminderEvent; +import com.futsch1.medtimer.helpers.ViewColorHelper; import com.futsch1.medtimer.reminders.ReminderProcessor; import com.google.android.material.card.MaterialCardView; import com.google.android.material.chip.Chip; @@ -66,10 +67,9 @@ public void bind(ReminderEvent reminderEvent) { }); if (reminderEvent.useColor) { - ((MaterialCardView) itemView).setCardBackgroundColor(reminderEvent.color); + ViewColorHelper.setCardBackground((MaterialCardView) itemView, new TextView[]{reminderEventText}, reminderEvent.color); } else { - int defaultColor = new MaterialCardView(itemView.getContext()).getCardBackgroundColor().getDefaultColor(); - ((MaterialCardView) itemView).setCardBackgroundColor(defaultColor); + ViewColorHelper.setDefaultColors((MaterialCardView) itemView, new TextView[]{reminderEventText}); } } }