Show More
@@ -1,119 +1,119 | |||
|
1 | 1 | """ Style utilities, templates, and defaults for syntax highlighting widgets. |
|
2 | 2 | """ |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Imports |
|
5 | 5 | #----------------------------------------------------------------------------- |
|
6 | 6 | |
|
7 | 7 | from colorsys import rgb_to_hls |
|
8 | 8 | from pygments.styles import get_style_by_name |
|
9 | 9 | from pygments.token import Token |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Constants |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | # The default light style sheet: black text on a white background. |
|
16 | 16 | default_light_style_template = ''' |
|
17 | 17 | QPlainTextEdit, QTextEdit { background-color: %(bgcolor)s; |
|
18 | 18 | color: %(fgcolor)s ; |
|
19 | 19 | selection-background-color: %(select)s} |
|
20 | 20 | .error { color: red; } |
|
21 | 21 | .in-prompt { color: navy; } |
|
22 | 22 | .in-prompt-number { font-weight: bold; } |
|
23 | 23 | .out-prompt { color: darkred; } |
|
24 | 24 | .out-prompt-number { font-weight: bold; } |
|
25 | 25 | ''' |
|
26 | 26 | default_light_style_sheet = default_light_style_template%dict( |
|
27 | 27 | bgcolor='white', fgcolor='black', select="#ccc") |
|
28 | 28 | default_light_syntax_style = 'default' |
|
29 | 29 | |
|
30 | 30 | # The default dark style sheet: white text on a black background. |
|
31 | 31 | default_dark_style_template = ''' |
|
32 | 32 | QPlainTextEdit, QTextEdit { background-color: %(bgcolor)s; |
|
33 | 33 | color: %(fgcolor)s ; |
|
34 | 34 | selection-background-color: %(select)s} |
|
35 | 35 | QFrame { border: 1px solid grey; } |
|
36 | 36 | .error { color: red; } |
|
37 | 37 | .in-prompt { color: lime; } |
|
38 | 38 | .in-prompt-number { color: lime; font-weight: bold; } |
|
39 | 39 | .out-prompt { color: red; } |
|
40 | 40 | .out-prompt-number { color: red; font-weight: bold; } |
|
41 | 41 | ''' |
|
42 | 42 | default_dark_style_sheet = default_dark_style_template%dict( |
|
43 | 43 | bgcolor='black', fgcolor='white', select="#555") |
|
44 | 44 | default_dark_syntax_style = 'monokai' |
|
45 | 45 | |
|
46 | 46 | # The default monochrome |
|
47 | 47 | default_bw_style_sheet = ''' |
|
48 | 48 | QPlainTextEdit, QTextEdit { background-color: white; |
|
49 | 49 | color: black ; |
|
50 | 50 | selection-background-color: #cccccc} |
|
51 | 51 | .in-prompt-number { font-weight: bold; } |
|
52 | 52 | .out-prompt-number { font-weight: bold; } |
|
53 | 53 | ''' |
|
54 | 54 | default_bw_syntax_style = 'bw' |
|
55 | 55 | |
|
56 | 56 | |
|
57 | 57 | def hex_to_rgb(color): |
|
58 | 58 | """Convert a hex color to rgb integer tuple.""" |
|
59 | 59 | if color.startswith('#'): |
|
60 | 60 | color = color[1:] |
|
61 | 61 | if len(color) == 3: |
|
62 | 62 | color = ''.join([c*2 for c in color]) |
|
63 | 63 | if len(color) != 6: |
|
64 | 64 | return False |
|
65 | 65 | try: |
|
66 | 66 | r = int(color[:2],16) |
|
67 |
g = int(color[: |
|
|
68 |
b = int(color[: |
|
|
67 | g = int(color[2:4],16) | |
|
68 | b = int(color[4:],16) | |
|
69 | 69 | except ValueError: |
|
70 | 70 | return False |
|
71 | 71 | else: |
|
72 | 72 | return r,g,b |
|
73 | 73 | |
|
74 | 74 | def dark_color(color): |
|
75 | 75 | """Check whether a color is 'dark'. |
|
76 | 76 | |
|
77 | 77 | Currently, this is simply whether the luminance is <50%""" |
|
78 | 78 | rgb = hex_to_rgb(color) |
|
79 | 79 | if rgb: |
|
80 | 80 | return rgb_to_hls(*rgb)[1] < 128 |
|
81 | 81 | else: # default to False |
|
82 | 82 | return False |
|
83 | 83 | |
|
84 | 84 | def dark_style(stylename): |
|
85 | 85 | """Guess whether the background of the style with name 'stylename' |
|
86 | 86 | counts as 'dark'.""" |
|
87 | 87 | return dark_color(get_style_by_name(stylename).background_color) |
|
88 | 88 | |
|
89 | 89 | def get_colors(stylename): |
|
90 | 90 | """Construct the keys to be used building the base stylesheet |
|
91 | 91 | from a templatee.""" |
|
92 | 92 | style = get_style_by_name(stylename) |
|
93 | 93 | fgcolor = style.style_for_token(Token.Text)['color'] or '' |
|
94 | 94 | if len(fgcolor) in (3,6): |
|
95 | 95 | # could be 'abcdef' or 'ace' hex, which needs '#' prefix |
|
96 | 96 | try: |
|
97 | 97 | int(fgcolor, 16) |
|
98 | 98 | except TypeError: |
|
99 | 99 | pass |
|
100 | 100 | else: |
|
101 | 101 | fgcolor = "#"+fgcolor |
|
102 | 102 | |
|
103 | 103 | return dict( |
|
104 | 104 | bgcolor = style.background_color, |
|
105 | 105 | select = style.highlight_color, |
|
106 | 106 | fgcolor = fgcolor |
|
107 | 107 | ) |
|
108 | 108 | |
|
109 | 109 | def sheet_from_template(name, colors='lightbg'): |
|
110 | 110 | """Use one of the base templates, and set bg/fg/select colors.""" |
|
111 | 111 | colors = colors.lower() |
|
112 | 112 | if colors=='lightbg': |
|
113 | 113 | return default_light_style_template%get_colors(name) |
|
114 | 114 | elif colors=='linux': |
|
115 | 115 | return default_dark_style_template%get_colors(name) |
|
116 | 116 | elif colors=='nocolor': |
|
117 | 117 | return default_bw_style_sheet |
|
118 | 118 | else: |
|
119 | 119 | raise KeyError("No such color scheme: %s"%colors) |
General Comments 0
You need to be logged in to leave comments.
Login now