All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.jetbrains.datalore.plot.config.theme.ThemeConfig.kt Maven / Gradle / Ivy

There is a newer version: 4.5.3-alpha1
Show newest version
/*
 * Copyright (c) 2019. JetBrains s.r.o.
 * Use of this source code is governed by the MIT license that can be found in the LICENSE file.
 */

package jetbrains.datalore.plot.config.theme

import jetbrains.datalore.plot.builder.defaultTheme.DefaultTheme
import jetbrains.datalore.plot.builder.defaultTheme.ThemeFlavor
import jetbrains.datalore.plot.builder.defaultTheme.values.ThemeOption
import jetbrains.datalore.plot.builder.defaultTheme.values.ThemeOption.ELEMENT_BLANK
import jetbrains.datalore.plot.builder.defaultTheme.values.ThemeValues
import jetbrains.datalore.plot.builder.defaultTheme.values.ThemeValues.Companion.mergeWith
import jetbrains.datalore.plot.builder.presentation.FontFamilyRegistry
import jetbrains.datalore.plot.builder.theme.Theme
import jetbrains.datalore.plot.config.Option
import jetbrains.datalore.plot.config.getString

class ThemeConfig constructor(
    themeSettings: Map = emptyMap(),
    fontFamilyRegistry: FontFamilyRegistry
) {

    val theme: Theme

    init {

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

        // Make sure all values are converted to proper objects.
        @Suppress("NAME_SHADOWING")
        val userOptions: Map = themeSettings.mapValues { (key, value) ->
            var value = convertElementBlank(value)
            value = convertMargins(value)
            LegendThemeConfig.convertValue(key, value)
        }

        // User specific options will be applied to the combination of named theme and flavor options
        val effectiveOptions = baselineValues.values.let {
            val flavorName = themeSettings.getString(Option.Theme.FLAVOR)
            if (flavorName != null) {
                ThemeFlavor.forName(flavorName).updateColors(it)
            } else {
                it
            }
        }.mergeWith(userOptions)

        theme = DefaultTheme(effectiveOptions, fontFamilyRegistry)
    }

    companion object {

        /**
         * Converts a simple "blank" string to a 'blank element'.
         */
        private fun convertElementBlank(value: Any): Any {
            if (value is String && value == ThemeOption.ELEMENT_BLANK_SHORTHAND) {
                return ELEMENT_BLANK
            }
            if (value is Map<*, *> && value["name"] == "blank") {
                return ELEMENT_BLANK
            }
            return value
        }

        private fun convertMargins(value: Any): Any {
            return if (value is Map<*, *> && ThemeOption.Elem.MARGIN in value) {
                val oldMargins = value[ThemeOption.Elem.MARGIN] as Map<*, *>
                val newMargins = oldMargins.map { (k, v) -> ThemeOption.Elem.MARGIN + "_" + k to v }
                value - ThemeOption.Elem.MARGIN + newMargins
            } else {
                value
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy