[MouseFox logo]
The MouseFox Project
Join The Community Discord Server
[Discord logo]
Edit on GitHub

kvex.widgets.color

Home of XPickColor and XSelectColor.

 1"""Home of `XPickColor` and `XSelectColor`."""
 2
 3from .. import kivy as kv
 4from ..colors import XColor
 5from .layouts import XBox
 6from .label import XLabelClick
 7from .dropdown import XDropDown
 8from .slider import XSliderText
 9
10
11class XPickColor(XBox):
12    """Color picking widget."""
13
14    color = kv.ObjectProperty(XColor(0.5, 0.5, 0.5, 1))
15
16    def __init__(self, **kwargs):
17        """Same keyword arguments for Slider."""
18        super().__init__(orientation="vertical")
19        self.set_size(x=300, y=100)
20        update_color = self._update_from_sliders
21        self.sliders = []
22        for i, c in enumerate("RGBA"):
23            slider_kwargs = {
24                "range": (0, 1),
25                "step": 0.01,
26                "value_track": True,
27                "value_track_color": XColor(**{c.lower(): 0.75}).rgba,
28                "value_track_width": "6dp",
29                "cursor_size": (0, 0),
30            } | kwargs
31            s = XSliderText(**slider_kwargs)
32            s.slider.bind(value=update_color)
33            self.add_widget(s)
34            self.sliders.append(s)
35        self.r, self.g, self.b, self.a = self.sliders
36        self.set_color(self.color)
37
38    def set_color(self, color: XColor):
39        """Set the current color."""
40        self.r.slider.value = color.r
41        self.g.slider.value = color.g
42        self.b.slider.value = color.b
43        self.a.slider.value = color.a
44
45    def _update_from_sliders(self, *a):
46        color = XColor(
47            self.r.slider.value,
48            self.g.slider.value,
49            self.b.slider.value,
50            self.a.slider.value,
51        )
52        is_bright = sum(color.rgb) > 1.5
53        for s in self.sliders:
54            s.label.color = (0, 0, 0, 1) if is_bright else (1, 1, 1, 1)
55        self.make_bg(color)
56        self.color = color
57
58
59class XSelectColor(XLabelClick):
60    """An XPickColor that drops down from an XLabelClick."""
61
62    def __init__(
63        self,
64        prefix: str = "[u]Color:[/u]\n",
65        show_color_values: bool = True,
66        **kwargs,
67    ):
68        """Initialize the class.
69
70        Args:
71            prefix: Text to show before the RGB values.
72            show_color_values: Show the RGB values of the current color.
73            kwargs: Keyword arguments for the XLabelClick.
74        """
75        self.prefix = prefix
76        self.show_color_values = show_color_values
77        super().__init__(**kwargs)
78        self.picker = XPickColor()
79        self.dropdown = XDropDown(auto_width=False, on_dismiss=self._on_color)
80        self.dropdown.set_size(*self.picker.size)
81        self.dropdown.add_widget(self.picker)
82        self.picker.bind(size=lambda w, s: self.dropdown.set_size(*s))
83        self.bind(on_release=self.dropdown.open)
84        self.on_color()
85
86    def _on_color(self, *args):
87        color = self.picker.color
88        self.make_bg(color)
89        text = self.prefix
90        if self.show_color_values:
91            text += " , ".join(str(round(c, 2)) for c in color.rgba)
92        self.text = text
93
94
95__all = (
96    "XPickColor",
97    "XSelectColor",
98)
class XPickColor(kvex.widgets.layouts.XBox):
12class XPickColor(XBox):
13    """Color picking widget."""
14
15    color = kv.ObjectProperty(XColor(0.5, 0.5, 0.5, 1))
16
17    def __init__(self, **kwargs):
18        """Same keyword arguments for Slider."""
19        super().__init__(orientation="vertical")
20        self.set_size(x=300, y=100)
21        update_color = self._update_from_sliders
22        self.sliders = []
23        for i, c in enumerate("RGBA"):
24            slider_kwargs = {
25                "range": (0, 1),
26                "step": 0.01,
27                "value_track": True,
28                "value_track_color": XColor(**{c.lower(): 0.75}).rgba,
29                "value_track_width": "6dp",
30                "cursor_size": (0, 0),
31            } | kwargs
32            s = XSliderText(**slider_kwargs)
33            s.slider.bind(value=update_color)
34            self.add_widget(s)
35            self.sliders.append(s)
36        self.r, self.g, self.b, self.a = self.sliders
37        self.set_color(self.color)
38
39    def set_color(self, color: XColor):
40        """Set the current color."""
41        self.r.slider.value = color.r
42        self.g.slider.value = color.g
43        self.b.slider.value = color.b
44        self.a.slider.value = color.a
45
46    def _update_from_sliders(self, *a):
47        color = XColor(
48            self.r.slider.value,
49            self.g.slider.value,
50            self.b.slider.value,
51            self.a.slider.value,
52        )
53        is_bright = sum(color.rgb) > 1.5
54        for s in self.sliders:
55            s.label.color = (0, 0, 0, 1) if is_bright else (1, 1, 1, 1)
56        self.make_bg(color)
57        self.color = color

Color picking widget.

XPickColor(**kwargs)
17    def __init__(self, **kwargs):
18        """Same keyword arguments for Slider."""
19        super().__init__(orientation="vertical")
20        self.set_size(x=300, y=100)
21        update_color = self._update_from_sliders
22        self.sliders = []
23        for i, c in enumerate("RGBA"):
24            slider_kwargs = {
25                "range": (0, 1),
26                "step": 0.01,
27                "value_track": True,
28                "value_track_color": XColor(**{c.lower(): 0.75}).rgba,
29                "value_track_width": "6dp",
30                "cursor_size": (0, 0),
31            } | kwargs
32            s = XSliderText(**slider_kwargs)
33            s.slider.bind(value=update_color)
34            self.add_widget(s)
35            self.sliders.append(s)
36        self.r, self.g, self.b, self.a = self.sliders
37        self.set_color(self.color)

Same keyword arguments for Slider.

color

ObjectProperty(defaultvalue=None, rebind=False, **kw) Property that represents a Python object.

:Parameters:
    `defaultvalue`: object type
        Specifies the default value of the property.
    `rebind`: bool, defaults to False
        Whether kv rules using this object as an intermediate attribute
        in a kv rule, will update the bound property when this object
        changes.

        That is the standard behavior is that if there's a kv rule
        ``text: self.a.b.c.d``, where ``a``, ``b``, and ``c`` are
        properties with ``rebind`` ``False`` and ``d`` is a
        `StringProperty`. Then when the rule is applied, ``text``
        becomes bound only to ``d``. If ``a``, ``b``, or ``c`` change,
        ``text`` still remains bound to ``d``. Furthermore, if any of them
        were ``None`` when the rule was initially evaluated, e.g. ``b`` was
        ``None``; then ``text`` is bound to ``b`` and will not become bound
        to ``d`` even when ``b`` is changed to not be ``None``.

        By setting ``rebind`` to ``True``, however, the rule will be
        re-evaluated and all the properties rebound when that intermediate
        property changes. E.g. in the example above, whenever ``b`` changes
        or becomes not ``None`` if it was ``None`` before, ``text`` is
        evaluated again and becomes rebound to ``d``. The overall result is
        that ``text`` is now bound to all the properties among ``a``,
        ``b``, or ``c`` that have ``rebind`` set to ``True``.
    `\*\*kwargs`: a list of keyword arguments
        `baseclass`
            If kwargs includes a `baseclass` argument, this value will be
            used for validation: `isinstance(value, kwargs['baseclass'])`.

<div class="pdoc-alert pdoc-alert-warning" markdown="1">
To mark the property as changed, you must reassign a new python object.
</div>

*Changed in version 1.9.0:*
`rebind` has been introduced.


*Changed in version 1.7.0:*
`baseclass` parameter added.
def set_color(self, color: kvex.colors.XColor):
39    def set_color(self, color: XColor):
40        """Set the current color."""
41        self.r.slider.value = color.r
42        self.g.slider.value = color.g
43        self.b.slider.value = color.b
44        self.a.slider.value = color.a

Set the current color.

Inherited Members
kivy.uix.boxlayout.BoxLayout
spacing
padding
orientation
minimum_width
minimum_height
minimum_size
do_layout
add_widget
remove_widget
kivy.uix.layout.Layout
layout_hint_with_bounds
kivy.uix.widget.Widget
proxy_ref
apply_class_lang_rules
collide_point
collide_widget
on_motion
on_touch_down
on_touch_move
on_touch_up
on_kv_post
clear_widgets
register_for_motion_event
unregister_for_motion_event
export_to_png
export_as_image
get_root_window
get_parent_window
walk
walk_reverse
to_widget
to_window
to_parent
to_local
get_window_matrix
x
y
width
height
pos
size
get_right
set_right
right
get_top
set_top
top
get_center_x
set_center_x
center_x
get_center_y
set_center_y
center_y
center
cls
children
parent
size_hint_x
size_hint_y
size_hint
pos_hint
size_hint_min_x
size_hint_min_y
size_hint_min
size_hint_max_x
size_hint_max_y
size_hint_max
ids
opacity
on_opacity
canvas
get_disabled
set_disabled
inc_disabled
dec_disabled
disabled
motion_filter
kivy._event.EventDispatcher
register_event_type
unregister_event_types
unregister_event_type
is_event_type
bind
unbind
fbind
funbind
unbind_uid
get_property_observers
events
dispatch
dispatch_generic
dispatch_children
setter
getter
property
properties
create_property
apply_property
class XSelectColor(kvex.widgets.label.XLabelClick):
60class XSelectColor(XLabelClick):
61    """An XPickColor that drops down from an XLabelClick."""
62
63    def __init__(
64        self,
65        prefix: str = "[u]Color:[/u]\n",
66        show_color_values: bool = True,
67        **kwargs,
68    ):
69        """Initialize the class.
70
71        Args:
72            prefix: Text to show before the RGB values.
73            show_color_values: Show the RGB values of the current color.
74            kwargs: Keyword arguments for the XLabelClick.
75        """
76        self.prefix = prefix
77        self.show_color_values = show_color_values
78        super().__init__(**kwargs)
79        self.picker = XPickColor()
80        self.dropdown = XDropDown(auto_width=False, on_dismiss=self._on_color)
81        self.dropdown.set_size(*self.picker.size)
82        self.dropdown.add_widget(self.picker)
83        self.picker.bind(size=lambda w, s: self.dropdown.set_size(*s))
84        self.bind(on_release=self.dropdown.open)
85        self.on_color()
86
87    def _on_color(self, *args):
88        color = self.picker.color
89        self.make_bg(color)
90        text = self.prefix
91        if self.show_color_values:
92            text += " , ".join(str(round(c, 2)) for c in color.rgba)
93        self.text = text

An XPickColor that drops down from an XLabelClick.

XSelectColor( prefix: str = '[u]Color:[/u]\n', show_color_values: bool = True, **kwargs)
63    def __init__(
64        self,
65        prefix: str = "[u]Color:[/u]\n",
66        show_color_values: bool = True,
67        **kwargs,
68    ):
69        """Initialize the class.
70
71        Args:
72            prefix: Text to show before the RGB values.
73            show_color_values: Show the RGB values of the current color.
74            kwargs: Keyword arguments for the XLabelClick.
75        """
76        self.prefix = prefix
77        self.show_color_values = show_color_values
78        super().__init__(**kwargs)
79        self.picker = XPickColor()
80        self.dropdown = XDropDown(auto_width=False, on_dismiss=self._on_color)
81        self.dropdown.set_size(*self.picker.size)
82        self.dropdown.add_widget(self.picker)
83        self.picker.bind(size=lambda w, s: self.dropdown.set_size(*s))
84        self.bind(on_release=self.dropdown.open)
85        self.on_color()

Initialize the class.

Arguments:
  • prefix: Text to show before the RGB values.
  • show_color_values: Show the RGB values of the current color.
  • kwargs: Keyword arguments for the XLabelClick.
Inherited Members
kivy.uix.behaviors.button.ButtonBehavior
state
last_touch
min_state_time
always_release
cancel_event
on_touch_down
on_touch_move
on_touch_up
on_press
on_release
trigger_action
kivy.uix.label.Label
texture_update
on_ref_press
disabled_color
text
text_size
base_direction
text_language
font_context
font_family
font_name
font_size
font_features
line_height
bold
italic
underline
strikethrough
padding_x
padding_y
padding
halign
valign
color
outline_width
outline_color
disabled_outline_color
texture
texture_size
mipmap
shorten
shorten_from
is_shortened
split_str
ellipsis_options
unicode_errors
markup
refs
anchors
max_lines
strip
font_hinting
font_kerning
font_blended
kivy.uix.widget.Widget
proxy_ref
apply_class_lang_rules
collide_point
collide_widget
on_motion
on_kv_post
add_widget
remove_widget
clear_widgets
register_for_motion_event
unregister_for_motion_event
export_to_png
export_as_image
get_root_window
get_parent_window
walk
walk_reverse
to_widget
to_window
to_parent
to_local
get_window_matrix
x
y
width
height
pos
size
get_right
set_right
right
get_top
set_top
top
get_center_x
set_center_x
center_x
get_center_y
set_center_y
center_y
center
cls
children
parent
size_hint_x
size_hint_y
size_hint
pos_hint
size_hint_min_x
size_hint_min_y
size_hint_min
size_hint_max_x
size_hint_max_y
size_hint_max
ids
opacity
on_opacity
canvas
get_disabled
set_disabled
inc_disabled
dec_disabled
disabled
motion_filter
kivy._event.EventDispatcher
register_event_type
unregister_event_types
unregister_event_type
is_event_type
bind
unbind
fbind
funbind
unbind_uid
get_property_observers
events
dispatch
dispatch_generic
dispatch_children
setter
getter
property
properties
create_property
apply_property