Skip to content

Commit

Permalink
theming: change primary_color to ColorProperty
Browse files Browse the repository at this point in the history
`dynamic_color`: add more color roles
  • Loading branch information
T-Dynamos committed May 28, 2024
1 parent 7971b36 commit 6ae68ec
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 26 deletions.
192 changes: 192 additions & 0 deletions kivymd/dynamic_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,3 +630,195 @@ class DynamicColor:
:attr:`rippleColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `'#BDBDBD'`.
"""

colorAccentPrimaryColor = ColorProperty()
"""
Color accent primary.
:attr:`colorAccentPrimaryColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

colorAccentPrimaryVariantColor = ColorProperty()
"""
Color accent primary variant.
:attr:`colorAccentPrimaryVariantColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

colorAccentSecondaryColor = ColorProperty()
"""
Color accent secondary.
:attr:`colorAccentSecondaryColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

colorAccentSecondaryVariantColor = ColorProperty()
"""
Color accent secondary variant.
:attr:`colorAccentSecondaryVariantColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

colorAccentTertiaryColor = ColorProperty()
"""
Color accent tertiary.
:attr:`colorAccentTertiaryColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

colorAccentTertiaryVariantColor = ColorProperty()
"""
Color accent tertiary variant.
:attr:`colorAccentTertiaryVariantColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

textColorPrimaryColor = ColorProperty()
"""
Text color primary.
:attr:`textColorPrimaryColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

textColorSecondaryColor = ColorProperty()
"""
Text color secondary.
:attr:`textColorSecondaryColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

textColorTertiaryColor = ColorProperty()
"""
Text color tertiary.
:attr:`textColorTertiaryColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

textColorPrimaryInverseColor = ColorProperty()
"""
Text color primary inverse.
:attr:`textColorPrimaryInverseColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

textColorSecondaryInverseColor = ColorProperty()
"""
Text color secondary inverse.
:attr:`textColorSecondaryInverseColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

textColorTertiaryInverseColor = ColorProperty()
"""
Text color tertiary inverse.
:attr:`textColorTertiaryInverseColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

colorBackgroundColor = ColorProperty()
"""
Color background.
:attr:`colorBackgroundColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

colorBackgroundFloatingColor = ColorProperty()
"""
Color background floating.
:attr:`colorBackgroundFloatingColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

colorSurfaceColor = ColorProperty()
"""
Color surface.
:attr:`colorSurfaceColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

colorSurfaceVariantColor = ColorProperty()
"""
Color surface variant.
:attr:`colorSurfaceVariantColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

colorSurfaceHighlightColor = ColorProperty()
"""
Color surface highlight.
:attr:`colorSurfaceHighlightColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

surfaceHeaderColor = ColorProperty()
"""
Surface header.
:attr:`surfaceHeaderColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

underSurfaceColor = ColorProperty()
"""
Under surface.
:attr:`underSurfaceColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

offStateColor = ColorProperty()
"""
Off state.
:attr:`offStateColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

accentSurfaceColor = ColorProperty()
"""
Accent surface.
:attr:`accentSurfaceColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

textPrimaryOnAccentColor = ColorProperty()
"""
Text primary on accent.
:attr:`textPrimaryOnAccentColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

textSecondaryOnAccentColor = ColorProperty()
"""
Text secondary on accent.
:attr:`textSecondaryOnAccentColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""

volumeBackgroundColor = ColorProperty()
"""
Volume background.
:attr:`volumeBackgroundColor` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""
66 changes: 40 additions & 26 deletions kivymd/theming.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,46 +26,50 @@
import os.path
from timeit import default_timer

import kivy.utils
from kivy import platform
from kivy.app import App
from kivy.logger import Logger
from kivy.core.window import Window
from kivy.event import EventDispatcher
from kivy.logger import Logger
from kivy.properties import (
AliasProperty,
BooleanProperty,
ColorProperty,
DictProperty,
NumericProperty,
ObjectProperty,
OptionProperty,
StringProperty,
)
from kivy import platform
from kivy.utils import get_color_from_hex, rgba, hex_colormap
from materialyoucolor.dislike.dislike_analyzer import DislikeAnalyzer
from materialyoucolor.dynamiccolor.material_dynamic_colors import (
MaterialDynamicColors,
)
from materialyoucolor.hct import Hct
from materialyoucolor.scheme.scheme_android import SchemeAndroid
from materialyoucolor.utils.color_utils import argb_from_rgba_01
from materialyoucolor.utils.platform_utils import SCHEMES, get_dynamic_scheme

from kivymd.dynamic_color import DynamicColor
from kivymd.font_definitions import theme_font_styles
from kivymd.material_resources import DEVICE_IOS

from materialyoucolor.utils.color_utils import argb_from_rgba_01
from materialyoucolor.dynamiccolor.material_dynamic_colors import (
MaterialDynamicColors,
)
from materialyoucolor.utils.platform_utils import SCHEMES, get_dynamic_scheme
from materialyoucolor.hct import Hct
from materialyoucolor.dislike.dislike_analyzer import DislikeAnalyzer

# A small patch to support color names even when they are not in lower case
kivy.utils.colormap = type("_colormap",(),{
"get": staticmethod(
lambda value, *args: kivy.utils.colormap.get(value.lower(), *args)
)},)()


class ThemeManager(EventDispatcher, DynamicColor):
primary_palette = OptionProperty(
None,
options=[name_color.capitalize() for name_color in hex_colormap.keys()],
)
primary_palette = ColorProperty("blue")
"""
The name of the color scheme that the application will use.
The color which will be used to generate scheme.
All major `material` components will have the color
of the specified color theme.
See :attr:`kivy.utils.hex_colormap` keys for available values.
of the generated color scheme.
To change the color scheme of an application:
Expand Down Expand Up @@ -140,8 +144,8 @@ def build(self):
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/primary-palette-m3.png
:align: center
:attr:`primary_palette` is an :class:`~kivy.properties.OptionProperty`
and defaults to `None`.
:attr:`primary_palette` is an :class:`~kivy.properties.ColorProperty`
and defaults to `blue`.
"""

dynamic_color_quality = NumericProperty(1 if platform == "android" else 10)
Expand Down Expand Up @@ -237,7 +241,7 @@ def callback(permission, results):
:attr:`dynamic_color` is an :class:`~kivy.properties.BooleanProperty`
and defaults to `False`.
"""

dynamic_scheme_name = OptionProperty("TONAL_SPOT", options=SCHEMES.keys())
"""
Name of the dynamic scheme. Availabe schemes `TONAL_SPOT`, `SPRITZ`
Expand Down Expand Up @@ -640,7 +644,7 @@ def build(self):
"""

_size_current_wallpaper = NumericProperty(0)
_dark_mode = lambda self : False if self.theme_style == "Light" else True
_dark_mode = lambda self: False if self.theme_style == "Light" else True

def __init__(self, **kwargs):
super().__init__(**kwargs)
Expand All @@ -663,7 +667,7 @@ def set_colors(self, *args) -> None:
fallback_wallpaper_path=self.path_to_wallpaper,
fallback_scheme_name=self.dynamic_scheme_name,
message_logger=Logger.info,
logger_head="KivyMD"
logger_head="KivyMD",
)
if system_scheme:
self._set_color_names(system_scheme)
Expand Down Expand Up @@ -700,12 +704,10 @@ def sync_theme_styles(self, *args) -> None:

def _set_application_scheme(
self,
color = "blue", # Google default
color=[0, 0, 1, 1], # Google default
) -> None:
if not color:
color = "blue"

color = get_color_from_hex(hex_colormap[color.lower()])
color = [0, 0, 1, 1]
color = Hct.from_int(argb_from_rgba_01(color))
color = DislikeAnalyzer.fix_if_disliked(color).to_int()

Expand All @@ -718,10 +720,22 @@ def _set_application_scheme(
)

def _set_color_names(self, scheme) -> None:
# Dynamic colors
_added_colors = []
for color_name in vars(MaterialDynamicColors).keys():
attr = getattr(MaterialDynamicColors, color_name)
if hasattr(attr, "get_hct"):
color_value = rgba(attr.get_hct(scheme).to_rgba())
_added_colors.append(color_name)
exec(f"self.{color_name}Color = {color_value}")

# Static colors
static_scheme = getattr(SchemeAndroid, self.theme_style.lower())(
scheme.source_color_argb
)
for color_name in static_scheme.props.keys():
if color_name not in _added_colors: # prefer dynamic
color_value = rgba(static_scheme.props[color_name])
exec(f"self.{color_name}Color = {color_value}")

self.disabledTextColor = self._get_disabled_hint_text_color()
Expand Down

3 comments on commit 6ae68ec

@HeaTTheatR
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is not necessary to make changes with dynamic color yet. This will all be changed in the near future.

@T-Dynamos
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, just a reminder to add support for SchemeAndroid colors, also.

@HeaTTheatR
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine. But to dynamically change colors in Python code, I changed and added a lot. So it's best not to make changes related to dynamic color for now.

Please sign in to comment.