forked from Pymol-Scripts/Pymol-script-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorblindfriendly.py
219 lines (176 loc) · 6.52 KB
/
colorblindfriendly.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
'''
More information and examples can be found at:
http://www.pymolwiki.org/index.php/colorblindfriendly
DESCRIPTION
Certain colors are indistinguishable to people with the various forms of
color blindness, and therefore are better not used in figures intended for
public viewing.
This script generates a palette of named colors for PyMOL that are
unambiguous both to colorblind and non-colorblind people.
The colors listed here are defined according to recommendations found at
http://jfly.iam.u-tokyo.ac.jp/html/color_blind/. This website is a good
reference to consult when making all kinds of figures, not just those made
using PyMOL.
The colors are:
* cb_black
* cb_orange
* cb_sky_blue (also: cb_skyblue, cb_light_blue, cb_lightblue)
* cb_bluish_green (also: cb_bluishgreen, cb_green)
* cb_yellow
* cb_blue
* cb_vermillion (also: cb_red, cb_redorange, cb_red_orange)
* cb_reddish_purple (also: cb_rose, cb_violet, cb_magenta)
USAGE
import colorblindfriendly as cbf
# Add the new colors
cbf.set_colors()
color myObject, cb_red
# Replace built-in colors with cbf ones
cbf.set_colors(replace=True)
color myOtherObject, yellow # actually cb_yellow
# Add a `cb_colors` menu item to the OpenGL GUI ([C] menu in the right panel)
cbf.add_menu()
REQUIREMENTS
The cb_colors menu (`add_menu()` function) requires PyMOL 1.6.0 or later.
AUTHOR
Jared Sampson
Github: @jaredsampson
LICENSE
Copyright (c) 2014-2017 Jared Sampson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
CHANGELOG
0.2.0 Complete overhaul for PyMOL 2.0 with conversion to module format.
Now, setting the new `cb_*` color values requires a call to the
`set_colors()` function after import. You can also now add a
`cb_colors` menu to the OpenGL GUI via the `add_menu()` function.
[10/26/2017]
'''
from __future__ import print_function
__author__ = 'Jared Sampson'
__version__ = '0.2.0'
import pymol
from pymol import cmd
# Color blind-friendly color list based on information found at:
# http://jfly.iam.u-tokyo.ac.jp/html/color_blind/#pallet
CB_COLORS = {
'black': {
'rgb': [0, 0, 0],
'alt': None,
},
'orange': {
'rgb': [230, 159, 0],
'alt': None,
},
'sky_blue': {
'rgb': [86, 180, 233],
'alt': ['skyblue', 'light_blue', 'lightblue'],
},
'bluish_green': {
'rgb': [0, 158, 115],
'alt': ['bluishgreen', 'green'],
},
'yellow': {
'rgb': [240, 228, 66],
'alt': None,
},
'blue': {
'rgb': [0, 114, 178],
'alt': None,
},
'vermillion': {
'rgb': [213, 94, 0],
'alt': ['red', 'red_orange', 'redorange'],
},
'reddish_purple': {
'rgb': [204, 121, 167],
'alt': ['reddishpurple', 'rose', 'violet', 'magenta'],
},
}
def set_colors(replace=False):
'''Add the color blind-friendly colors to PyMOL.'''
# Track the added colors
added_colors = []
for color, properties in CB_COLORS.items():
# RGB tuple shortcut
rgb = properties['rgb']
# Get the primary and alternate color names into a single list
names = [color]
if properties['alt']:
names.extend(properties['alt'])
# Set the colors
for name in names:
# Set the cb_color
cb_name = 'cb_{}'.format(name)
cmd.set_color(cb_name, rgb)
# Optionally replace built-in colors
if replace:
cmd.set_color(name, rgb)
spacer = (20 - len(name)) * ' '
added_colors.append(' {}{}{}'.format(name, spacer, cb_name))
else:
added_colors.append(' {}'.format(cb_name))
# Notify user of newly available colors
print('\nColor blind-friendly colors are now available:')
print('\n'.join(added_colors))
print('')
def add_menu():
'''Add a color blind-friendly list of colors to the PyMOL OpenGL menu.'''
# Make sure cb_colors are installed.
print('Checking for colorblindfriendly colors...')
try:
if cmd.get_color_index('cb_red') == -1:
# mimic pre-1.7.4 behavior
raise pymol.CmdException
except pymol.CmdException:
print('Adding colorblindfriendly colors...')
set_colors()
# Abort if PyMOL is too old.
try:
from pymol.menu import all_colors_list
except ImportError:
print('PyMOL version too old for cb_colors menu. Requires 1.6.0 or later.')
return
# Add the menu
print('Adding cb_colors menu...')
# mimic pymol.menu.all_colors_list format
# first color in list is used for menu item color
cb_colors = ('cb_colors', [
('830', 'cb_red'),
('064', 'cb_green'),
('046', 'cb_blue'),
('882', 'cb_yellow'),
('746', 'cb_magenta'),
('368', 'cb_skyblue'),
('860', 'cb_orange'),
])
# First `pymol` is the program instance, second is the Python module
all_colors_list = pymol.pymol.menu.all_colors_list
if cb_colors in all_colors_list:
print('Menu was already added!')
else:
all_colors_list.append(cb_colors)
print(' done.')
def remove_menu():
'''Remove the cb_colors menu.'''
all_colors_list = pymol.pymol.menu.all_colors_list
if all_colors_list[-1][0] == 'cb_colors':
all_colors_list.pop()
print('The `cb_colors` menu has been removed.')
else:
print('The `cb_colors` menu was not found! Aborting.')
if __name__ == "pymol":
add_menu()