Skip to content

Instantly share code, notes, and snippets.

@hakanai
Created October 24, 2021 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakanai/987b501d0de72b256e8733db06470e10 to your computer and use it in GitHub Desktop.
Save hakanai/987b501d0de72b256e8733db06470e10 to your computer and use it in GitHub Desktop.
Minimal(?) example of custom theme in Jetpack Compose
import androidx.compose.desktop.Window
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color
fun isSystemInDarkTheme(): Boolean {
// TODO: On Android, presumably this is `AppCompatDelegate.getDefaultNightMode()`.
// What is it on Desktop?
return false
}
@Composable
fun MyTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
content: @Composable () -> Unit
) {
// The only two values I actually have in the brand
val desire = Color(0xe8, 0x37, 0x55)
val tuftsBlue = Color(0x41, 0x98, 0xdf)
// The rest I still have to provide for some reason, presumably Compose
// is too lazy to apply whatever function is necessary to darken or lighten
// a colour slightly?
val primaryColor = desire
val primaryLightColor = Color(0xff, 0x6f, 0x81)
val primaryDarkColor = Color(0xaf, 0x00, 0x2d)
val secondaryColor = tuftsBlue
val secondaryLightColor = Color(0x7c, 0xc8, 0xff)
val secondaryDarkColor = Color(0x00, 0x6a, 0xad)
// Also note how we have to figure out for ourselves how the terminology
// in the theme creation tools maps to the terminology in the APIs.
val colors = if (darkTheme) {
darkColors(
primary = primaryColor,
primaryVariant = primaryLightColor,
secondary = secondaryColor,
secondaryVariant = secondaryLightColor
)
} else {
lightColors(
primary = primaryColor,
primaryVariant = primaryDarkColor,
secondary = secondaryColor,
secondaryVariant = secondaryDarkColor
)
}
MaterialTheme(colors = colors) {
content()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment