G4Colour.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 // 
00030 // John Allison 20th October 1996
00031 
00032 #include "G4Colour.hh"
00033 
00034 G4Colour::G4Colour (G4double r, G4double gr, G4double b, G4double a):
00035 red (r), green (gr), blue (b), alpha (a)
00036 {
00037   if( red   > 1.0 ){red   = 1.0;} if( red   < 0.0 ){red   = 0.0;}
00038   if( green > 1.0 ){green = 1.0;} if( green < 0.0 ){green = 0.0;}
00039   if( blue  > 1.0 ){blue  = 1.0;} if( blue  < 0.0 ){blue  = 0.0;}
00040   if( alpha > 1.0 ){alpha = 1.0;} if( alpha < 0.0 ){alpha = 0.0;}
00041 }
00042 
00043 G4Colour::G4Colour (G4ThreeVector v):
00044 red (v.x()), green (v.y()), blue (v.z()), alpha (1.)
00045 {
00046   if( red   > 1.0 ){red   = 1.0;} if( red   < 0.0 ){red   = 0.0;}
00047   if( green > 1.0 ){green = 1.0;} if( green < 0.0 ){green = 0.0;}
00048   if( blue  > 1.0 ){blue  = 1.0;} if( blue  < 0.0 ){blue  = 0.0;}
00049 }
00050 
00051 G4Colour::operator G4ThreeVector() {
00052   return G4ThreeVector(red,green,blue);
00053 }
00054 
00055 std::ostream& operator << (std::ostream& os, const G4Colour& c) {
00056   os << '(' << c.red << ',' << c.green << ',' << c.blue
00057             << ',' << c.alpha << ')';
00058   const std::map<G4String, G4Colour>& colourMap = G4Colour::GetMap();
00059   // Reverse iterator to pick up English spelling of grey!!  :)
00060   std::map<G4String, G4Colour>::const_reverse_iterator ri;
00061   for (ri = colourMap.rbegin(); ri != colourMap.rend(); ++ri) {
00062     if (c == ri->second) {
00063       os << " (" << ri->first << ')';
00064       break;
00065     }
00066   }
00067   
00068   return os;
00069 }
00070 
00071 G4bool G4Colour::operator != (const G4Colour& c) const {
00072   if (
00073       (red   != c.red)   ||
00074       (green != c.green) ||
00075       (blue  != c.blue)  ||
00076       (alpha != c.alpha)
00077       )
00078     return true;
00079   return false;
00080 }
00081 
00082 std::map<G4String, G4Colour> G4Colour::fColourMap;
00083 bool G4Colour::fInitColourMap = false;
00084 
00085 void
00086 G4Colour::AddToMap(const G4String& key, const G4Colour& colour) 
00087 {
00088   // Convert to lower case since colour map is case insensitive
00089   G4String myKey(key);
00090   myKey.toLower();
00091 
00092   std::map<G4String, G4Colour>::iterator iter = fColourMap.find(myKey);
00093   
00094   if (iter == fColourMap.end()) fColourMap[myKey] = colour;  
00095   else {
00096     G4ExceptionDescription ed; 
00097     ed << "G4Colour with key "<<myKey<<" already exists."<<G4endl;
00098     G4Exception
00099       ("G4Colour::AddToMap(const G4String& key, const G4Colour& colour)",
00100        "greps0001", JustWarning, ed,
00101        "Colour key exists");
00102   }
00103 }
00104 
00105 void
00106 G4Colour::InitialiseColourMap() 
00107 {
00108   // Standard colours
00109   AddToMap("white",   G4Colour::White());
00110   AddToMap("grey",    G4Colour::Grey());
00111   AddToMap("gray",    G4Colour::Gray());
00112   AddToMap("black",   G4Colour::Black());
00113   AddToMap("brown",   G4Colour::Brown());
00114   AddToMap("red",     G4Colour::Red());
00115   AddToMap("green",   G4Colour::Green());
00116   AddToMap("blue",    G4Colour::Blue());
00117   AddToMap("cyan",    G4Colour::Cyan());
00118   AddToMap("magenta", G4Colour::Magenta());
00119   AddToMap("yellow",  G4Colour::Yellow());
00120 }
00121 
00122 bool
00123 G4Colour::GetColour(const G4String& key, G4Colour& result) 
00124 {
00125   if (false == fInitColourMap) {
00126     fInitColourMap = true;
00127     // Add standard colours to map
00128     InitialiseColourMap();
00129   }
00130  
00131   G4String myKey(key);
00132   myKey.toLower();
00133  
00134   std::map<G4String, G4Colour>::iterator iter = fColourMap.find(myKey);
00135 
00136   // Don't modify "result" if colour was not found in map
00137   if (iter == fColourMap.end()) return false;
00138   
00139   result = iter->second;
00140 
00141   return true;
00142 }
00143 
00144 const std::map<G4String, G4Colour>& G4Colour::GetMap()
00145 {
00146   if (false == fInitColourMap) {
00147     fInitColourMap = true;
00148     // Add standard colours to map
00149     InitialiseColourMap();
00150   }
00151  
00152   return fColourMap;
00153 }

Generated on Mon May 27 17:47:56 2013 for Geant4 by  doxygen 1.4.7