-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- saving category colours cause its pretty
- Loading branch information
Showing
20 changed files
with
169 additions
and
55 deletions.
There are no files selected for viewing
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
34 changes: 15 additions & 19 deletions
34
app/src/main/java/cloud/pablos/overload/data/Converters.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 |
---|---|---|
@@ -1,27 +1,23 @@ | ||
package cloud.pablos.overload.data | ||
|
||
class Converters { | ||
/*@TypeConverter | ||
fun iconToString(icon: ImageVector): String { | ||
return icon.name.split(".")[1] | ||
} | ||
@TypeConverter | ||
fun stringToIcon(name: String): ImageVector { | ||
val cl = Class.forName("androidx.compose.material.icons.filled.${name}Kt") | ||
val method = cl.declaredMethods.first() | ||
return method.invoke(null, Icons.Filled) as ImageVector | ||
} | ||
import androidx.compose.ui.graphics.Color | ||
|
||
class Converters { | ||
companion object { | ||
fun iconToString(icon: ImageVector): String { | ||
return icon.name.split(".")[1] | ||
fun convertColorToLong(color: Color): Long { | ||
val alpha = 255 // (color.alpha * 255).toInt() | ||
val red = (color.red * 255).toInt() | ||
val green = (color.green * 255).toInt() | ||
val blue = (color.blue * 255).toInt() | ||
return (alpha.toLong() shl 24) or (red.toLong() shl 16) or (green.toLong() shl 8) or blue.toLong() | ||
} | ||
|
||
fun stringToIcon(name: String): ImageVector { | ||
val cl = Class.forName("androidx.compose.material.icons.filled.${name}Kt") | ||
val method = cl.declaredMethods.first() | ||
return method.invoke(null, Icons.Filled) as ImageVector | ||
fun convertLongToColor(value: Long): Color { | ||
val alpha = 1f // (value shr 24 and 0xFF).toFloat() / 255f | ||
val red = (value shr 16 and 0xFF).toFloat() / 255f | ||
val green = (value shr 8 and 0xFF).toFloat() / 255f | ||
val blue = (value and 0xFF).toFloat() / 255f | ||
return Color(red, green, blue, alpha) | ||
} | ||
}*/ | ||
} | ||
} |
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,57 @@ | ||
package cloud.pablos.overload.data | ||
|
||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.graphics.Color | ||
import cloud.pablos.overload.data.category.CategoryState | ||
import kotlin.math.max | ||
import kotlin.math.min | ||
import kotlin.math.pow | ||
|
||
class Helpers { | ||
companion object { | ||
private fun calculateRelativeLuminance(color: Color): Double { | ||
val red = if (color.red <= 0.03928) color.red / 12.92 else ((color.red + 0.055) / 1.055).pow(2.4) | ||
val green = if (color.green <= 0.03928) color.green / 12.92 else ((color.green + 0.055) / 1.055).pow(2.4) | ||
val blue = if (color.blue <= 0.03928) color.blue / 12.92 else ((color.blue + 0.055) / 1.055).pow(2.4) | ||
return 0.2126 * red + 0.7152 * green + 0.0722 * blue | ||
} | ||
|
||
private fun calculateContrastRatio( | ||
background: Color, | ||
foreground: Color, | ||
): Double { | ||
val lum1 = calculateRelativeLuminance(background) | ||
val lum2 = calculateRelativeLuminance(foreground) | ||
val lighter = max(lum1, lum2) | ||
val darker = min(lum1, lum2) | ||
return (lighter + 0.05) / (darker + 0.05) | ||
} | ||
|
||
@Composable | ||
fun decideBackground(categoryState: CategoryState): Color { | ||
return ( | ||
categoryState.categories.find { category -> | ||
category.id == categoryState.selectedCategory | ||
} | ||
) | ||
?.let { | ||
if (it.isDefault) { | ||
MaterialTheme.colorScheme.primaryContainer | ||
} else { | ||
Converters.convertLongToColor(it.color) | ||
} | ||
} | ||
?: MaterialTheme.colorScheme.primaryContainer | ||
} | ||
|
||
@Composable | ||
fun decideForeground(background: Color): Color { | ||
val white = MaterialTheme.colorScheme.background | ||
val black = MaterialTheme.colorScheme.onBackground | ||
val contrastWithWhite = calculateContrastRatio(background, white) | ||
val contrastWithBlack = calculateContrastRatio(background, black) | ||
return if (contrastWithWhite >= contrastWithBlack) white else black | ||
} | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.