@freyso had the question and also then the answer on how to custom color atoms in nglview while using ball and stick
I added couple of lines below to further explain what is going on
The create_colorscheme creates a dictionary and a color scheme label for the indices of the atoms with specific colors and a label (from the getcolor function) that is passed to the nglview.color._ColorScheme directly and then the entire nglview color scheme is passed to add_ball_and_stick function
def getcolor(atom):
""" Take a pyiron atom, return the custom color based on type and height"""
if atom.symbol == "Si":
if atom.position[2] > 8.5:
return '#0000aa'
else:
return '#c0c0ff'
elif atom.symbol == "B":
return 'green'
else:
return None #'white'
def create_colorscheme (struct, func):
""" Create a NGLview colorscheme from a structure and a color-defining python function"""
colors = {}
for i,a in enumerate(struct):
col = func(a)
if col is None:
col = 'white'
if col not in colors:
colors[col] = []
colors[col].append (i)
idxlist = { col : ",".join ([str(i) for i in idx]) for col,idx in colors.items () }
label = hashlib.md5("|".join([col + ":" + idx for col,idx in idxlist.items ()]).encode ("utf-8")).hexdigest ()
return nv.color._ColorScheme([ [col, "@" + idx] for col,idx in idxlist.items ()], label=label)
@freyso had the question and also then the answer on how to custom color atoms in nglview while using ball and stick
The create_colorscheme creates a dictionary and a color scheme label for the indices of the atoms with specific colors and a label (from the getcolor function) that is passed to the nglview.color._ColorScheme directly and then the entire nglview color scheme is passed to add_ball_and_stick function