Geant4  10.00.p01
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 // $Id: G4Colour.cc 69802 2013-05-15 14:52:57Z gcosmo $
28 //
29 //
30 // John Allison 20th October 1996
31 
32 #include "G4Colour.hh"
33 
35 red (r), green (gr), blue (b), alpha (a)
36 {
37  if( red > 1.0 ){red = 1.0;} if( red < 0.0 ){red = 0.0;}
38  if( green > 1.0 ){green = 1.0;} if( green < 0.0 ){green = 0.0;}
39  if( blue > 1.0 ){blue = 1.0;} if( blue < 0.0 ){blue = 0.0;}
40  if( alpha > 1.0 ){alpha = 1.0;} if( alpha < 0.0 ){alpha = 0.0;}
41 }
42 
44 red (v.x()), green (v.y()), blue (v.z()), alpha (1.)
45 {
46  if( red > 1.0 ){red = 1.0;} if( red < 0.0 ){red = 0.0;}
47  if( green > 1.0 ){green = 1.0;} if( green < 0.0 ){green = 0.0;}
48  if( blue > 1.0 ){blue = 1.0;} if( blue < 0.0 ){blue = 0.0;}
49 }
50 
51 G4Colour::operator G4ThreeVector() {
52  return G4ThreeVector(red,green,blue);
53 }
54 
55 std::ostream& operator << (std::ostream& os, const G4Colour& c) {
56  os << '(' << c.red << ',' << c.green << ',' << c.blue
57  << ',' << c.alpha << ')';
58  const std::map<G4String, G4Colour>& colourMap = G4Colour::GetMap();
59  // Reverse iterator to pick up English spelling of grey!! :)
60  std::map<G4String, G4Colour>::const_reverse_iterator ri;
61  for (ri = colourMap.rbegin(); ri != colourMap.rend(); ++ri) {
62  if (c == ri->second) {
63  os << " (" << ri->first << ')';
64  break;
65  }
66  }
67 
68  return os;
69 }
70 
72  if (
73  (red != c.red) ||
74  (green != c.green) ||
75  (blue != c.blue) ||
76  (alpha != c.alpha)
77  )
78  return true;
79  return false;
80 }
81 
82 G4ThreadLocal std::map<G4String, G4Colour> *G4Colour::fColourMap = 0;
84 
85 void
86 G4Colour::AddToMap(const G4String& key, const G4Colour& colour)
87 {
88  if (!fColourMap)
89  fColourMap = new std::map<G4String, G4Colour>;
90 
91  // Convert to lower case since colour map is case insensitive
92  G4String myKey(key);
93  myKey.toLower();
94 
95  std::map<G4String, G4Colour>::iterator iter = fColourMap->find(myKey);
96 
97  if (iter == fColourMap->end()) (*fColourMap)[myKey] = colour;
98  else {
100  ed << "G4Colour with key "<<myKey<<" already exists."<<G4endl;
102  ("G4Colour::AddToMap(const G4String& key, const G4Colour& colour)",
103  "greps0001", JustWarning, ed,
104  "Colour key exists");
105  }
106 }
107 
108 void
110 {
111  // Standard colours
112  AddToMap("white", G4Colour::White());
113  AddToMap("grey", G4Colour::Grey());
114  AddToMap("gray", G4Colour::Gray());
115  AddToMap("black", G4Colour::Black());
116  AddToMap("brown", G4Colour::Brown());
117  AddToMap("red", G4Colour::Red());
118  AddToMap("green", G4Colour::Green());
119  AddToMap("blue", G4Colour::Blue());
120  AddToMap("cyan", G4Colour::Cyan());
121  AddToMap("magenta", G4Colour::Magenta());
122  AddToMap("yellow", G4Colour::Yellow());
123 }
124 
125 bool
126 G4Colour::GetColour(const G4String& key, G4Colour& result)
127 {
128  if (false == fInitColourMap) {
129  fInitColourMap = true;
130  // Add standard colours to map
132  }
133 
134  G4String myKey(key);
135  myKey.toLower();
136 
137  std::map<G4String, G4Colour>::iterator iter = fColourMap->find(myKey);
138 
139  // Don't modify "result" if colour was not found in map
140  if (iter == fColourMap->end()) return false;
141 
142  result = iter->second;
143 
144  return true;
145 }
146 
147 const std::map<G4String, G4Colour>& G4Colour::GetMap()
148 {
149  if (false == fInitColourMap) {
150  fInitColourMap = true;
151  // Add standard colours to map
153  }
154 
155  return *fColourMap;
156 }
static G4Colour Gray()
Definition: G4Colour.hh:144
Definition: test07.cc:36
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
CLHEP::Hep3Vector G4ThreeVector
G4double alpha
Definition: G4Colour.hh:131
static G4Colour Green()
Definition: G4Colour.hh:149
G4double z
Definition: TRTMaterials.hh:39
static G4bool GetColour(const G4String &key, G4Colour &result)
Definition: G4Colour.cc:126
static G4Colour Magenta()
Definition: G4Colour.hh:152
G4double a
Definition: TRTMaterials.hh:39
static G4Colour Brown()
Definition: G4Colour.hh:147
#define G4ThreadLocal
Definition: tls.hh:52
Definition: test07.cc:36
G4Colour(G4double r=1., G4double g=1., G4double b=1., G4double a=1.)
Definition: G4Colour.cc:34
static G4Colour Black()
Definition: G4Colour.hh:146
G4double red
Definition: G4Colour.hh:131
static G4Colour Grey()
Definition: G4Colour.hh:145
static void InitialiseColourMap()
Definition: G4Colour.cc:109
bool G4bool
Definition: G4Types.hh:79
std::ostream & operator<<(std::ostream &os, const G4Colour &c)
Definition: G4Colour.cc:55
static void AddToMap(const G4String &key, const G4Colour &colour)
Definition: G4Colour.cc:86
static G4Colour Blue()
Definition: G4Colour.hh:150
void toLower()
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4double blue
Definition: G4Colour.hh:131
static G4Colour Cyan()
Definition: G4Colour.hh:151
static G4ThreadLocal G4bool fInitColourMap
Definition: G4Colour.hh:134
#define G4endl
Definition: G4ios.hh:61
static G4Colour Red()
Definition: G4Colour.hh:148
G4double green
Definition: G4Colour.hh:131
static const std::map< G4String, G4Colour > & GetMap()
Definition: G4Colour.cc:147
static G4ThreadLocal std::map< G4String, G4Colour > * fColourMap
Definition: G4Colour.hh:133
double G4double
Definition: G4Types.hh:76
static const G4double alpha
static G4Colour Yellow()
Definition: G4Colour.hh:153
G4bool operator!=(const G4Colour &c) const
Definition: G4Colour.cc:71
static G4Colour White()
Definition: G4Colour.hh:143