Small bug on "CodeInput" and "get_color_from_hex" with solve sollution.
Software Versions
- Python: 3.10.4
- OS: Windows 10 Pro N 21H2
- Kivy: 2.1.0
- Kivy installation method: pip
Describe the bug On CodeInput class (kivy\uix\codeinput.py) the background_color value is not self update it via pygments styles. Your on_style_name definition from line 116 is not call it by default and is a bit annoying because I end it up by editing your original code.
Expected behavior
As expectation will be to replace if not kwargs.get('background_color'): logic from line 114 with the below one witch fix all of this, or to make a new smarter one but with same result please.
if not kwargs.get('background_color'):
try: self.background_color = get_color_from_hex(style.background_color)
except Exception: self.background_color = [.9, .92, .92, 1]
else: self.background_color = kwargs.get("background_color")
Additional bug/future The get_color_from_hex definition from line 113 (kivy\utils.py) also have a small room for improve. This one accept just 6-8 characters as value. Pygments style allow also 3 and 6 like #FF0000 == #F00. Again I end it up by editing your original code by doubling each character if len(color) is less then 5 to allow and alpha value just for the sick of the consisting API as in get_hex_from_color. Bellow is the entire get_color_from_hex logic.
def get_color_from_hex(s):
'''Transform a hex string color to a kivy
:class:`~kivy.graphics.Color`.
'''
if s.startswith('#'):
return get_color_from_hex(s[1:])
if len(s) < 5:
tmp = ""
for i in range(len(s)):
tmp += s[i] * 2
s = tmp
value = [int(x, 16) / 255.
for x in split('([0-9a-f]{2})', s.lower()) if x != '']
if len(value) == 3:
value.append(1.0)
return value
# First attempt was to work on that regex (like bellow code) but #F00 got me an wrong color like a very dark red/brown
# So just the above code works for me. May by you can get it smarter then me, but with same result?
value = [int(x, 16) / 255.
for x in split('([0-9a-f]%s)' % "{2}" if len(s) > 4 else "", s.lower()) if x != '']