Skip to content

Commit 1f73be4

Browse files
committed
-
1 parent 016eef6 commit 1f73be4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+15615
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2009-2015 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
'''Defines various tools for wxPython.'''
5+
6+
import wx
7+
8+
9+
is_mac = (wx.Platform == '__WXMAC__')
10+
is_gtk = (wx.Platform == '__WXGTK__')
11+
is_win = (wx.Platform == '__WXMSW__')
12+
13+
14+
from . import colors
15+
from . import keyboard
16+
from . import window_tools
17+
from . import bitmap_tools
18+
from . import cursors
19+
from . import event_tools
20+
from . import generic_bitmaps
21+
from . import drawing_tools
22+
from . import timing
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2009-2015 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
'''Defines bitmap-related tools.'''
5+
6+
import pkg_resources
7+
import wx
8+
9+
10+
def color_replaced_bitmap(bitmap, old_rgb, new_rgb):
11+
'''Replace all appearances of `old_rgb` with `new_rgb` in `bitmap`.'''
12+
old_r, old_g, old_b = old_rgb
13+
new_r, new_g, new_b = new_rgb
14+
image = wx.ImageFromBitmap(bitmap)
15+
assert isinstance(image, wx.Image)
16+
image.Replace(old_r, old_g, old_b, new_r, new_g, new_b)
17+
return wx.BitmapFromImage(image)
18+
19+
20+
def bitmap_from_pkg_resources(package_or_requirement, resource_name):
21+
'''
22+
Get a bitmap from a file using `pkg_resources`.
23+
24+
Example:
25+
26+
my_bitmap = bitmap_from_pkg_resources('whatever.images', 'image.jpg')
27+
28+
'''
29+
return wx.BitmapFromImage(
30+
wx.ImageFromStream(
31+
pkg_resources.resource_stream(package_or_requirement,
32+
resource_name),
33+
wx.BITMAP_TYPE_ANY
34+
)
35+
)
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# Copyright 2009-2015 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
'''
5+
Defines color-related tools.
6+
7+
This includes functions for getting general colors (e.g. background color) and
8+
functions to convert between different respresentations of colors.
9+
'''
10+
11+
12+
from __future__ import division
13+
14+
import colorsys
15+
import warnings
16+
17+
import wx
18+
19+
from python_toolbox import caching
20+
from python_toolbox import color_tools
21+
22+
23+
is_mac = (wx.Platform == '__WXMAC__')
24+
is_gtk = (wx.Platform == '__WXGTK__')
25+
is_win = (wx.Platform == '__WXMSW__')
26+
27+
28+
@caching.cache()
29+
def get_foreground_color():
30+
'''Get the default foreground color.'''
31+
return wx.SystemSettings.GetColour(wx.SYS_COLOUR_MENUTEXT)
32+
33+
34+
@caching.cache()
35+
def get_background_color():
36+
'''Get the default background color'''
37+
38+
if is_win:
39+
# return wx.Colour(212, 208, 200)
40+
return wx.SystemSettings.GetColour(wx.SYS_COLOUR_MENUBAR)
41+
elif is_mac:
42+
return wx.Colour(232, 232, 232)
43+
elif is_gtk:
44+
# Until `SYS_COLOUR_*` get their act togother, we're using Windows
45+
# colors for Linux.
46+
return wx.Colour(212, 208, 200)
47+
48+
else:
49+
warnings.warn("Unidentified platform! It's neither '__WXGTK__', "
50+
"'__WXMAC__' nor '__WXMSW__'. Things might not work "
51+
"properly.")
52+
return wx.Colour(212, 208, 200)
53+
54+
55+
@caching.cache()
56+
def get_background_brush():
57+
'''Get the default background brush.'''
58+
return wx.Brush(get_background_color())
59+
60+
61+
62+
63+
### Color conversions: ########################################################
64+
# #
65+
def wx_color_to_html_color(wx_color):
66+
'''Convert a wxPython color to an HTML color string.'''
67+
rgb = wx_color.GetRGB()
68+
(green_blue, red) = divmod(rgb, 256)
69+
(blue, green) = divmod(green_blue, 256)
70+
return '#%02x%02x%02x' % (red, green, blue)
71+
72+
73+
def hls_to_wx_color(hls, alpha=255):
74+
'''Convert an HLS color to a wxPython color.'''
75+
return rgb_to_wx_color(colorsys.hls_to_rgb(*hls), alpha=alpha)
76+
77+
78+
def wx_color_to_hls(wx_color):
79+
'''Convert a wxPython color to an HLS color.'''
80+
return colorsys.rgb_to_hls(wx_color.red, wx_color.blue, wx_color.green)
81+
82+
83+
def rgb_to_wx_color(rgb, alpha=255):
84+
'''Convert an RGB color to a wxPython color.'''
85+
r, g, b = rgb
86+
return wx.Colour(r * 255, g * 255, b * 255, alpha)
87+
88+
89+
def wx_color_to_rgb(wx_color):
90+
'''Convert a wxPython color to an RGB color.'''
91+
return (
92+
wx_color.red / 255,
93+
wx_color.green / 255,
94+
wx_color.blue / 255
95+
)
96+
97+
98+
def wx_color_to_big_rgb(wx_color):
99+
'''Convert a wxPython color to a big (i.e. `int`) RGB color.'''
100+
return (
101+
wx_color.red,
102+
wx_color.green,
103+
wx_color.blue
104+
)
105+
# #
106+
### Finished color conversions. ###############################################
107+
108+
### Color inversion: ##########################################################
109+
# #
110+
def invert_rgb(rgb):
111+
red, green, blue = rgb
112+
return (
113+
1 - red,
114+
1 - green,
115+
1 - blue
116+
)
117+
118+
119+
def invert_hls(hls):
120+
rgb = colorsys.hls_to_rgb(hls)
121+
inverted_rgb = inverted_rgb(rgb)
122+
return colorsys.rgb_to_hls(inverted_rgb)
123+
124+
125+
def invert_wx_color(wx_color):
126+
rgb = wx_color_to_rgb(wx_color)
127+
inverted_rgb = invert_rgb(rgb)
128+
return rgb_to_wx_color(inverted_rgb)
129+
# #
130+
### Finished color inversion. #################################################
131+
132+
133+
def mix_wx_color(ratio, color1, color2):
134+
'''Mix two wxPython colors according to the given `ratio`.'''
135+
rgb = color_tools.mix_rgb(
136+
ratio,
137+
wx_color_to_rgb(color1),
138+
wx_color_to_rgb(color2)
139+
)
140+
return rgb_to_wx_color(rgb)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright 2009-2015 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
'''Defines various cursor-related tools.'''
5+
6+
from . import collection
7+
from .cursor_changer import CursorChanger
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright 2009-2015 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
'''A collection of cursors.'''
5+
6+
from .collection import get_open_grab, get_closed_grab
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2009-2015 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
'''A collection of cursors.'''
5+
6+
import pkg_resources
7+
import wx
8+
9+
from python_toolbox import caching
10+
11+
12+
from . import images as __images_package
13+
images_package = __images_package.__name__
14+
15+
16+
@caching.cache()
17+
def get_open_grab():
18+
'''Get the "open grab" cursor.'''
19+
file_name = 'open_grab.png'
20+
hotspot = (8, 8)
21+
stream = pkg_resources.resource_stream(images_package,
22+
file_name)
23+
image = wx.ImageFromStream(stream, wx.BITMAP_TYPE_ANY)
24+
25+
if hotspot is not None:
26+
image.SetOptionInt(wx.IMAGE_OPTION_CUR_HOTSPOT_X, hotspot[0])
27+
image.SetOptionInt(wx.IMAGE_OPTION_CUR_HOTSPOT_Y, hotspot[1])
28+
29+
cursor = wx.CursorFromImage(image)
30+
return cursor
31+
32+
33+
@caching.cache()
34+
def get_closed_grab():
35+
'''Get the "closed grab" cursor.'''
36+
file_name = 'closed_grab.png'
37+
hotspot = (8, 8)
38+
stream = pkg_resources.resource_stream(images_package,
39+
file_name)
40+
image = wx.ImageFromStream(stream, wx.BITMAP_TYPE_ANY)
41+
42+
if hotspot is not None:
43+
image.SetOptionInt(wx.IMAGE_OPTION_CUR_HOTSPOT_X, hotspot[0])
44+
image.SetOptionInt(wx.IMAGE_OPTION_CUR_HOTSPOT_Y, hotspot[1])
45+
46+
cursor = wx.CursorFromImage(image)
47+
return cursor
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright 2009-2015 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
'''Images package.'''
2.82 KB
Loading
2.85 KB
Loading
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2009-2015 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
import wx
5+
6+
from python_toolbox.temp_value_setting import TempValueSetter
7+
8+
9+
class CursorChanger(TempValueSetter):
10+
'''Context manager for showing specified cursor while suite executes.'''
11+
def __init__(self, window, cursor):
12+
'''
13+
Construct the `CursorChanger`.
14+
15+
`cursor` may be either a `wx.Cursor` object or a constant like
16+
`wx.CURSOR_BULLSEYE`.
17+
'''
18+
assert isinstance(window, wx.Window)
19+
self.window = window
20+
self.cursor = cursor if isinstance(cursor, wx.Cursor) \
21+
else wx.StockCursor(cursor)
22+
TempValueSetter.__init__(self,
23+
(window.GetCursor, window.SetCursor),
24+
self.cursor,
25+
assert_no_fiddling=False)

0 commit comments

Comments
 (0)