Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get monster icon color black/white by background color #245

Merged
merged 1 commit into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import br.alexandregpereira.hunter.ui.compose.AppBarIcon
import br.alexandregpereira.hunter.ui.compose.ChallengeRatingCircle
import br.alexandregpereira.hunter.ui.compose.MonsterTypeIcon
import br.alexandregpereira.hunter.ui.compose.Window
import br.alexandregpereira.hunter.ui.compose.getTintColor
import br.alexandregpereira.hunter.ui.transition.AlphaTransition
import br.alexandregpereira.hunter.ui.transition.getPageOffset
import br.alexandregpereira.hunter.ui.transition.getTransitionData
Expand Down Expand Up @@ -385,6 +386,7 @@ private fun MonsterTypeIcon(
MonsterTypeIcon(
iconRes = data.imageState.type.iconRes,
iconSize = 32.dp,
tint = data.imageState.backgroundColor.getColor(isSystemInDarkTheme()).getTintColor()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ fun MonsterImage(
fontSize = challengeRatingFontSize
)

MonsterTypeIcon(iconRes = iconRes, iconSize = iconSize)
MonsterTypeIcon(
iconRes = iconRes,
iconSize = iconSize,
tint = backgroundColor.getTintColor()
)
}
}

Expand All @@ -75,3 +79,15 @@ fun MonsterImagePreview() = HunterTheme {
iconRes = R.drawable.ic_aberration
)
}

@Preview
@Composable
fun MonsterImageBlackBackgroundPreview() = HunterTheme {
MonsterImage(
url = "asdasdas",
backgroundColor = "#000000",
contentDescription = "Anything",
challengeRating = 18f,
iconRes = R.drawable.ic_aberration
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import br.alexandregpereira.hunter.ui.util.toColor
import kotlin.math.pow

@Composable
fun MonsterTypeIcon(
@DrawableRes iconRes: Int,
iconSize: Dp,
modifier: Modifier = Modifier,
contentDescription: String = ""
contentDescription: String = "",
tint: Color = Color.Black,
) = Box(
contentAlignment = Alignment.TopEnd,
modifier = modifier
Expand All @@ -47,9 +50,25 @@ fun MonsterTypeIcon(
Icon(
painter = painterResource(iconRes),
contentDescription = contentDescription,
tint = Color.Black.copy(alpha = LocalContentAlpha.current),
tint = tint,
modifier = Modifier
.size(iconSize)
.alpha(0.7f)
)
}
}

fun String.getTintColor(): Color {
val color = this.toColor()
val luminance = colorToLuminance(color.red, color.green, color.blue)
return if (luminance > 0.5) Color.Black else Color.White
}

private fun colorToLuminance(red: Float, green: Float, blue: Float): Double {
// Adjust colors based on luminance
val rLum = if (red <= 0.03928f) red / 12.92f else ((red + 0.055f) / 1.055f).pow(2.4f)
val gLum = if (green <= 0.03928f) green / 12.92f else ((green + 0.055f) / 1.055f).pow(2.4f)
val bLum = if (blue <= 0.03928f) blue / 12.92f else ((blue + 0.055f) / 1.055f).pow(2.4f)

// Apply the luminance formula
return (0.2126 * rLum + 0.7152 * gLum + 0.0722 * bLum).toDouble()
}
Loading