10

Mathematica takes the standard color code for atoms when plotting an *.xyz file, but I want to customize the color for the specific atoms.

I really become desperate as I did not get it so far.

Example:

Import["ExampleData/caffeine.xyz"]

caffeine

It doesn't really matter which colors are used. I just need to know how to change the default ones.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Sebastian
  • 101
  • 2

3 Answers3

13

One can use the (undocumented?) option ColorRules:

Import["ExampleData/caffeine.xyz", 
 ColorRules -> {"H" -> Red, "C" -> Black, "N" -> Darker@Green, "O" -> White}]

Mathematica graphics

Addendum: Other options may be found here: Options[Graphics`MoleculePlotDump`iMoleculePlot3D]. Note: The option ColorFunction seems to be unimplemented.

Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • 2
  • Great find! For the particular problem I can't imagine a better answer. I still do like the generality of my approach and I hope people find value in it elsewhere. – Mr.Wizard Nov 13 '14 at 09:34
  • @Mr.Wizard Thanks. I liked your answer so much, I nearly gave up after reading it. But I was curious how the bonds are determined, when bond data is missing in the xyz format. So I kept looking. – Michael E2 Nov 13 '14 at 12:35
10

Try this:

t = Import["c:\\work\\temp\\1.xyz"];

enter image description here

allColors = {};
(*get all colors from Graphics3D*)
Scan[If[MatchQ[#, RGBColor[__]], AppendTo[allColors, #]] &, t, 
  Infinity];
allColors = DeleteDuplicates[allColors]
(*{RGBColor[0.65, 0.7, 0.7], RGBColor[0.4, 0.4, 0.4]}*)
(*replace all obtained colors with any another*)
replaceRules = MapThread[Rule, {allColors, {RGBColor[0.4, 0.4, 1], RGBColor[0.4, 1, 1]}}];
t /. replaceRules

enter image description here

molekyla777
  • 2,888
  • 1
  • 14
  • 28
6

This question is closely related to: How to change element color in Periodic Table? and my solution there works here as well. However thanks to this question I realized that it had a bug which I have now fixed.

An example for your application:

(* please load code from linked answer *)

withScheme[
  Import["ExampleData/caffeine.xyz"],
  "Atoms",
  {"H" -> Blue, "O" -> Yellow, "N" -> Orange}
]

enter image description here

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371