Skip to content

Commit 6ae68ec

Browse files
committed
theming: change primary_color to ColorProperty
`dynamic_color`: add more color roles
1 parent 7971b36 commit 6ae68ec

File tree

2 files changed

+232
-26
lines changed

2 files changed

+232
-26
lines changed

kivymd/dynamic_color.py

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,3 +630,195 @@ class DynamicColor:
630630
:attr:`rippleColor` is an :class:`~kivy.properties.ColorProperty`
631631
and defaults to `'#BDBDBD'`.
632632
"""
633+
634+
colorAccentPrimaryColor = ColorProperty()
635+
"""
636+
Color accent primary.
637+
638+
:attr:`colorAccentPrimaryColor` is an :class:`~kivy.properties.ColorProperty`
639+
and defaults to `None`.
640+
"""
641+
642+
colorAccentPrimaryVariantColor = ColorProperty()
643+
"""
644+
Color accent primary variant.
645+
646+
:attr:`colorAccentPrimaryVariantColor` is an :class:`~kivy.properties.ColorProperty`
647+
and defaults to `None`.
648+
"""
649+
650+
colorAccentSecondaryColor = ColorProperty()
651+
"""
652+
Color accent secondary.
653+
654+
:attr:`colorAccentSecondaryColor` is an :class:`~kivy.properties.ColorProperty`
655+
and defaults to `None`.
656+
"""
657+
658+
colorAccentSecondaryVariantColor = ColorProperty()
659+
"""
660+
Color accent secondary variant.
661+
662+
:attr:`colorAccentSecondaryVariantColor` is an :class:`~kivy.properties.ColorProperty`
663+
and defaults to `None`.
664+
"""
665+
666+
colorAccentTertiaryColor = ColorProperty()
667+
"""
668+
Color accent tertiary.
669+
670+
:attr:`colorAccentTertiaryColor` is an :class:`~kivy.properties.ColorProperty`
671+
and defaults to `None`.
672+
"""
673+
674+
colorAccentTertiaryVariantColor = ColorProperty()
675+
"""
676+
Color accent tertiary variant.
677+
678+
:attr:`colorAccentTertiaryVariantColor` is an :class:`~kivy.properties.ColorProperty`
679+
and defaults to `None`.
680+
"""
681+
682+
textColorPrimaryColor = ColorProperty()
683+
"""
684+
Text color primary.
685+
686+
:attr:`textColorPrimaryColor` is an :class:`~kivy.properties.ColorProperty`
687+
and defaults to `None`.
688+
"""
689+
690+
textColorSecondaryColor = ColorProperty()
691+
"""
692+
Text color secondary.
693+
694+
:attr:`textColorSecondaryColor` is an :class:`~kivy.properties.ColorProperty`
695+
and defaults to `None`.
696+
"""
697+
698+
textColorTertiaryColor = ColorProperty()
699+
"""
700+
Text color tertiary.
701+
702+
:attr:`textColorTertiaryColor` is an :class:`~kivy.properties.ColorProperty`
703+
and defaults to `None`.
704+
"""
705+
706+
textColorPrimaryInverseColor = ColorProperty()
707+
"""
708+
Text color primary inverse.
709+
710+
:attr:`textColorPrimaryInverseColor` is an :class:`~kivy.properties.ColorProperty`
711+
and defaults to `None`.
712+
"""
713+
714+
textColorSecondaryInverseColor = ColorProperty()
715+
"""
716+
Text color secondary inverse.
717+
718+
:attr:`textColorSecondaryInverseColor` is an :class:`~kivy.properties.ColorProperty`
719+
and defaults to `None`.
720+
"""
721+
722+
textColorTertiaryInverseColor = ColorProperty()
723+
"""
724+
Text color tertiary inverse.
725+
726+
:attr:`textColorTertiaryInverseColor` is an :class:`~kivy.properties.ColorProperty`
727+
and defaults to `None`.
728+
"""
729+
730+
colorBackgroundColor = ColorProperty()
731+
"""
732+
Color background.
733+
734+
:attr:`colorBackgroundColor` is an :class:`~kivy.properties.ColorProperty`
735+
and defaults to `None`.
736+
"""
737+
738+
colorBackgroundFloatingColor = ColorProperty()
739+
"""
740+
Color background floating.
741+
742+
:attr:`colorBackgroundFloatingColor` is an :class:`~kivy.properties.ColorProperty`
743+
and defaults to `None`.
744+
"""
745+
746+
colorSurfaceColor = ColorProperty()
747+
"""
748+
Color surface.
749+
750+
:attr:`colorSurfaceColor` is an :class:`~kivy.properties.ColorProperty`
751+
and defaults to `None`.
752+
"""
753+
754+
colorSurfaceVariantColor = ColorProperty()
755+
"""
756+
Color surface variant.
757+
758+
:attr:`colorSurfaceVariantColor` is an :class:`~kivy.properties.ColorProperty`
759+
and defaults to `None`.
760+
"""
761+
762+
colorSurfaceHighlightColor = ColorProperty()
763+
"""
764+
Color surface highlight.
765+
766+
:attr:`colorSurfaceHighlightColor` is an :class:`~kivy.properties.ColorProperty`
767+
and defaults to `None`.
768+
"""
769+
770+
surfaceHeaderColor = ColorProperty()
771+
"""
772+
Surface header.
773+
774+
:attr:`surfaceHeaderColor` is an :class:`~kivy.properties.ColorProperty`
775+
and defaults to `None`.
776+
"""
777+
778+
underSurfaceColor = ColorProperty()
779+
"""
780+
Under surface.
781+
782+
:attr:`underSurfaceColor` is an :class:`~kivy.properties.ColorProperty`
783+
and defaults to `None`.
784+
"""
785+
786+
offStateColor = ColorProperty()
787+
"""
788+
Off state.
789+
790+
:attr:`offStateColor` is an :class:`~kivy.properties.ColorProperty`
791+
and defaults to `None`.
792+
"""
793+
794+
accentSurfaceColor = ColorProperty()
795+
"""
796+
Accent surface.
797+
798+
:attr:`accentSurfaceColor` is an :class:`~kivy.properties.ColorProperty`
799+
and defaults to `None`.
800+
"""
801+
802+
textPrimaryOnAccentColor = ColorProperty()
803+
"""
804+
Text primary on accent.
805+
806+
:attr:`textPrimaryOnAccentColor` is an :class:`~kivy.properties.ColorProperty`
807+
and defaults to `None`.
808+
"""
809+
810+
textSecondaryOnAccentColor = ColorProperty()
811+
"""
812+
Text secondary on accent.
813+
814+
:attr:`textSecondaryOnAccentColor` is an :class:`~kivy.properties.ColorProperty`
815+
and defaults to `None`.
816+
"""
817+
818+
volumeBackgroundColor = ColorProperty()
819+
"""
820+
Volume background.
821+
822+
:attr:`volumeBackgroundColor` is an :class:`~kivy.properties.ColorProperty`
823+
and defaults to `None`.
824+
"""

kivymd/theming.py

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,46 +26,50 @@
2626
import os.path
2727
from timeit import default_timer
2828

29+
import kivy.utils
30+
from kivy import platform
2931
from kivy.app import App
30-
from kivy.logger import Logger
3132
from kivy.core.window import Window
3233
from kivy.event import EventDispatcher
34+
from kivy.logger import Logger
3335
from kivy.properties import (
3436
AliasProperty,
3537
BooleanProperty,
38+
ColorProperty,
3639
DictProperty,
3740
NumericProperty,
3841
ObjectProperty,
3942
OptionProperty,
4043
StringProperty,
4144
)
42-
from kivy import platform
4345
from kivy.utils import get_color_from_hex, rgba, hex_colormap
46+
from materialyoucolor.dislike.dislike_analyzer import DislikeAnalyzer
47+
from materialyoucolor.dynamiccolor.material_dynamic_colors import (
48+
MaterialDynamicColors,
49+
)
50+
from materialyoucolor.hct import Hct
51+
from materialyoucolor.scheme.scheme_android import SchemeAndroid
52+
from materialyoucolor.utils.color_utils import argb_from_rgba_01
53+
from materialyoucolor.utils.platform_utils import SCHEMES, get_dynamic_scheme
4454

4555
from kivymd.dynamic_color import DynamicColor
4656
from kivymd.font_definitions import theme_font_styles
4757
from kivymd.material_resources import DEVICE_IOS
4858

49-
from materialyoucolor.utils.color_utils import argb_from_rgba_01
50-
from materialyoucolor.dynamiccolor.material_dynamic_colors import (
51-
MaterialDynamicColors,
52-
)
53-
from materialyoucolor.utils.platform_utils import SCHEMES, get_dynamic_scheme
54-
from materialyoucolor.hct import Hct
55-
from materialyoucolor.dislike.dislike_analyzer import DislikeAnalyzer
59+
60+
# A small patch to support color names even when they are not in lower case
61+
kivy.utils.colormap = type("_colormap",(),{
62+
"get": staticmethod(
63+
lambda value, *args: kivy.utils.colormap.get(value.lower(), *args)
64+
)},)()
5665

5766

5867
class ThemeManager(EventDispatcher, DynamicColor):
59-
primary_palette = OptionProperty(
60-
None,
61-
options=[name_color.capitalize() for name_color in hex_colormap.keys()],
62-
)
68+
primary_palette = ColorProperty("blue")
6369
"""
64-
The name of the color scheme that the application will use.
70+
The color which will be used to generate scheme.
6571
All major `material` components will have the color
66-
of the specified color theme.
67-
68-
See :attr:`kivy.utils.hex_colormap` keys for available values.
72+
of the generated color scheme.
6973
7074
To change the color scheme of an application:
7175
@@ -140,8 +144,8 @@ def build(self):
140144
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/primary-palette-m3.png
141145
:align: center
142146
143-
:attr:`primary_palette` is an :class:`~kivy.properties.OptionProperty`
144-
and defaults to `None`.
147+
:attr:`primary_palette` is an :class:`~kivy.properties.ColorProperty`
148+
and defaults to `blue`.
145149
"""
146150

147151
dynamic_color_quality = NumericProperty(1 if platform == "android" else 10)
@@ -237,7 +241,7 @@ def callback(permission, results):
237241
:attr:`dynamic_color` is an :class:`~kivy.properties.BooleanProperty`
238242
and defaults to `False`.
239243
"""
240-
244+
241245
dynamic_scheme_name = OptionProperty("TONAL_SPOT", options=SCHEMES.keys())
242246
"""
243247
Name of the dynamic scheme. Availabe schemes `TONAL_SPOT`, `SPRITZ`
@@ -640,7 +644,7 @@ def build(self):
640644
"""
641645

642646
_size_current_wallpaper = NumericProperty(0)
643-
_dark_mode = lambda self : False if self.theme_style == "Light" else True
647+
_dark_mode = lambda self: False if self.theme_style == "Light" else True
644648

645649
def __init__(self, **kwargs):
646650
super().__init__(**kwargs)
@@ -663,7 +667,7 @@ def set_colors(self, *args) -> None:
663667
fallback_wallpaper_path=self.path_to_wallpaper,
664668
fallback_scheme_name=self.dynamic_scheme_name,
665669
message_logger=Logger.info,
666-
logger_head="KivyMD"
670+
logger_head="KivyMD",
667671
)
668672
if system_scheme:
669673
self._set_color_names(system_scheme)
@@ -700,12 +704,10 @@ def sync_theme_styles(self, *args) -> None:
700704

701705
def _set_application_scheme(
702706
self,
703-
color = "blue", # Google default
707+
color=[0, 0, 1, 1], # Google default
704708
) -> None:
705709
if not color:
706-
color = "blue"
707-
708-
color = get_color_from_hex(hex_colormap[color.lower()])
710+
color = [0, 0, 1, 1]
709711
color = Hct.from_int(argb_from_rgba_01(color))
710712
color = DislikeAnalyzer.fix_if_disliked(color).to_int()
711713

@@ -718,10 +720,22 @@ def _set_application_scheme(
718720
)
719721

720722
def _set_color_names(self, scheme) -> None:
723+
# Dynamic colors
724+
_added_colors = []
721725
for color_name in vars(MaterialDynamicColors).keys():
722726
attr = getattr(MaterialDynamicColors, color_name)
723727
if hasattr(attr, "get_hct"):
724728
color_value = rgba(attr.get_hct(scheme).to_rgba())
729+
_added_colors.append(color_name)
730+
exec(f"self.{color_name}Color = {color_value}")
731+
732+
# Static colors
733+
static_scheme = getattr(SchemeAndroid, self.theme_style.lower())(
734+
scheme.source_color_argb
735+
)
736+
for color_name in static_scheme.props.keys():
737+
if color_name not in _added_colors: # prefer dynamic
738+
color_value = rgba(static_scheme.props[color_name])
725739
exec(f"self.{color_name}Color = {color_value}")
726740

727741
self.disabledTextColor = self._get_disabled_hint_text_color()

0 commit comments

Comments
 (0)