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

Refactor code of theme/flavors, theme_none() uses blue colors #891

Merged
merged 8 commits into from
Sep 29, 2023
Prev Previous commit
Next Next commit
Add function to prepare ready-made parameters for theme creation.
  • Loading branch information
OLarionova-HORIS committed Sep 28, 2023
commit 1fa8c0aba6b532788f26cf82f62a175c2c1c95c6
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import org.jetbrains.letsPlot.core.plot.base.scale.breaks.ScaleBreaksUtil
import org.jetbrains.letsPlot.core.plot.builder.AxisUtil
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.DefaultTheme
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeValuesRClassic
import org.jetbrains.letsPlot.core.plot.builder.guide.AxisComponent
import org.jetbrains.letsPlot.core.plot.builder.guide.Orientation
import demo.plot.common.model.SimpleDemoBase
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.ThemeOptionsUtil
import org.jetbrains.letsPlot.datamodel.svg.dom.SvgRectElement
import org.jetbrains.letsPlot.datamodel.svg.dom.SvgSvgElement

Expand Down Expand Up @@ -135,11 +135,13 @@ open class AxisComponentDemo : SimpleDemoBase(DEMO_BOX_SIZE) {
// axis.gridLineWidth.set(Plot.Axis.GRID_LINE_WIDTH)
// axis.gridLineLength.set(100.0)

val baselineValues = ThemeValuesRClassic()
val themeOptions = baselineValues + mapOf(
ThemeOption.PANEL_GRID to mapOf(ThemeOption.Elem.COLOR to Color.RED)
val themeOptions = ThemeOptionsUtil.prepareThemeOptions(
ThemeOption.Name.R_CLASSIC,
userOptions = mapOf(
ThemeOption.PANEL_GRID to mapOf(ThemeOption.Elem.COLOR to Color.RED)
)
)
val theme = DefaultTheme.withBaseFlavor(themeOptions)
val theme = DefaultTheme(themeOptions)

val axis = AxisComponent(
length = axisLength,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ import org.jetbrains.letsPlot.core.plot.base.scale.breaks.QuantizeScale
import org.jetbrains.letsPlot.core.plot.base.scale.transform.Transforms
import org.jetbrains.letsPlot.core.plot.builder.AxisUtil
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.DefaultTheme
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeValuesRClassic
import org.jetbrains.letsPlot.core.plot.builder.guide.AxisComponent
import org.jetbrains.letsPlot.core.plot.builder.guide.Orientation
import org.jetbrains.letsPlot.core.commons.color.ColorPalette
import org.jetbrains.letsPlot.core.commons.color.ColorScheme
import org.jetbrains.letsPlot.core.commons.color.PaletteUtil.schemeColors
import demo.plot.common.model.SimpleDemoBase
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.ThemeOptionsUtil
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption

open class ScatterDemo : SimpleDemoBase() {

Expand All @@ -46,6 +47,13 @@ open class ScatterDemo : SimpleDemoBase() {
)
}

private fun classicTheme() = DefaultTheme(
ThemeOptionsUtil.prepareThemeOptions(
ThemeOption.Name.LP_MINIMAL,
userOptions = emptyMap()
)
)

private fun gauss(): GroupComponent {
val count = 200
val a = normal(count, 0.0, 100.0, 32) // X
Expand Down Expand Up @@ -97,7 +105,7 @@ open class ScatterDemo : SimpleDemoBase() {
// Render
val groupComponent = GroupComponent()

val theme = DefaultTheme.withBaseFlavor(ThemeValuesRClassic().values)
val theme = classicTheme()

run {
// X axis
Expand Down Expand Up @@ -281,7 +289,7 @@ open class ScatterDemo : SimpleDemoBase() {
// coord system
val coord = Coords.DemoAndTest.create(domainX, domainY, demoInnerSize)

val theme = DefaultTheme.withBaseFlavor(ThemeValuesRClassic().values)
val theme = classicTheme()

run {
// X axis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ class DefaultTheme(

companion object {
// For demo and tests
fun minimal2() = withBaseFlavor(ThemeValuesLPMinimal2().values)

fun withBaseFlavor(themeValues: Map<String, Any>) = DefaultTheme(
ThemeFlavorUtil.applyFlavor(themeValues, Flavor.BASE)
fun minimal2() = DefaultTheme(
ThemeFlavorUtil.applyFlavor(ThemeValuesLPMinimal2().values, Flavor.BASE)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.Elem
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeValues.Companion.mergeWith

object ThemeFlavorUtil {
internal object ThemeFlavorUtil {

fun applyFlavor(themeSettings: Map<String, Any>, flavorName: String): Map<String, Any> {
val flavor = createFlavor(flavorName)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2023. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/

package org.jetbrains.letsPlot.core.plot.builder.defaultTheme

import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeValues
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeValues.Companion.mergeWith

object ThemeOptionsUtil {

fun prepareThemeOptions(
themeName: String,
userOptions: Map<String, Any>
): Map<String, Any> {
val baselineValues = ThemeValues.forName(themeName).values

val flavorName = userOptions[ThemeOption.FLAVOR] as? String
?: baselineValues[ThemeOption.FLAVOR] as? String
?: error("Flavor name should be specified")

return ThemeFlavorUtil.applyFlavor(baselineValues, flavorName)
.mergeWith(userOptions)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values

abstract class ThemeValues(
internal abstract class ThemeValues(
val values: Map<String, Any>
) {
operator fun plus(other: Map<String, Any>): Map<String, Any> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.TOOLTIP_TITLE_TEXT
import org.jetbrains.letsPlot.core.plot.builder.presentation.Defaults

open class ThemeValuesBase : ThemeValues(VALUES) {
internal open class ThemeValuesBase : ThemeValues(VALUES) {

companion object {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.RECT
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.TEXT

class ThemeValuesLPMinimal2 : ThemeValues(VALUES) {
internal class ThemeValuesLPMinimal2 : ThemeValues(VALUES) {

companion object {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

package org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values

class ThemeValuesLPNone : ThemeValuesBase()
internal class ThemeValuesLPNone : ThemeValuesBase()

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.RECT
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.TEXT

class ThemeValuesRBW : ThemeValues(VALUES) {
internal class ThemeValuesRBW : ThemeValues(VALUES) {

companion object {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.RECT
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.TEXT

class ThemeValuesRClassic : ThemeValues(VALUES) {
internal class ThemeValuesRClassic : ThemeValues(VALUES) {

companion object {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.RECT
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.TEXT

class ThemeValuesRGrey : ThemeValues(VALUES) {
internal class ThemeValuesRGrey : ThemeValues(VALUES) {

companion object {
private val VALUES: Map<String, Any> = ThemeValuesBase() + mapOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.RECT
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.TEXT

class ThemeValuesRLight : ThemeValues(VALUES) {
internal class ThemeValuesRLight : ThemeValues(VALUES) {

companion object {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.RECT
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.TEXT

class ThemeValuesRMinimal : ThemeValues(VALUES) {
internal class ThemeValuesRMinimal : ThemeValues(VALUES) {

companion object {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@

package org.jetbrains.letsPlot.core.spec.config

import org.jetbrains.letsPlot.commons.values.Color
import org.jetbrains.letsPlot.core.plot.base.theme.Theme
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.DefaultTheme
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.ThemeFlavorUtil.applyFlavor
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.ThemeOptionsUtil
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.ELEMENT_BLANK
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.FLAVOR
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeValues
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeValues.Companion.mergeWith
import org.jetbrains.letsPlot.core.plot.builder.presentation.FontFamilyRegistry
import org.jetbrains.letsPlot.core.spec.Option
import org.jetbrains.letsPlot.core.spec.getString

class ThemeConfig constructor(
themeSettings: Map<String, Any> = emptyMap(),
Expand All @@ -28,7 +23,6 @@ class ThemeConfig constructor(
init {

val themeName = themeSettings.getOrElse(Option.Meta.NAME) { ThemeOption.Name.LP_MINIMAL }.toString()
val baselineValues = ThemeValues.forName(themeName).values

// Make sure all values are converted to proper objects.
@Suppress("NAME_SHADOWING")
Expand All @@ -37,12 +31,8 @@ class ThemeConfig constructor(
value = convertMargins(value)
LegendThemeConfig.convertValue(key, value)
}
val flavorName = themeSettings.getString(FLAVOR)
?: baselineValues.getString(FLAVOR)
?: ThemeOption.Flavor.BASE

val effectiveOptions = applyFlavor(baselineValues, flavorName)
.mergeWith(userOptions)
val effectiveOptions = ThemeOptionsUtil.prepareThemeOptions(themeName, userOptions)

theme = DefaultTheme(effectiveOptions, fontFamilyRegistry)
}
Expand Down