Geant4-11
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
36red (r), green (gr), blue (b), alpha (a)
37{
38 if( red > 1.0 ){red = 1.0;} if( red < 0.0 ){red = 0.0;}
39 if( green > 1.0 ){green = 1.0;} if( green < 0.0 ){green = 0.0;}
40 if( blue > 1.0 ){blue = 1.0;} if( blue < 0.0 ){blue = 0.0;}
41 if( alpha > 1.0 ){alpha = 1.0;} if( alpha < 0.0 ){alpha = 0.0;}
42}
43
45red (v.x()), green (v.y()), blue (v.z()), alpha (1.)
46{
47 if( red > 1.0 ){red = 1.0;} if( red < 0.0 ){red = 0.0;}
48 if( green > 1.0 ){green = 1.0;} if( green < 0.0 ){green = 0.0;}
49 if( blue > 1.0 ){blue = 1.0;} if( blue < 0.0 ){blue = 0.0;}
50}
51
53{
54 red = r;
55 if( red > 1.0 ){red = 1.0;} if( red < 0.0 ){red = 0.0;}
56}
57
59{
60 green = gr;
61 if( green > 1.0 ){green = 1.0;} if( green < 0.0 ){green = 0.0;}
62}
63
65{
66 blue = b;
67 if( blue > 1.0 ){blue = 1.0;} if( blue < 0.0 ){blue = 0.0;}
68}
69
71{
72 alpha = a;
73 if( alpha > 1.0 ){alpha = 1.0;} if( alpha < 0.0 ){alpha = 0.0;}
74}
75
76G4Colour::operator G4ThreeVector() {
77 return G4ThreeVector(red,green,blue);
78}
79
80std::ostream& operator << (std::ostream& os, const G4Colour& c) {
81 os << '(' << c.red << ',' << c.green << ',' << c.blue
82 << ',' << c.alpha << ')';
83 const std::map<G4String, G4Colour>& colourMap = G4Colour::GetMap();
84 // Reverse iterator to pick up English spelling of grey!! :)
85 std::map<G4String, G4Colour>::const_reverse_iterator ri;
86 for (ri = colourMap.rbegin(); ri != colourMap.rend(); ++ri) {
87 if (c == ri->second) {
88 os << " (" << ri->first << ')';
89 break;
90 }
91 }
92
93 return os;
94}
95
97 if (
98 (red != c.red) ||
99 (green != c.green) ||
100 (blue != c.blue) ||
101 (alpha != c.alpha)
102 )
103 return true;
104 return false;
105}
106
107std::map<G4String, G4Colour> G4Colour::fColourMap;
109
110void G4Colour::AddToMap(const G4String& key, const G4Colour& colour)
111{
112 // Allow only master thread to populate the map
114 static G4bool first = true;
115 if (first) {
116 first = false;
118 ("G4Colour::AddToMap(const G4String& key, const G4Colour& colour)",
119 "greps0002", JustWarning,
120 "Attempt to add to colour map from non-master thread.");
121 }
122 return;
123 }
124
125 // Convert to lower case since colour map is case insensitive
127
128 std::map<G4String, G4Colour>::iterator iter = fColourMap.find(myKey);
129
130 if (iter == fColourMap.end()) fColourMap[myKey] = colour;
131 else {
133 ed << "G4Colour with key " << myKey << " already exists." << G4endl;
135 ("G4Colour::AddToMap(const G4String& key, const G4Colour& colour)",
136 "greps0001", JustWarning, ed,
137 "Colour key exists");
138 }
139}
140
142{
143 if (fInitColourMap) return;
144
145 fInitColourMap = true;
146
147 // Standard colours
148 AddToMap("white", G4Colour::White());
149 AddToMap("grey", G4Colour::Grey());
150 AddToMap("gray", G4Colour::Gray());
151 AddToMap("black", G4Colour::Black());
152 AddToMap("brown", G4Colour::Brown());
153 AddToMap("red", G4Colour::Red());
154 AddToMap("green", G4Colour::Green());
155 AddToMap("blue", G4Colour::Blue());
156 AddToMap("cyan", G4Colour::Cyan());
157 AddToMap("magenta", G4Colour::Magenta());
158 AddToMap("yellow", G4Colour::Yellow());
159}
160
162{
163 // Add standard colours to map
164 InitialiseColourMap(); // Initialises if not already initialised
165
167
168 std::map<G4String, G4Colour>::iterator iter = fColourMap.find(myKey);
169
170 // Don't modify "result" if colour was not found in map
171 if (iter == fColourMap.end()) return false;
172
173 result = iter->second;
174
175 return true;
176}
177
178const std::map<G4String, G4Colour>& G4Colour::GetMap()
179{
180 // Add standard colours to map
181 InitialiseColourMap(); // Initialises if not already initialised
182
183 return fColourMap;
184}
185
187{
188 if (red < rhs.red) return true;
189 else if (red == rhs.red) {
190 if (green < rhs.green) return true;
191 else if (green == rhs.green) {
192 if (blue < rhs.blue) return true;
193 else if (blue == rhs.blue) {
194 if (alpha < rhs.alpha) return true;
195 }}}
196 return false;
197}
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:40
static const G4double alpha
CLHEP::Hep3Vector G4ThreeVector
double G4double
Definition: G4Types.hh:83
bool G4bool
Definition: G4Types.hh:86
#define G4endl
Definition: G4ios.hh:57
static G4Colour White()
Definition: G4Colour.hh:156
static G4bool GetColour(const G4String &key, G4Colour &result)
Definition: G4Colour.cc:161
void SetAlpha(G4double)
Definition: G4Colour.cc:70
static G4Colour Yellow()
Definition: G4Colour.hh:166
static G4Colour Green()
Definition: G4Colour.hh:162
static void AddToMap(const G4String &key, const G4Colour &colour)
Definition: G4Colour.cc:110
static void InitialiseColourMap()
Definition: G4Colour.cc:141
static G4Colour Red()
Definition: G4Colour.hh:161
static G4Colour Brown()
Definition: G4Colour.hh:160
G4double alpha
Definition: G4Colour.hh:145
static G4bool fInitColourMap
Definition: G4Colour.hh:148
static G4Colour Grey()
Definition: G4Colour.hh:158
G4double green
Definition: G4Colour.hh:145
void SetGreen(G4double)
Definition: G4Colour.cc:58
static const std::map< G4String, G4Colour > & GetMap()
Definition: G4Colour.cc:178
static G4Colour Black()
Definition: G4Colour.hh:159
G4bool operator!=(const G4Colour &c) const
Definition: G4Colour.cc:96
static G4Colour Magenta()
Definition: G4Colour.hh:165
static G4Colour Blue()
Definition: G4Colour.hh:163
void SetRed(G4double)
Definition: G4Colour.cc:52
static G4Colour Gray()
Definition: G4Colour.hh:157
G4Colour(G4double r_=1., G4double g_=1., G4double b_=1., G4double a_=1.)
Definition: G4Colour.cc:35
G4double red
Definition: G4Colour.hh:145
G4double blue
Definition: G4Colour.hh:145
void SetBlue(G4double)
Definition: G4Colour.cc:64
G4bool operator<(const G4Colour &rhs) const
Definition: G4Colour.cc:186
static std::map< G4String, G4Colour > fColourMap
Definition: G4Colour.hh:147
static G4Colour Cyan()
Definition: G4Colour.hh:164
std::ostream & operator<<(std::ostream &, const BasicVector3D< float > &)
G4String to_lower_copy(G4String str)
Return lowercased copy of string.
G4bool IsMasterThread()
Definition: G4Threading.cc:124