BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSColours.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2022.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include "BDSColours.hh"
20#include "BDSDebug.hh"
21#include "BDSException.hh"
22#include "BDSUtilities.hh"
23
24#include "globals.hh" // geant4 types / globals
25#include "G4Colour.hh"
26
27#include <iomanip>
28#include <map>
29#include <sstream>
30
31BDSColours* BDSColours::instance = nullptr;
32
34{
35 if (!instance)
36 {instance = new BDSColours();}
37 return instance;
38}
39
40BDSColours::~BDSColours()
41{
42 instance = nullptr;
43}
44
46{
47 magnetName[1] = "sectorbend";
48 magnetName[2] = "quadrupole";
49 magnetName[3] = "sextupole";
50 magnetName[4] = "octupole";
51 magnetName[5] = "decapole";
52
53 // colour definitions (red,green,blue)
54 // some additional info:
55 // http://geant4.web.cern.ch/geant4/G4UsersDocuments/UsersGuides/ForApplicationDeveloper/html/Visualization/attributes.html
56 // https://en.wikipedia.org/wiki/Web_colors
57
58 // special
59 colours["default"] = new G4Colour(0.9, 0.9, 0.9); // almost white
60 colours["warning"] = new G4Colour(1, 0.078, 0.576); // hot warning pink
61
62 // visualisation of trajectories
63 colours["traj_neutral"] = new G4Colour(0.2, 0.7, 0.0, 0.2); // 0 charge - nicer green 0, semi-transparent as tonnes of photons usually
64 colours["traj_positive"] = new G4Colour(0.0, 0.2, 0.9); // nicer blue
65 colours["traj_negative"] = new G4Colour(0.8, 0.0, 0.0); // nicer red
66
67 // tunnel
68 colours["tunnel"] = new G4Colour(0.545, 0.533, 0.470); // a nice grey
69 colours["tunnelfloor"] = new G4Colour(0.5, 0.5, 0.45); // another grey
70 colours["soil"] = new G4Colour(0.545, 0.353, 0, 0.4);// brown
71
72 // elements - one for each type in parser/elementtype.cc
73 colours["marker"] = colours["default"];
74 colours["beampipe"] = new G4Colour(0.4, 0.4, 0.4); // dark grey
75 colours["drift"] = colours["beampipe"];
76 colours["sectorbend"] = new G4Colour(0, 0.4, 0.8); // blue
77 colours["sbend"] = colours["sectorbend"];
78 colours["rectangularbend"] = colours["sectorbend"];
79 colours["rbend"] = colours["sectorbend"];
80 colours["quadrupole"] = new G4Colour(0.82, 0.1, 0.1); // red
81 colours["sextupole"] = new G4Colour(1, 0.8, 0); // yellow
82 colours["octupole"] = new G4Colour(0, 0.6, 0.3); // green
83 colours["decapole"] = new G4Colour(0.3, 0.2, 0.7); // purple
84 colours["solenoid"] = new G4Colour(1, 0.549, 0, 0.7);// orange - semi transparent to see coil
85 colours["multipole"] = new G4Colour(0.466, 0.533, 0.6); // slate gray
86 colours["rfcavity"] = new G4Colour(0.466, 0.533, 0.6); // slate gray
87 colours["rf"] = colours["rfcavity"];
88 colours["srfcavity"] = new G4Colour(0.69, 0.769, 0.871); // light steel blue
89 colours["collimator"] = new G4Colour(0.25, 0.4, 0.2); // dark green
90 colours["ecol"] = colours["collimator"];
91 colours["jcol"] = colours["collimator"];
92 colours["rcol"] = colours["collimator"];
93 colours["jcol"] = colours["collimator"];
94 colours["target"] = colours["collimator"];
95 colours["muonspoiler"] = new G4Colour(0, 0.807, 0.819); // "light blue" / tab blue
96 colours["vkicker"] = new G4Colour(0.73, 0.33, 0.83); // blue
97 colours["hkicker"] = new G4Colour(0.3, 0.2, 0.7); // blue
98 colours["kicker"] = colours["sectorbend"];
99 colours["tkicker"] = colours["kicker"];
100 colours["degrader"] = new G4Colour(0.625, 0.625, 0.625); // silver
101 colours["wirescanner"] = colours["tunnel"]; // grey
102 colours["undulator"] = new G4Colour(0.625, 0.625, 0.625); // silver
103 colours["shield"] = colours["tunnel"];
104 colours["crystal"] = colours["srfcavity"];
105 colours["thinmultipole"] = colours["default"];
106 colours["dipolefringe"] = colours["default"];
107 colours["rmatrix"] = colours["default"];
108 colours["thinrmatrix"] = colours["rmatrix"];
109 colours["paralleltransporter"] = colours["default"];
110 colours["element"] = colours["default"];
111 colours["screen"] = new G4Colour(0.69, 0.769, 0.871); // slate grey
112 colours["awakescreen"] = colours["screen"];
113 colours["awakespectrometer"]= colours["sectorbend"];
114 colours["gap"] = colours["default"];
115 colours["opaquebox"] = new G4Colour(0.2, 0.2, 0.2, 0.2); // dark grey but mostly transparent
116
117 // some specific materials
118 colours["iron"] = new G4Colour(0.509, 0.321, 0.294);
119 colours["kapton"] = new G4Colour(0.929, 0.380, 0.082, 0.5);
120 colours["lead"] = new G4Colour(0.377, 0.408, 0.451);
121
122 // element parts
123 colours["coil"] = new G4Colour(0.722, 0.451, 0.2); // copper
124 colours["copper"] = colours["coil"];
125 colours["LHCcopperskin"] = new G4Colour(0.722, 0.525, 0.043); // copper
126 colours["LHCcoil"] = new G4Colour(0.9, 0.75, 0); // gold-ish
127 colours["LHCcollar"] = new G4Colour(0.9, 0.9, 0.9); // gainsboro
128 colours["LHCyoke"] = new G4Colour(0, 0.5, 1.0); // LHC blue
129 colours["LHCyokered"] = new G4Colour(*(colours["quadrupole"]));// quadrupole red
130 colours["gdml"] = new G4Colour(0.4, 0.2, 0); // poo brown
131 colours["screenframe"] = new G4Colour(0.7, 0.7, 0.7, 0.4);// light grey, semi-transparent
132
133 // general
134 colours["white"] = new G4Colour(G4Colour::White());
135 colours["gray"] = new G4Colour(G4Colour::Gray());
136 colours["grey"] = new G4Colour(G4Colour::Grey());
137 colours["black"] = new G4Colour(G4Colour::Black());
138 colours["brown"] = new G4Colour(G4Colour::Brown());
139 colours["red"] = new G4Colour(G4Colour::Red());
140 colours["green"] = new G4Colour(G4Colour::Green());
141 colours["blue"] = new G4Colour(G4Colour::Blue());
142 colours["cyan"] = new G4Colour(G4Colour::Cyan());
143 colours["magenta"] = new G4Colour(G4Colour::Magenta());
144 colours["yellow"] = new G4Colour(G4Colour::Yellow());
145 colours["reallyreallydarkgrey"] = new G4Colour(0.2, 0.2, 0.2);
146
147#ifdef BDSDEBUG
148 Print();
149#endif
150}
151
152void BDSColours::DefineColour(const G4String& name,
153 G4double red,
154 G4double green,
155 G4double blue,
156 G4double alpha,
157 G4bool normaliseTo255)
158{
159 if (colours.find(name) != colours.end())
160 {
161 G4cerr << "Colour \"" << name << "\" is already defined - clashing definitions" << G4endl;
162 G4cout << "Already defined colours are " << G4endl;
163 Print();
164 throw BDSException(__METHOD_NAME__, "duplicate colour definition");
165 }
166
167 BDS::EnsureInLimits(red,0,255);
168 BDS::EnsureInLimits(green,0,255);
169 BDS::EnsureInLimits(blue,0,255);
170 BDS::EnsureInLimits(alpha,0,1);
171 G4Colour* newColour;
172 if (normaliseTo255)
173 {newColour = new G4Colour(red/255.,green/255.,blue/255.,alpha);}
174 else
175 {newColour = new G4Colour(red, green, blue, alpha);}
176 colours[name] = newColour;
177}
178
180{
181 // auto-generate the manual colour table in rst syntax
182 G4cout << __METHOD_NAME__ << "Colour Table" << G4endl;
183 G4cout << "This is only the pre-defined BDSIM colours and not the user-defined ones." << G4endl;
184 G4cout << "+---------------------+-----+-----+-----+------+" << G4endl;
185 G4cout << "| Name | R | G | B | A |" << G4endl;
186 G4cout << "+=====================+=====+=====+=====+======+" << G4endl;
187 for (const auto& col : colours)
188 {
189 int r = (int)(col.second->GetRed() * 255);
190 int g = (int)(col.second->GetGreen() * 255);
191 int b = (int)(col.second->GetBlue() * 255);
192 double a = col.second->GetAlpha();
193 G4cout << "| " << std::setw(20) << col.first << "| "
194 << std::setw(3) << r << " | "
195 << std::setw(3) << g << " | "
196 << std::setw(3) << b << " | "
197 << std::setw(4) << a << " |" << G4endl;
198 G4cout << "+---------------------+-----+-----+-----+------+" << G4endl;
199 }
200}
201
202G4Colour* BDSColours::GetColour(const G4String& type,
203 G4bool normaliseTo255)
204{
205 G4String colourName = type;
206 G4bool canDefine = false;
207 if (BDS::StrContains(type, ":"))
208 {
209 colourName = type.substr(0, type.find(":"));
210 canDefine = true;
211 }
212
213 auto it = colours.find(colourName);
214 if (it != colours.end())
215 {// colour must therefore exist
216 return it->second;
217 }
218 else if (it == colours.end() && canDefine)
219 {
220 G4double r = 240;
221 G4double g = 240;
222 G4double b = 240; // default rgb is almost white but visible
223 G4String rgb = type.substr(type.find(":")+1); // everything after ':'
224 std::stringstream ss(rgb);
225 G4double a = 1;
226 ss >> r >> g >> b;
227 if (ss.rdbuf()->in_avail() != 0)
228 {ss >> a;}
229 DefineColour(colourName, r, g, b, a, normaliseTo255);
230 return colours[colourName];
231 }
232 else
233 {throw BDSException(__METHOD_NAME__, "unknown colour \"" + type + "\"");}
234}
Colour class that holds all colours used in BDSIM.
Definition: BDSColours.hh:33
static BDSColours * Instance()
singleton pattern
Definition: BDSColours.cc:33
G4Colour * GetColour(const G4String &type, G4bool normaliseTo255=true)
Get colour from name.
Definition: BDSColours.cc:202
void DefineColour(const G4String &name, G4double red, G4double green, G4double blue, G4double alpha=1, G4bool normaliseTo255=true)
Define a new colour.
Definition: BDSColours.cc:152
BDSColours()
Private constructor as singleton.
Definition: BDSColours.cc:45
void Print()
Definition: BDSColours.cc:179
std::map< G4int, G4String > magnetName
Definition: BDSColours.hh:61
std::map< G4String, G4Colour * > colours
A map of the colour for each type or component by name.
Definition: BDSColours.hh:64
General exception with possible name of object and message.
Definition: BDSException.hh:35
G4bool StrContains(const G4String &str, const G4String &test)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
Definition: BDSUtilities.cc:66
void EnsureInLimits(G4double &value, G4double lowerLimit, G4double upperLimit)