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