Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4Colour.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26//
27//
28//
29// John Allison 20th October 1996
30
31#include "G4Colour.hh"
32
33#include "G4Threading.hh"
34
35#include <algorithm>
36
37namespace {
38 auto clamp = [](double rgba){return std::clamp(rgba, 0., 1.);};
39}
40
41// clang-format off
43red(clamp(r)), green(clamp(gr)), blue(clamp(b)), alpha(clamp(a))
44{}
45
47red(clamp(v.x())), green(clamp(v.y())), blue(clamp(v.z())), alpha (1.)
48{}
49
50void G4Colour::SetRed (G4double r) {red = clamp(r);}
51void G4Colour::SetGreen (G4double gr) {green = clamp(gr);}
52void G4Colour::SetBlue (G4double b) {blue = clamp(b);}
53void G4Colour::SetAlpha (G4double a) {alpha = clamp(a);}
54// clang-format on
55
56G4Colour::operator G4ThreeVector() {
57 return G4ThreeVector(red,green,blue);
58}
59
60std::ostream& operator << (std::ostream& os, const G4Colour& c) {
61 os << '(' << c.red << ',' << c.green << ',' << c.blue
62 << ',' << c.alpha << ')';
63 const std::map<G4String, G4Colour>& colourMap = G4Colour::GetMap();
64 // Reverse iterator to pick up English spelling of grey!! :)
65 std::map<G4String, G4Colour>::const_reverse_iterator ri;
66 for (ri = colourMap.rbegin(); ri != colourMap.rend(); ++ri) {
67 if (c == ri->second) {
68 os << " (" << ri->first << ')';
69 break;
70 }
71 }
72
73 return os;
74}
75
77 return (red != c.red) ||
78 (green != c.green) ||
79 (blue != c.blue) ||
80 (alpha != c.alpha);
81}
82
83std::map<G4String, G4Colour> G4Colour::fColourMap;
84G4bool G4Colour::fInitColourMap = false;
85
86void G4Colour::AddToMap(const G4String& key, const G4Colour& colour)
87{
88 // Allow only master thread to populate the map
90 static G4bool first = true;
91 if (first) {
92 first = false;
94 ("G4Colour::AddToMap(const G4String& key, const G4Colour& colour)",
95 "greps0002", JustWarning,
96 "Attempt to add to colour map from non-master thread.");
97 }
98 return;
99 }
100
101 // Add standard colours to map
102 InitialiseColourMap(); // Initialises if not already initialised
103
104 // Convert to lower case since colour map is case insensitive
106
107 if (fColourMap.find(myKey) == fColourMap.end()) fColourMap[myKey] = colour;
108 else {
110 ed << "G4Colour with key " << myKey << " already exists." << G4endl;
112 ("G4Colour::AddToMap(const G4String& key, const G4Colour& colour)",
113 "greps0001", JustWarning, ed,
114 "Colour key exists");
115 }
116}
117
119{
120 if (fInitColourMap) return;
121
122 fInitColourMap = true;
123
124 // clang-format off
125 // Standard colours
126 AddToMap("white", G4Colour::White());
127 AddToMap("grey", G4Colour::Grey());
128 AddToMap("gray", G4Colour::Gray());
129 AddToMap("black", G4Colour::Black());
130 AddToMap("brown", G4Colour::Brown());
131 AddToMap("red", G4Colour::Red());
132 AddToMap("green", G4Colour::Green());
133 AddToMap("blue", G4Colour::Blue());
134 AddToMap("cyan", G4Colour::Cyan());
135 AddToMap("magenta", G4Colour::Magenta());
136 AddToMap("yellow", G4Colour::Yellow());
137 // clang-format off
138}
139
141// Get colour for given key, placing it in result.
142// The key is usually the name of the colour.
143// The key is case insensitive.
144// Returns false if key doesn't exist, leaving result unchanged.
145{
146 // Add standard colours to map
147 InitialiseColourMap(); // Initialises if not already initialised
148
150
151 // NOLINTNEXTLINE(modernize-use-auto): Explicitly want a const_iterator
152 std::map<G4String, G4Colour>::const_iterator iter = fColourMap.find(myKey);
153
154 // Don't modify "result" if colour was not found in map
155 if (iter == fColourMap.cend()) {
157 ed << "Colour \"" << key << "\" not found. No action taken.";
158 G4Exception("G4Colour::GetColour", "greps0003", JustWarning, ed);
159 return false;
160 }
161
162 result = iter->second;
163
164 return true;
165}
166
167const std::map<G4String, G4Colour>& G4Colour::GetMap()
168{
169 // Add standard colours to map
170 InitialiseColourMap(); // Initialises if not already initialised
171
172 return fColourMap;
173}
174
176{
177 if (red < rhs.red) return true;
178 if (red == rhs.red) {
179 if (green < rhs.green) return true;
180 if (green == rhs.green) {
181 if (blue < rhs.blue) return true;
182 if (blue == rhs.blue) {
183 if (alpha < rhs.alpha) return true;
184 }
185 }
186 }
187 return false;
188}
std::ostream & operator<<(std::ostream &os, const G4Colour &c)
Definition G4Colour.cc:60
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::ostringstream G4ExceptionDescription
CLHEP::Hep3Vector G4ThreeVector
double G4double
Definition G4Types.hh:83
bool G4bool
Definition G4Types.hh:86
#define G4endl
Definition G4ios.hh:67
static G4Colour White()
Definition G4Colour.hh:175
static G4bool GetColour(const G4String &key, G4Colour &result)
Definition G4Colour.cc:140
void SetAlpha(G4double)
Definition G4Colour.cc:53
static G4Colour Yellow()
Definition G4Colour.hh:185
static G4Colour Green()
Definition G4Colour.hh:181
static void AddToMap(const G4String &key, const G4Colour &colour)
Definition G4Colour.cc:86
static void InitialiseColourMap()
Definition G4Colour.cc:118
static G4Colour Red()
Definition G4Colour.hh:180
static G4Colour Brown()
Definition G4Colour.hh:179
static G4Colour Grey()
Definition G4Colour.hh:177
void SetGreen(G4double)
Definition G4Colour.cc:51
G4Colour(G4double r=1., G4double gr=1., G4double b=1., G4double a=1.)
Definition G4Colour.cc:42
static const std::map< G4String, G4Colour > & GetMap()
Definition G4Colour.cc:167
static G4Colour Black()
Definition G4Colour.hh:178
G4bool operator!=(const G4Colour &c) const
Definition G4Colour.cc:76
static G4Colour Magenta()
Definition G4Colour.hh:184
static G4Colour Blue()
Definition G4Colour.hh:182
void SetRed(G4double)
Definition G4Colour.cc:50
static G4Colour Gray()
Definition G4Colour.hh:176
void SetBlue(G4double)
Definition G4Colour.cc:52
G4bool operator<(const G4Colour &rhs) const
Definition G4Colour.cc:175
static G4Colour Cyan()
Definition G4Colour.hh:183
G4String to_lower_copy(G4String str)
Return lowercased copy of string.
G4bool IsMasterThread()
T clamp(T value, T lo, T hi)