-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide background+text color combinations for calendar screen
- Loading branch information
1 parent
6e83be8
commit c9c61e4
Showing
7 changed files
with
112 additions
and
57 deletions.
There are no files selected for viewing
9 changes: 0 additions & 9 deletions
9
app/src/main/java/com/mensinator/app/calendar/CalendarColors.kt
This file was deleted.
Oops, something went wrong.
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
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/mensinator/app/calendar/ColorCombination.kt
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,8 @@ | ||
package com.mensinator.app.calendar | ||
|
||
import androidx.compose.ui.graphics.Color | ||
|
||
data class ColorCombination( | ||
val backgroundColor: Color, | ||
val textColor: Color, | ||
) |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/com/mensinator/app/extensions/ColorExtensions.kt
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,32 @@ | ||
package com.mensinator.app.extensions | ||
|
||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.luminance | ||
|
||
// Pick the text color with the best contrast against this background color. | ||
fun Color.pickBestContrastTextColorForThisBackground( | ||
isDarkMode: Boolean, | ||
textColor1: Color, | ||
textColor2: Color | ||
): Color { | ||
fun calculateContrastRatio(color1: Color, color2: Color): Double { | ||
val lum1 = color1.luminance() | ||
val lum2 = color2.luminance() | ||
|
||
val lighter = maxOf(lum1, lum2) | ||
val darker = minOf(lum1, lum2) | ||
|
||
return (lighter + 0.05) / (darker + 0.05) // +0.05 to avoid null division | ||
} | ||
|
||
val safeBackground = when { | ||
Color.Transparent == this && isDarkMode -> Color.Black | ||
Color.Transparent == this -> Color.White | ||
else -> this | ||
} | ||
|
||
val contrast1 = calculateContrastRatio(safeBackground, textColor1) | ||
val contrast2 = calculateContrastRatio(safeBackground, textColor2) | ||
|
||
return if (contrast1 >= contrast2) textColor1 else textColor2 | ||
} |
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