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

Add element_geom(pen, brush, paper) #836

Merged
merged 15 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Access to colors via ColorTheme (also use for color defaults of geo…
…metries).
  • Loading branch information
OLarionova-HORIS committed Aug 4, 2023
commit 6b270a2ecddc278a6020f206f9dd60ec5ec99b3e
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ import org.jetbrains.letsPlot.commons.values.Color
import org.jetbrains.letsPlot.commons.values.Colors
import org.jetbrains.letsPlot.core.plot.base.GeomKind
import org.jetbrains.letsPlot.core.plot.base.aes.GeomTheme
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.AXIS
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.AXIS_LINE
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.LINE
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.PLOT_BKGR_RECT
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.RECT
import org.jetbrains.letsPlot.core.plot.builder.presentation.DefaultFontFamilyRegistry
import org.jetbrains.letsPlot.core.plot.base.theme.ColorTheme

internal class DefaultGeomTheme private constructor(
private val color: Color,
Expand All @@ -35,18 +29,6 @@ internal class DefaultGeomTheme private constructor(
override fun lineWidth() = lineWidth

companion object {
private class InheritedColors(
options: Map<String, Any>
) : ThemeValuesAccess(options, DefaultFontFamilyRegistry()) {
private val lineKey = listOf(AXIS_LINE, AXIS, LINE)

private val backgroundKey = listOf(PLOT_BKGR_RECT, RECT)

fun lineColor() = getColor(getElemValue(lineKey), ThemeOption.Elem.COLOR)

fun backgroundFill() = getColor(getElemValue(backgroundKey), ThemeOption.Elem.FILL)
}

private class FixedColors(geomKind: GeomKind) {
val color = if (geomKind == GeomKind.SMOOTH) {
Color.MAGENTA
Expand All @@ -57,8 +39,7 @@ internal class DefaultGeomTheme private constructor(
}

// defaults for geomKind
fun forGeomKind(geomKind: GeomKind, themeSettings: Map<String, Any>): GeomTheme {
val inheritedColors = InheritedColors(themeSettings)
fun forGeomKind(geomKind: GeomKind, colorTheme: ColorTheme): GeomTheme {
val fixedColors = FixedColors(geomKind)

var color = fixedColors.color
Expand All @@ -82,13 +63,13 @@ internal class DefaultGeomTheme private constructor(
GeomKind.Q_Q_2_LINE,
GeomKind.ERROR_BAR,
GeomKind.LINE_RANGE -> {
color = inheritedColors.lineColor()
color = colorTheme.pen()
size *= sizeMultiplier
}

GeomKind.CONTOUR,
GeomKind.DENSITY2D -> {
color = inheritedColors.lineColor()
color = colorTheme.pen()
}

GeomKind.AREA_RIDGES,
Expand All @@ -97,50 +78,50 @@ internal class DefaultGeomTheme private constructor(
GeomKind.RECT,
GeomKind.RIBBON,
GeomKind.MAP -> {
color = inheritedColors.lineColor()
fill = Colors.withOpacity(inheritedColors.lineColor(), 0.1)
color = colorTheme.pen()
fill = Colors.withOpacity(colorTheme.pen(), 0.1)
size *= sizeMultiplier
}

GeomKind.VIOLIN,
GeomKind.CROSS_BAR,
GeomKind.BOX_PLOT -> {
color = inheritedColors.lineColor()
fill = inheritedColors.backgroundFill()
color = colorTheme.pen()
fill = colorTheme.paper()
size *= sizeMultiplier
}

GeomKind.POINT,
GeomKind.JITTER,
GeomKind.Q_Q,
GeomKind.Q_Q_2 -> {
color = inheritedColors.lineColor()
fill = inheritedColors.backgroundFill()
color = colorTheme.pen()
fill = colorTheme.paper()
size = 2.0 * sizeMultiplier
lineWidth *= sizeMultiplier
}

GeomKind.DOT_PLOT,
GeomKind.Y_DOT_PLOT -> {
color = inheritedColors.backgroundFill()
color = colorTheme.paper()
}

GeomKind.POINT_RANGE -> {
color = inheritedColors.lineColor()
fill = inheritedColors.backgroundFill()
color = colorTheme.pen()
fill = colorTheme.paper()
size *= sizeMultiplier // mid-point size
lineWidth = 1.0 * sizeMultiplier // line width and stroke for point
}

GeomKind.LOLLIPOP -> {
color = inheritedColors.lineColor()
fill = inheritedColors.backgroundFill()
color = colorTheme.pen()
fill = colorTheme.paper()
size = 2.0 // point size
lineWidth = 1.0 * sizeMultiplier // line width and stroke for point
}

GeomKind.SMOOTH -> {
fill = inheritedColors.lineColor()
fill = colorTheme.pen()
alpha = 0.15
size *= sizeMultiplier
}
Expand All @@ -151,19 +132,19 @@ internal class DefaultGeomTheme private constructor(
}

GeomKind.HISTOGRAM -> {
color = inheritedColors.lineColor()
fill = inheritedColors.lineColor()
color = colorTheme.pen()
fill = colorTheme.pen()
}

GeomKind.POLYGON -> {
color = inheritedColors.backgroundFill()
color = colorTheme.paper()
size *= sizeMultiplier
}

GeomKind.TILE,
GeomKind.BIN_2D -> {
color = Color.TRANSPARENT
fill = inheritedColors.lineColor()
fill = colorTheme.pen()
size *= sizeMultiplier
}

Expand All @@ -174,8 +155,8 @@ internal class DefaultGeomTheme private constructor(
}

GeomKind.TEXT, GeomKind.LABEL -> {
color = inheritedColors.lineColor()
fill = inheritedColors.backgroundFill() // background for label
color = colorTheme.pen()
fill = colorTheme.paper() // background for label
size = 7.0
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class DefaultTheme(

override fun geometries(geomKind: GeomKind): GeomTheme = geometries.getOrPut(geomKind) {
// use settings from named theme and flavor options (without specified in theme())
DefaultGeomTheme.forGeomKind(geomKind, themeSettings)
DefaultGeomTheme.forGeomKind(geomKind, colors)
}

override fun colors(): ColorTheme = colors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.Elem

class ThemeFlavor(
val fill: Color,
val color: Color,
private val fill: Color,
private val color: Color,
private val specialColors: Map<String, Map<String, Color>> = emptyMap(),
) {
fun updateColors(options: Map<String, Any>): Map<String, Any> {
Expand Down Expand Up @@ -46,6 +46,11 @@ class ThemeFlavor(
if (key == ThemeOption.TOOLTIP_RECT) {
specialColors[key]?.get(Elem.FILL)?.let { updated[Elem.FILL] = it }
}

// Set flavor values for named colors
updated[ThemeOption.Geom.PEN] = color
updated[ThemeOption.Geom.PAPER] = fill

updated
} else {
value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ object ThemeOption {
const val TOOLTIP_TEXT = "tooltip_text"
const val TOOLTIP_TITLE_TEXT = "tooltip_title_text"

const val FLAVOR = "flavor"
const val GEOM = "geom"

// view element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ThemeOptionsOrderToApplyTest(
@Parameterized.Parameters
fun params(): Collection<Array<Any?>> {
val namedThemeOption = mapOf(Option.Meta.NAME to ThemeOption.Name.LP_MINIMAL)
val flavorOption = mapOf(Option.Theme.FLAVOR to ThemeOption.Flavor.DARCULA)
val flavorOption = mapOf(ThemeOption.FLAVOR to ThemeOption.Flavor.DARCULA)

return listOf(
arrayOf(namedThemeOption, Color.WHITE, null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,6 @@ object Option {
}

object Theme {
const val FLAVOR = "flavor"

// All other options were moved to
// org.jetbrains.letsPlot.core.plot.builder.theme2.values.ThemeOption

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ abstract class PlotConfig(
run {
val themeConfig = ThemeConfig(getMap(Option.Plot.THEME), DefaultFontFamilyRegistry())
val colorConverter = NamedSystemColorOptionConverter(
NamedSystemColors(
colorTheme = themeConfig.theme.colors(),
themeFlavor = themeConfig.themeFlavor
)
NamedSystemColors(colorTheme = themeConfig.theme.colors())
)
AesOptionConversion.updateWith(colorConverter)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class ThemeConfig constructor(
) {

val theme: Theme
val themeFlavor: ThemeFlavor?

init {

Expand All @@ -36,9 +35,13 @@ class ThemeConfig constructor(
LegendThemeConfig.convertValue(key, value)
}

val themeFlavorOptions = baselineValues.values.let { baseOptions ->
themeFlavor = themeSettings.getString(Option.Theme.FLAVOR)?.let(ThemeFlavor.Companion::forName)
themeFlavor?.updateColors(baseOptions) ?: baseOptions
val themeFlavorOptions = baselineValues.values.let {
val flavorName = themeSettings.getString(ThemeOption.FLAVOR)
if (flavorName != null) {
ThemeFlavor.forName(flavorName).updateColors(it)
} else {
it
}
}

theme = DefaultTheme(themeFlavorOptions, fontFamilyRegistry, userOptions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ package org.jetbrains.letsPlot.core.spec.conversion

import org.jetbrains.letsPlot.commons.values.Color
import org.jetbrains.letsPlot.core.plot.base.theme.ColorTheme
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.ThemeFlavor

class NamedSystemColors(
private val colorTheme: ColorTheme,
private val themeFlavor: ThemeFlavor?
private val colorTheme: ColorTheme
) {
fun getColor(id: String): Color? {
val systemColor = toSystemColor(id) ?: return null
return when (systemColor) {
SystemColor.PEN -> themeFlavor?.color ?: colorTheme.pen()
SystemColor.PAPER -> themeFlavor?.fill ?: colorTheme.paper()
SystemColor.PEN -> colorTheme.pen()
SystemColor.PAPER -> colorTheme.paper()
SystemColor.BRUSH -> colorTheme.brush()
}
}
Expand Down