forked from cool-RR/python_toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolors.py
More file actions
140 lines (102 loc) · 3.84 KB
/
colors.py
File metadata and controls
140 lines (102 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Copyright 2009-2017 Ram Rachum.
# This program is distributed under the MIT license.
'''
Defines color-related tools.
This includes functions for getting general colors (e.g. background color) and
functions to convert between different respresentations of colors.
'''
from __future__ import division
import colorsys
import warnings
import wx
from python_toolbox import caching
from python_toolbox import color_tools
is_mac = (wx.Platform == '__WXMAC__')
is_gtk = (wx.Platform == '__WXGTK__')
is_win = (wx.Platform == '__WXMSW__')
@caching.cache()
def get_foreground_color():
'''Get the default foreground color.'''
return wx.SystemSettings.GetColour(wx.SYS_COLOUR_MENUTEXT)
@caching.cache()
def get_background_color():
'''Get the default background color'''
if is_win:
# return wx.Colour(212, 208, 200)
return wx.SystemSettings.GetColour(wx.SYS_COLOUR_MENUBAR)
elif is_mac:
return wx.Colour(232, 232, 232)
elif is_gtk:
# Until `SYS_COLOUR_*` get their act togother, we're using Windows
# colors for Linux.
return wx.Colour(212, 208, 200)
else:
warnings.warn("Unidentified platform! It's neither '__WXGTK__', "
"'__WXMAC__' nor '__WXMSW__'. Things might not work "
"properly.")
return wx.Colour(212, 208, 200)
@caching.cache()
def get_background_brush():
'''Get the default background brush.'''
return wx.Brush(get_background_color())
### Color conversions: ########################################################
# #
def wx_color_to_html_color(wx_color):
'''Convert a wxPython color to an HTML color string.'''
rgb = wx_color.GetRGB()
(green_blue, red) = divmod(rgb, 256)
(blue, green) = divmod(green_blue, 256)
return '#%02x%02x%02x' % (red, green, blue)
def hls_to_wx_color(hls, alpha=255):
'''Convert an HLS color to a wxPython color.'''
return rgb_to_wx_color(colorsys.hls_to_rgb(*hls), alpha=alpha)
def wx_color_to_hls(wx_color):
'''Convert a wxPython color to an HLS color.'''
return colorsys.rgb_to_hls(wx_color.red, wx_color.blue, wx_color.green)
def rgb_to_wx_color(rgb, alpha=255):
'''Convert an RGB color to a wxPython color.'''
r, g, b = rgb
return wx.Colour(r * 255, g * 255, b * 255, alpha)
def wx_color_to_rgb(wx_color):
'''Convert a wxPython color to an RGB color.'''
return (
wx_color.red / 255,
wx_color.green / 255,
wx_color.blue / 255
)
def wx_color_to_big_rgb(wx_color):
'''Convert a wxPython color to a big (i.e. `int`) RGB color.'''
return (
wx_color.red,
wx_color.green,
wx_color.blue
)
# #
### Finished color conversions. ###############################################
### Color inversion: ##########################################################
# #
def invert_rgb(rgb):
red, green, blue = rgb
return (
1 - red,
1 - green,
1 - blue
)
def invert_hls(hls):
rgb = colorsys.hls_to_rgb(hls)
inverted_rgb = inverted_rgb(rgb)
return colorsys.rgb_to_hls(inverted_rgb)
def invert_wx_color(wx_color):
rgb = wx_color_to_rgb(wx_color)
inverted_rgb = invert_rgb(rgb)
return rgb_to_wx_color(inverted_rgb)
# #
### Finished color inversion. #################################################
def mix_wx_color(ratio, color1, color2):
'''Mix two wxPython colors according to the given `ratio`.'''
rgb = color_tools.mix_rgb(
ratio,
wx_color_to_rgb(color1),
wx_color_to_rgb(color2)
)
return rgb_to_wx_color(rgb)