def set_atom_style(self, atoms=None, fill_color=None, outline_color=None): if atoms is None: indices = range(len(self.atoms)) else: indices = map(self.get_atom_index, atoms) spec = {} if fill_color is not None: spec['fill'] = translate_color(fill_color, prefix='#') if outline_color is not None: spec['stroke'] = translate_color(outline_color, prefix='#') self.viewer('setAtomStyle', [indices, spec])
def draw_orbital(self, orbname, npts=50, isoval=0.01, opacity=0.8, negative_color='red', positive_color='blue', remove_old_orbitals=True, render=True): """Display a molecular orbital Args: orbname: name of the orbital (interface dependent) npts (int): resolution in each dimension isoval (float): isosurface value to draw opacity (float): opacity of the orbital (between 0 and 1) positive_color (str or int): color of the positive isosurfaces negative_color (str or int): color of the negative isosurfaces remove_old_orbitals (bool): remove any previously drawn orbitals render (bool): update the 3D scene before returning """ self.orbital_spec = dict(npts=npts, isoval=isoval, opacity=opacity, negative_color=negative_color, positive_color=positive_color) self.current_orbital = orbname if remove_old_orbitals: self.remove_orbitals(render=False) positive_color = translate_color(positive_color) negative_color = translate_color(negative_color) orbidx = self.get_orbidx(orbname) voldata = self.get_voldata(orbidx, npts, self.current_frame) positive_orbital = JSObject('shape') self.viewer('drawIsosurface', [ voldata.id, positive_orbital.id, { 'isoval': isoval, 'color': positive_color, 'opacity': opacity } ]) negative_orbital = JSObject('shape') self.viewer('drawIsosurface', [ voldata.id, negative_orbital.id, { 'isoval': -isoval, 'color': negative_color, 'opacity': opacity } ]) self.current_js_orbitals.extend([positive_orbital, negative_orbital]) if render: self.render()
def draw_orbital(self, orbname, npts=50, isoval=0.01, opacity=0.8, negative_color='red', positive_color='blue', remove_old_orbitals=True, render=True): """Display a molecular orbital Args: orbname: name of the orbital (interface dependent) npts (int): resolution in each dimension isoval (float): isosurface value to draw opacity (float): opacity of the orbital (between 0 and 1) positive_color (str or int): color of the positive isosurfaces negative_color (str or int): color of the negative isosurfaces remove_old_orbitals (bool): remove any previously drawn orbitals render (bool): update the 3D scene before returning """ self.orbital_spec = dict(npts=npts, isoval=isoval, opacity=opacity, negative_color=negative_color, positive_color=positive_color) self.current_orbital = orbname if remove_old_orbitals: self.remove_orbitals(render=False) positive_color = translate_color(positive_color) negative_color = translate_color(negative_color) orbidx = self.get_orbidx(orbname) voldata = self.get_voldata(orbidx, npts, self.current_frame) positive_orbital = JSObject('shape') self.viewer('drawIsosurface', [voldata.id, positive_orbital.id, {'isoval': isoval, 'color': positive_color, 'opacity': opacity}]) negative_orbital = JSObject('shape') self.viewer('drawIsosurface', [voldata.id, negative_orbital.id, {'isoval': -isoval, 'color': negative_color, 'opacity': opacity}]) self.current_js_orbitals.extend([positive_orbital, negative_orbital]) if render: self.render()
def draw_arrow(self, start, end=None, vector=None, radius=0.15, color='red', opacity=1.0, clickable=False, render=True): if (end is None) == (vector is None): raise ValueError( "Either 'end' or 'vector' should be passed, but not both.") if end is None: end = np.array(start) + np.array(vector) facestart = self._convert_units(start) faceend = self._convert_units(end) color = translate_color(color) spec = dict(start=self._list_to_jsvec(facestart), end=self._list_to_jsvec(faceend), radius=radius, color=color, alpha=opacity) js_shape = JSObject('shape') self.viewer('renderPyShape', ['Arrow', spec, js_shape.id, clickable]) if render: self.render() return js_shape
def draw_label(self, position, text, background='black', border='black', color='white', fontsize=14, opacity=1.0, render=True): js_label = JSObject('label') position = self._convert_units(position) color = translate_color(color) background = translate_color(background) spec = dict(position=self._list_to_jsvec(position), fontColor=color, backgroundColor=background, borderColor=border, fontSize=fontsize, backgroundOpacity=opacity) self.viewer('renderPyLabel', [text, spec, js_label.id]) if render: self.render() return js_label
def set_colors(self, colormap, render=True): """ Args: colormap(Mapping[str,List[Atoms]]): mapping of colors to atoms """ json = {} for color, atoms in colormap.iteritems(): json[translate_color(color)] = self._atoms_to_json(atoms) self.viewer('setColorArray', [json,]) if render: self.render()
def set_colors(self, colormap, render=True): """ Args: colormap(Mapping[str,List[Atoms]]): mapping of colors to atoms """ json = {} for color, atoms in colormap.iteritems(): json[translate_color(color)] = self._atoms_to_json(atoms) self.viewer('setColorArray', [ json, ]) if render: self.render()
def draw_label(self, position, text, background='black', border='black', color='white', fontsize=14, opacity=1.0, render=True): js_label = JSObject('label') position = self._convert_units(position) color = translate_color(color) background = translate_color(background) spec = dict(position=self._list_to_jsvec(position), fontColor=color, backgroundColor=background, borderColor=border, fontSize=fontsize, backgroundOpacity=opacity) self.viewer('renderPyLabel', [text, spec, js_label.id]) if render: self.render() return js_label
def draw_sphere(self, position, radius=2.0, color='red', opacity=1.0, clickable=False, render=True): js_shape = JSObject('shape') position = self._convert_units(position) radius = self._convert_units(radius) center = dict(x=position[0], y=position[1], z=position[2]) color = translate_color(color) self.viewer('renderPyShape', ['Sphere', dict(center=center, radius=radius, color=color, alpha=opacity), js_shape.id, clickable]) if render: self.render() return js_shape
def draw_sphere(self, position, radius=2.0, color='red', opacity=1.0, clickable=False, render=True): js_shape = JSObject('shape') position = self._convert_units(position) radius = self._convert_units(radius) center = dict(x=position[0], y=position[1], z=position[2]) color = translate_color(color) self.viewer('renderPyShape', [ 'Sphere', dict(center=center, radius=radius, color=color, alpha=opacity), js_shape.id, clickable ]) if render: self.render() return js_shape
def _change_style(self, backend_call, style_string, atoms, options, render): if atoms is None: atom_json = {} else: atom_json = self._atoms_to_json(atoms) if 'color' in options: try: options['color'] = translate_color(options['color']) except AttributeError: pass style = self.convert_style_name(style_string) if style is None: style_spec = {} else: style_spec = {style: options} self.viewer(backend_call, [atom_json, style_spec]) if render: self.render()
def _change_style(self, backend_call, style_string, atoms, options, render): if atoms is None: atom_json = {} else: atom_json = self._atoms_to_json(atoms) if 'color' in options: try: options['color'] = translate_color(options['color']) except AttributeError: pass style = self.convert_style_name(style_string) if style is None: style_spec = {} else: style_spec = {style: options} self.viewer(backend_call, [atom_json, style_spec]) if render: self.render()
def _draw3dmol_cylinder(self, color, start, end, draw_start_face, draw_end_face, opacity, radius, clickable, render, batch): color = translate_color(color) js_shape = JSObject('shape') facestart = self._convert_units(start) faceend = self._convert_units(end) radius = self._convert_units(radius) spec = dict( start=self._list_to_jsvec(facestart), end=self._list_to_jsvec(faceend), radius=radius, color=color, alpha=opacity, fromCap=draw_start_face, toCap=draw_end_face) args = ['Cylinder', spec, js_shape.id, clickable] if batch: self.batch_message('renderPyShape', args) else: self.viewer('renderPyShape', args) if render: self.render() return js_shape
def _draw3dmol_cylinder(self, color, start, end, draw_start_face, draw_end_face, opacity, radius, clickable, render, batch): color = translate_color(color) js_shape = JSObject('shape') facestart = self._convert_units(start) faceend = self._convert_units(end) radius = self._convert_units(radius) spec = dict(start=self._list_to_jsvec(facestart), end=self._list_to_jsvec(faceend), radius=radius, color=color, alpha=opacity, fromCap=draw_start_face, toCap=draw_end_face) args = ['Cylinder', spec, js_shape.id, clickable] if batch: self.batch_message('renderPyShape', args) else: self.viewer('renderPyShape', args) if render: self.render() return js_shape
def draw_arrow(self, start, end=None, vector=None, radius=0.15, color='red', opacity=1.0, clickable=False, render=True): if (end is None) == (vector is None): raise ValueError("Either 'end' or 'vector' should be passed, but not both.") if end is None: end = np.array(start) + np.array(vector) facestart = self._convert_units(start) faceend = self._convert_units(end) color = translate_color(color) spec = dict( start=self._list_to_jsvec(facestart), end=self._list_to_jsvec(faceend), radius=radius, color=color, alpha=opacity) js_shape = JSObject('shape') self.viewer('renderPyShape', ['Arrow', spec, js_shape.id, clickable]) if render: self.render() return js_shape
def test_example(): css_color = 'tomato' translated_color = translate_color(css_color) assert translated_color == '0xff6347'
def set_color(self, color, atoms=None, render=True): atom_json = self._atoms_to_json(atoms) color = translate_color(color) self.viewer('setAtomColor', [atom_json, color]) if render: self.render()
def set_background_color(self, color, opacity=1.0, render=True): color = translate_color(color) self.viewer('setBackgroundColor', args=[color, opacity]) if render: self.render()
def test_color_translation(): css_color = 'tomato' translated_color = translate_color(css_color) assert translated_color == '0xff6347'
def test_color_translation(): css_color = 'tomato' translated_color = translate_color(css_color) assert translated_color == '0xff6347'
def set_color(self, color, atoms=None, render=True): atom_json = self._atoms_to_json(atoms) color = translate_color(color) self.viewer('setAtomColor', [atom_json, color]) if render: self.render()
def set_background_color(self, color, opacity=1.0, render=True): color = translate_color(color) self.viewer('setBackgroundColor', args=[color, opacity]) if render: self.render()