Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // 26 // 27 // 27 // 28 // 28 // 29 // John Allison 20th October 1996 29 // John Allison 20th October 1996 30 30 31 #include "G4Colour.hh" 31 #include "G4Colour.hh" 32 32 33 #include "G4Threading.hh" 33 #include "G4Threading.hh" 34 34 35 G4Colour::G4Colour (G4double r, G4double gr, G 35 G4Colour::G4Colour (G4double r, G4double gr, G4double b, G4double a): 36 red (r), green (gr), blue (b), alpha (a) 36 red (r), green (gr), blue (b), alpha (a) 37 { 37 { 38 if( red > 1.0 ){red = 1.0;} if( red < 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 < 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 < 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 < 41 if( alpha > 1.0 ){alpha = 1.0;} if( alpha < 0.0 ){alpha = 0.0;} 42 } 42 } 43 43 44 G4Colour::G4Colour (G4ThreeVector v): 44 G4Colour::G4Colour (G4ThreeVector v): 45 red (v.x()), green (v.y()), blue (v.z()), alph 45 red (v.x()), green (v.y()), blue (v.z()), alpha (1.) 46 { 46 { 47 if( red > 1.0 ){red = 1.0;} if( red < 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 < 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 < 49 if( blue > 1.0 ){blue = 1.0;} if( blue < 0.0 ){blue = 0.0;} 50 } 50 } 51 51 52 void G4Colour::SetRed (G4double r) 52 void G4Colour::SetRed (G4double r) 53 { 53 { 54 red = r; 54 red = r; 55 if( red > 1.0 ){red = 1.0;} if( red < 55 if( red > 1.0 ){red = 1.0;} if( red < 0.0 ){red = 0.0;} 56 } 56 } 57 57 58 void G4Colour::SetGreen (G4double gr) 58 void G4Colour::SetGreen (G4double gr) 59 { 59 { 60 green = gr; 60 green = gr; 61 if( green > 1.0 ){green = 1.0;} if( green < 61 if( green > 1.0 ){green = 1.0;} if( green < 0.0 ){green = 0.0;} 62 } 62 } 63 63 64 void G4Colour::SetBlue (G4double b) 64 void G4Colour::SetBlue (G4double b) 65 { 65 { 66 blue = b; 66 blue = b; 67 if( blue > 1.0 ){blue = 1.0;} if( blue < 67 if( blue > 1.0 ){blue = 1.0;} if( blue < 0.0 ){blue = 0.0;} 68 } 68 } 69 69 70 void G4Colour::SetAlpha (G4double a) 70 void G4Colour::SetAlpha (G4double a) 71 { 71 { 72 alpha = a; 72 alpha = a; 73 if( alpha > 1.0 ){alpha = 1.0;} if( alpha < 73 if( alpha > 1.0 ){alpha = 1.0;} if( alpha < 0.0 ){alpha = 0.0;} 74 } 74 } 75 75 76 G4Colour::operator G4ThreeVector() { 76 G4Colour::operator G4ThreeVector() { 77 return G4ThreeVector(red,green,blue); 77 return G4ThreeVector(red,green,blue); 78 } 78 } 79 79 80 std::ostream& operator << (std::ostream& os, c 80 std::ostream& operator << (std::ostream& os, const G4Colour& c) { 81 os << '(' << c.red << ',' << c.green << ',' 81 os << '(' << c.red << ',' << c.green << ',' << c.blue 82 << ',' << c.alpha << ')'; 82 << ',' << c.alpha << ')'; 83 const std::map<G4String, G4Colour>& colourMa 83 const std::map<G4String, G4Colour>& colourMap = G4Colour::GetMap(); 84 // Reverse iterator to pick up English spell 84 // Reverse iterator to pick up English spelling of grey!! :) 85 std::map<G4String, G4Colour>::const_reverse_ 85 std::map<G4String, G4Colour>::const_reverse_iterator ri; 86 for (ri = colourMap.rbegin(); ri != colourMa 86 for (ri = colourMap.rbegin(); ri != colourMap.rend(); ++ri) { 87 if (c == ri->second) { 87 if (c == ri->second) { 88 os << " (" << ri->first << ')'; 88 os << " (" << ri->first << ')'; 89 break; 89 break; 90 } 90 } 91 } 91 } 92 92 93 return os; 93 return os; 94 } 94 } 95 95 96 G4bool G4Colour::operator != (const G4Colour& 96 G4bool G4Colour::operator != (const G4Colour& c) const { 97 return (red != c.red) || 97 return (red != c.red) || 98 (green != c.green) || 98 (green != c.green) || 99 (blue != c.blue) || 99 (blue != c.blue) || 100 (alpha != c.alpha); 100 (alpha != c.alpha); 101 } 101 } 102 102 103 std::map<G4String, G4Colour> G4Colour::fColour 103 std::map<G4String, G4Colour> G4Colour::fColourMap; 104 G4bool G4Colour::fInitColourMap = false; 104 G4bool G4Colour::fInitColourMap = false; 105 105 106 void G4Colour::AddToMap(const G4String& key, c 106 void G4Colour::AddToMap(const G4String& key, const G4Colour& colour) 107 { 107 { 108 // Allow only master thread to populate the 108 // Allow only master thread to populate the map 109 if (!G4Threading::IsMasterThread()) { 109 if (!G4Threading::IsMasterThread()) { 110 static G4bool first = true; 110 static G4bool first = true; 111 if (first) { 111 if (first) { 112 first = false; 112 first = false; 113 G4Exception 113 G4Exception 114 ("G4Colour::AddToMap(const G4String& key 114 ("G4Colour::AddToMap(const G4String& key, const G4Colour& colour)", 115 "greps0002", JustWarning, 115 "greps0002", JustWarning, 116 "Attempt to add to colour map from non- 116 "Attempt to add to colour map from non-master thread."); 117 } 117 } 118 return; 118 return; 119 } 119 } 120 120 121 // Add standard colours to map << 122 InitialiseColourMap(); // Initialises if no << 123 << 124 // Convert to lower case since colour map is 121 // Convert to lower case since colour map is case insensitive 125 G4String myKey = G4StrUtil::to_lower_copy(ke 122 G4String myKey = G4StrUtil::to_lower_copy(key); 126 123 127 if (fColourMap.find(myKey) == fColourMap.end 124 if (fColourMap.find(myKey) == fColourMap.end()) fColourMap[myKey] = colour; 128 else { 125 else { 129 G4ExceptionDescription ed; 126 G4ExceptionDescription ed; 130 ed << "G4Colour with key " << myKey << " a 127 ed << "G4Colour with key " << myKey << " already exists." << G4endl; 131 G4Exception 128 G4Exception 132 ("G4Colour::AddToMap(const G4String& key 129 ("G4Colour::AddToMap(const G4String& key, const G4Colour& colour)", 133 "greps0001", JustWarning, ed, 130 "greps0001", JustWarning, ed, 134 "Colour key exists"); 131 "Colour key exists"); 135 } 132 } 136 } 133 } 137 134 138 void G4Colour::InitialiseColourMap() 135 void G4Colour::InitialiseColourMap() 139 { 136 { 140 if (fInitColourMap) return; 137 if (fInitColourMap) return; 141 138 142 fInitColourMap = true; 139 fInitColourMap = true; 143 140 144 // Standard colours 141 // Standard colours 145 AddToMap("white", G4Colour::White()); 142 AddToMap("white", G4Colour::White()); 146 AddToMap("grey", G4Colour::Grey()); 143 AddToMap("grey", G4Colour::Grey()); 147 AddToMap("gray", G4Colour::Gray()); 144 AddToMap("gray", G4Colour::Gray()); 148 AddToMap("black", G4Colour::Black()); 145 AddToMap("black", G4Colour::Black()); 149 AddToMap("brown", G4Colour::Brown()); 146 AddToMap("brown", G4Colour::Brown()); 150 AddToMap("red", G4Colour::Red()); 147 AddToMap("red", G4Colour::Red()); 151 AddToMap("green", G4Colour::Green()); 148 AddToMap("green", G4Colour::Green()); 152 AddToMap("blue", G4Colour::Blue()); 149 AddToMap("blue", G4Colour::Blue()); 153 AddToMap("cyan", G4Colour::Cyan()); 150 AddToMap("cyan", G4Colour::Cyan()); 154 AddToMap("magenta", G4Colour::Magenta()); 151 AddToMap("magenta", G4Colour::Magenta()); 155 AddToMap("yellow", G4Colour::Yellow()); 152 AddToMap("yellow", G4Colour::Yellow()); 156 } 153 } 157 154 158 G4bool G4Colour::GetColour(const G4String& key << 155 G4bool G4Colour::GetColour(const G4String& key, G4Colour& result) 159 // Get colour for given key, placing it in res << 160 // The key is usually the name of the colour. << 161 // The key is case insensitive. << 162 // Returns false if key doesn't exist, leaving << 163 { 156 { 164 // Add standard colours to map 157 // Add standard colours to map 165 InitialiseColourMap(); // Initialises if no 158 InitialiseColourMap(); // Initialises if not already initialised 166 159 167 G4String myKey = G4StrUtil::to_lower_copy(ke 160 G4String myKey = G4StrUtil::to_lower_copy(key); 168 161 169 // NOLINTNEXTLINE(modernize-use-auto): Expli 162 // NOLINTNEXTLINE(modernize-use-auto): Explicitly want a const_iterator 170 std::map<G4String, G4Colour>::const_iterator 163 std::map<G4String, G4Colour>::const_iterator iter = fColourMap.find(myKey); 171 164 172 // Don't modify "result" if colour was not f 165 // Don't modify "result" if colour was not found in map 173 if (iter == fColourMap.cend()) { << 166 if (iter == fColourMap.cend()) return false; 174 G4ExceptionDescription ed; << 167 175 ed << "Colour \"" << key << "\" not found. << 176 G4Exception("G4Colour::GetColour", "greps0 << 177 return false; << 178 } << 179 << 180 result = iter->second; 168 result = iter->second; 181 169 182 return true; 170 return true; 183 } 171 } 184 172 185 const std::map<G4String, G4Colour>& G4Colour:: 173 const std::map<G4String, G4Colour>& G4Colour::GetMap() 186 { 174 { 187 // Add standard colours to map 175 // Add standard colours to map 188 InitialiseColourMap(); // Initialises if no 176 InitialiseColourMap(); // Initialises if not already initialised 189 177 190 return fColourMap; 178 return fColourMap; 191 } 179 } 192 180 193 G4bool G4Colour::operator< (const G4Colour& rh 181 G4bool G4Colour::operator< (const G4Colour& rhs) const 194 { 182 { 195 if (red < rhs.red) return true; 183 if (red < rhs.red) return true; 196 if (red == rhs.red) { 184 if (red == rhs.red) { 197 if (green < rhs.green) return true; 185 if (green < rhs.green) return true; 198 if (green == rhs.green) { 186 if (green == rhs.green) { 199 if (blue < rhs.blue) return true; 187 if (blue < rhs.blue) return true; 200 if (blue == rhs.blue) { 188 if (blue == rhs.blue) { 201 if (alpha < rhs.alpha) return true; 189 if (alpha < rhs.alpha) return true; 202 } 190 } 203 } 191 } 204 } 192 } 205 return false; 193 return false; 206 } 194 } 207 195