00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
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
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
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
00128 InitialiseColourMap();
00129 }
00130
00131 G4String myKey(key);
00132 myKey.toLower();
00133
00134 std::map<G4String, G4Colour>::iterator iter = fColourMap.find(myKey);
00135
00136
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
00149 InitialiseColourMap();
00150 }
00151
00152 return fColourMap;
00153 }