| 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,v 1.11 2007/01/05 14:06:28 allison Exp $
|
|---|
| 28 | // GEANT4 tag $Name: geant4-09-04-beta-01 $
|
|---|
| 29 | //
|
|---|
| 30 | //
|
|---|
| 31 | // John Allison 20th October 1996
|
|---|
| 32 |
|
|---|
| 33 | #include "G4Colour.hh"
|
|---|
| 34 |
|
|---|
| 35 | #include <sstream>
|
|---|
| 36 |
|
|---|
| 37 | G4Colour::G4Colour (G4double r, G4double g, G4double b, G4double a):
|
|---|
| 38 | red (r), green (g), blue (b), alpha (a)
|
|---|
| 39 | {
|
|---|
| 40 | if( red > 1.0 ){red = 1.0;} if( red < 0.0 ){red = 0.0;}
|
|---|
| 41 | if( green > 1.0 ){green = 1.0;} if( green < 0.0 ){green = 0.0;}
|
|---|
| 42 | if( blue > 1.0 ){blue = 1.0;} if( blue < 0.0 ){blue = 0.0;}
|
|---|
| 43 | if( alpha > 1.0 ){alpha = 1.0;} if( alpha < 0.0 ){alpha = 0.0;}
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | G4Colour::G4Colour (G4ThreeVector v):
|
|---|
| 47 | red (v.x()), green (v.y()), blue (v.z()), alpha (1.)
|
|---|
| 48 | {
|
|---|
| 49 | if( red > 1.0 ){red = 1.0;} if( red < 0.0 ){red = 0.0;}
|
|---|
| 50 | if( green > 1.0 ){green = 1.0;} if( green < 0.0 ){green = 0.0;}
|
|---|
| 51 | if( blue > 1.0 ){blue = 1.0;} if( blue < 0.0 ){blue = 0.0;}
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | G4Colour::operator G4ThreeVector() {
|
|---|
| 55 | return G4ThreeVector(red,green,blue);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | std::ostream& operator << (std::ostream& os, const G4Colour& c) {
|
|---|
| 59 | return os << '(' << c.red << ',' << c.green << ',' << c.blue
|
|---|
| 60 | << ',' << c.alpha << ')';
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | G4bool G4Colour::operator != (const G4Colour& c) const {
|
|---|
| 64 | if (
|
|---|
| 65 | (red != c.red) ||
|
|---|
| 66 | (green != c.green) ||
|
|---|
| 67 | (blue != c.blue) ||
|
|---|
| 68 | (alpha != c.alpha)
|
|---|
| 69 | )
|
|---|
| 70 | return true;
|
|---|
| 71 | return false;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | map<G4String, G4Colour> G4Colour::fColourMap;
|
|---|
| 75 | bool G4Colour::fInitColourMap = false;
|
|---|
| 76 |
|
|---|
| 77 | void
|
|---|
| 78 | G4Colour::AddToMap(const G4String& key, const G4Colour& colour)
|
|---|
| 79 | {
|
|---|
| 80 | // Convert to lower case since colour map is case insensitive
|
|---|
| 81 | G4String myKey(key);
|
|---|
| 82 | myKey.toLower();
|
|---|
| 83 |
|
|---|
| 84 | map<G4String, G4Colour>::iterator iter = fColourMap.find(myKey);
|
|---|
| 85 |
|
|---|
| 86 | if (iter == fColourMap.end()) fColourMap[myKey] = colour;
|
|---|
| 87 | else {
|
|---|
| 88 | std::ostringstream o;
|
|---|
| 89 | o << "G4Colour with key "<<myKey<<" already exists ";
|
|---|
| 90 | G4Exception
|
|---|
| 91 | ("G4Colour::AddToMap(const G4String& key, const G4Colour& colour)",
|
|---|
| 92 | "ColourKeyExists", JustWarning, o.str().c_str());
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | void
|
|---|
| 97 | G4Colour::InitialiseColourMap()
|
|---|
| 98 | {
|
|---|
| 99 | // Standard colours
|
|---|
| 100 | AddToMap("white", G4Colour::White());
|
|---|
| 101 | AddToMap("gray", G4Colour::Gray());
|
|---|
| 102 | AddToMap("grey", G4Colour::Grey());
|
|---|
| 103 | AddToMap("black", G4Colour::Black());
|
|---|
| 104 | AddToMap("red", G4Colour::Red());
|
|---|
| 105 | AddToMap("green", G4Colour::Green());
|
|---|
| 106 | AddToMap("blue", G4Colour::Blue());
|
|---|
| 107 | AddToMap("cyan", G4Colour::Cyan());
|
|---|
| 108 | AddToMap("magenta", G4Colour::Magenta());
|
|---|
| 109 | AddToMap("yellow", G4Colour::Yellow());
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | bool
|
|---|
| 113 | G4Colour::GetColour(const G4String& key, G4Colour& result)
|
|---|
| 114 | {
|
|---|
| 115 | if (false == fInitColourMap) {
|
|---|
| 116 | fInitColourMap = true;
|
|---|
| 117 | // Add standard colours to map
|
|---|
| 118 | InitialiseColourMap();
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | G4String myKey(key);
|
|---|
| 122 | myKey.toLower();
|
|---|
| 123 |
|
|---|
| 124 | map<G4String, G4Colour>::iterator iter = fColourMap.find(myKey);
|
|---|
| 125 |
|
|---|
| 126 | // Don't modify "result" if colour was not found in map
|
|---|
| 127 | if (iter == fColourMap.end()) return false;
|
|---|
| 128 |
|
|---|
| 129 | result = iter->second;
|
|---|
| 130 |
|
|---|
| 131 | return true;
|
|---|
| 132 | }
|
|---|