diff --git a/kivy/uix/codeinput.py b/kivy/uix/codeinput.py index 67f227ce4..53ef2c977 100644 --- a/kivy/uix/codeinput.py +++ b/kivy/uix/codeinput.py @@ -66,22 +66,34 @@ class CodeInput(TextInput): def __init__(self, **kwargs): self.formatter = BBCodeFormatter() self.lexer = lexers.PythonLexer() - self.text_color = (0, 0, 0, 1) + self.text_color = '#000000' self._label_cached = Label() + self.use_text_color = True super(CodeInput, self).__init__(**kwargs) self._line_options = kw = self._get_line_options() self._label_cached = Label(**kw) - #use text_color as foreground color + # use text_color as foreground color text_color = kwargs.get('foreground_color') if text_color: - self.text_color = (text_color[0], text_color[1], text_color[2], - text_color[3]) + get_hex_clr = self.get_hex_clr + self.text_color = ''.join(('#', + get_hex_clr(text_color[0]), + get_hex_clr(text_color[1]), + get_hex_clr(text_color[2]), + get_hex_clr(text_color[3]))) # set foreground to white to allow text colors to show # use text_color as the default color in bbcodes - self.foreground_color = [1, 1, 1, 1] + self.use_text_color = False + self.foreground_color = [1, 1, 1, .999] if not kwargs.get('background_color'): self.background_color = [.9, .92, .92, 1] + def get_hex_clr(self, color): + clr = hex(int(color * 255))[2:] + if len(str(clr)) < 2: + clr = ''.join(('0', str(clr))) + return clr + def _create_line_label(self, text): # Create a label from a text, using line options ntext = text.replace('\n', '').replace('\t', ' ' * self.tab_width) @@ -104,7 +116,7 @@ class CodeInput(TextInput): try: label.refresh() except ValueError: - pass + return # ok, we found it. texture = label.texture @@ -116,6 +128,7 @@ class CodeInput(TextInput): kw = super(CodeInput, self)._get_line_options() kw['markup'] = True kw['valign'] = 'top' + kw['codeinput'] = True return kw def _get_bbcode(self, ntext): @@ -130,7 +143,7 @@ class CodeInput(TextInput): ntext = highlight(ntext, self.lexer, self.formatter) ntext = ntext.replace(u'⣿;', '&bl;').replace(u'⣾;', '&br;') # replace special chars with &bl; and &br; - ntext = ''.join(('[color=rgba', str(self.text_color), ']', + ntext = ''.join(('[color=', str(self.text_color), ']', ntext, '[/color]')) ntext = ntext.replace('\n', '') return ntext @@ -154,6 +167,21 @@ class CodeInput(TextInput): def on_lexer(self, instance, value): self._trigger_refresh_text() + def on_foreground_color(self, instance, text_color): + if not self.use_text_color: + self.use_text_color = True + return + get_hex_clr = self.get_hex_clr + self.text_color = ''.join(( + get_hex_clr(text_color[0]), + get_hex_clr(text_color[1]), + get_hex_clr(text_color[2]), + get_hex_clr(text_color[3]))) + self.use_text_color = False + self.foreground_color = (1, 1, 1, .999) + self._trigger_refresh_text() + + if __name__ == '__main__': from kivy.extras.highlight import KivyLexer from kivy.app import App