styles.py
122 lines
| 4.1 KiB
| text/x-python
|
PythonLexer
MinRK
|
r3170 | """ Style utilities, templates, and defaults for syntax highlighting widgets. | ||
""" | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
from colorsys import rgb_to_hls | ||||
from pygments.styles import get_style_by_name | ||||
from pygments.token import Token | ||||
#----------------------------------------------------------------------------- | ||||
# Constants | ||||
#----------------------------------------------------------------------------- | ||||
# The default light style sheet: black text on a white background. | ||||
default_light_style_template = ''' | ||||
QPlainTextEdit, QTextEdit { background-color: %(bgcolor)s; | ||||
color: %(fgcolor)s ; | ||||
selection-background-color: %(select)s} | ||||
.error { color: red; } | ||||
.in-prompt { color: navy; } | ||||
.in-prompt-number { font-weight: bold; } | ||||
.out-prompt { color: darkred; } | ||||
.out-prompt-number { font-weight: bold; } | ||||
Matthias BUSSONNIER
|
r7389 | .inverted { background-color: %(fgcolor)s ; color:%(bgcolor)s;} | ||
MinRK
|
r3170 | ''' | ||
default_light_style_sheet = default_light_style_template%dict( | ||||
bgcolor='white', fgcolor='black', select="#ccc") | ||||
default_light_syntax_style = 'default' | ||||
# The default dark style sheet: white text on a black background. | ||||
default_dark_style_template = ''' | ||||
QPlainTextEdit, QTextEdit { background-color: %(bgcolor)s; | ||||
color: %(fgcolor)s ; | ||||
selection-background-color: %(select)s} | ||||
QFrame { border: 1px solid grey; } | ||||
.error { color: red; } | ||||
.in-prompt { color: lime; } | ||||
.in-prompt-number { color: lime; font-weight: bold; } | ||||
.out-prompt { color: red; } | ||||
.out-prompt-number { color: red; font-weight: bold; } | ||||
Matthias BUSSONNIER
|
r7389 | .inverted { background-color: %(fgcolor)s ; color:%(bgcolor)s;} | ||
MinRK
|
r3170 | ''' | ||
default_dark_style_sheet = default_dark_style_template%dict( | ||||
bgcolor='black', fgcolor='white', select="#555") | ||||
default_dark_syntax_style = 'monokai' | ||||
MinRK
|
r3171 | # The default monochrome | ||
default_bw_style_sheet = ''' | ||||
QPlainTextEdit, QTextEdit { background-color: white; | ||||
color: black ; | ||||
selection-background-color: #cccccc} | ||||
.in-prompt-number { font-weight: bold; } | ||||
.out-prompt-number { font-weight: bold; } | ||||
Matthias BUSSONNIER
|
r7389 | .inverted { background-color: black ; color: white;} | ||
MinRK
|
r3171 | ''' | ||
default_bw_syntax_style = 'bw' | ||||
MinRK
|
r3170 | def hex_to_rgb(color): | ||
"""Convert a hex color to rgb integer tuple.""" | ||||
if color.startswith('#'): | ||||
color = color[1:] | ||||
if len(color) == 3: | ||||
color = ''.join([c*2 for c in color]) | ||||
if len(color) != 6: | ||||
return False | ||||
try: | ||||
r = int(color[:2],16) | ||||
MinRK
|
r6053 | g = int(color[2:4],16) | ||
b = int(color[4:],16) | ||||
MinRK
|
r3170 | except ValueError: | ||
return False | ||||
else: | ||||
return r,g,b | ||||
def dark_color(color): | ||||
"""Check whether a color is 'dark'. | ||||
Currently, this is simply whether the luminance is <50%""" | ||||
rgb = hex_to_rgb(color) | ||||
if rgb: | ||||
return rgb_to_hls(*rgb)[1] < 128 | ||||
else: # default to False | ||||
return False | ||||
def dark_style(stylename): | ||||
"""Guess whether the background of the style with name 'stylename' | ||||
counts as 'dark'.""" | ||||
return dark_color(get_style_by_name(stylename).background_color) | ||||
def get_colors(stylename): | ||||
MinRK
|
r3171 | """Construct the keys to be used building the base stylesheet | ||
from a templatee.""" | ||||
MinRK
|
r3170 | style = get_style_by_name(stylename) | ||
fgcolor = style.style_for_token(Token.Text)['color'] or '' | ||||
if len(fgcolor) in (3,6): | ||||
# could be 'abcdef' or 'ace' hex, which needs '#' prefix | ||||
try: | ||||
int(fgcolor, 16) | ||||
except TypeError: | ||||
pass | ||||
else: | ||||
fgcolor = "#"+fgcolor | ||||
return dict( | ||||
bgcolor = style.background_color, | ||||
select = style.highlight_color, | ||||
fgcolor = fgcolor | ||||
) | ||||
MinRK
|
r3173 | def sheet_from_template(name, colors='lightbg'): | ||
MinRK
|
r3170 | """Use one of the base templates, and set bg/fg/select colors.""" | ||
MinRK
|
r3171 | colors = colors.lower() | ||
MinRK
|
r3173 | if colors=='lightbg': | ||
MinRK
|
r3170 | return default_light_style_template%get_colors(name) | ||
MinRK
|
r3173 | elif colors=='linux': | ||
MinRK
|
r3171 | return default_dark_style_template%get_colors(name) | ||
elif colors=='nocolor': | ||||
return default_bw_style_sheet | ||||
MinRK
|
r3170 | else: | ||
MinRK
|
r3171 | raise KeyError("No such color scheme: %s"%colors) | ||