# Using different colors for the same type of atoms in MoleculePlot3D

I would like to plot a molecule in 3D and use different colors for the same atom type in the molecule. For example, by using:

MoleculePlot3D[Molecule["NC(=O)C[C@H](C(=O)O)N"], ColorRules -> {"C" -> Black}]


all C atoms become Black. But how can I make, for example, the first C atom green, the second orange, etc?

Although I was suspecting Jason B. had an undocumented option up his sleeve, I will nevertheless post an alternative solution.

colors = {Green, Orange, Pink, Yellow};

(* Generate regular plot *)
mol = MoleculePlot3D[Molecule["NC(=O)C[C@H](C(=O)O)N"]]

(* Extract atom indices of carbons *)
ind = Flatten@Position[MoleculeValue["NC(=O)C[C@H](C(=O)O)N", "FullAtomList"], Atom["C"]]

(* Make colored spheres *)

(* Replace carbons with colored spheres *)
mol /. {{RGBColor[__], Sphere[ind, r_]} :> (atoms /. radius -> r)}


• This is great! Thanks! Wonder if one can easily change Opacity[] of the spheres. I tried so, but failed. Oct 21, 2022 at 11:01
• You can: atoms = MapThread[{#1, Opacity[.5], Sphere[#2, radius]} &, {colors, ind}];. Just a note: take a look also at @JasonB. solution in which the bonds get the appropriate colors! My solution recolors only the atoms. Oct 21, 2022 at 12:39

This is underdocumented but the "ColorRules" option can take both atom indices and patterns.

MoleculePlot3D[Molecule["NC(=O)C[C@H](C(=O)O)N"],
ColorRules -> {2 -> Green, "C" -> Black, "O" -> Orange, 8 -> Pink}]


• Is it possible to target directly "the second carbon atom" with this approach, as the OP wants? Otherwise, one has to first extract the correct indices ... Oct 19, 2022 at 19:33
• I do not have a simple way to get that, other than something like AtomList[Molecule["caffeine"], "C", "AtomIndex"][[2]] Oct 19, 2022 at 19:47
• Thanks, @JasonB. Is there a way to add opacity as well? I tried x, but it did not work out. x = MoleculePlot3D[Molecule["NC(=O)C[C@H](C(=O)O)N"], ColorRules -> {2 -> {Green, Opacity[0.5]}}] Oct 21, 2022 at 22:10
• @david1983, use Directive, for example: MoleculePlot3D[Molecule["NC(=O)C[C@H](C(=O)O)N"], ColorRules -> {2 -> Directive[Red, Opacity[.2]], "C" -> Black, "O" -> Orange, 8 -> Pink}] Oct 21, 2022 at 22:37