OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WColor.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <cmath>
26 #include <string>
27 #include <vector>
28 
29 #include <boost/lexical_cast.hpp>
30 
31 #include "../common/exceptions/WOutOfBounds.h"
32 #include "../common/WStringUtils.h"
33 #include "WColor.h"
34 
35 // This function is taken from VTK 5.4.2. Since its BSD licensed the license
36 // notice follows below. It is not taken from FAnToM since it seems more self
37 // documenting.
38 //
39 // /*=========================================================================
40 //
41 // Program: Visualization Toolkit
42 // Module: $RCSfile: vtkMath.cxx,v $
43 //
44 // Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
45 // All rights reserved.
46 // See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
47 //
48 // This software is distributed WITHOUT ANY WARRANTY; without even
49 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
50 // PURPOSE. See the above copyright notice for more information.
51 //
52 // =========================================================================
53 // Copyright 2005 Sandia Corporation.
54 // Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
55 // license for use of this work by or on behalf of the
56 // U.S. Government. Redistribution and use in source and binary forms, with
57 // or without modification, are permitted provided that this Notice and any
58 // statement of authorship are reproduced on all copies.
59 //
60 // Contact: pppebay@sandia.gov,dcthomp@sandia.gov,
61 //
62 // =========================================================================*/
63 WColor convertHSVtoRGBA( double h, double s, double v )
64 {
65  const double onethird = 1.0 / 3.0;
66  const double onesixth = 1.0 / 6.0;
67  const double twothird = 2.0 / 3.0;
68  const double fivesixth = 5.0 / 6.0;
69  double r = 0.0;
70  double g = 0.0;
71  double b = 0.0;
72 
73  // compute RGB from HSV
74  if( h > onesixth && h <= onethird ) // green/red
75  {
76  g = 1.0;
77  r = ( onethird - h ) / onesixth;
78  b = 0.0;
79  }
80  else if( h > onethird && h <= 0.5 ) // green/blue
81  {
82  g = 1.0;
83  b = ( h - onethird ) / onesixth;
84  r = 0.0;
85  }
86  else if( h > 0.5 && h <= twothird ) // blue/green
87  {
88  b = 1.0;
89  g = ( twothird - h ) / onesixth;
90  r = 0.0;
91  }
92  else if( h > twothird && h <= fivesixth ) // blue/red
93  {
94  b = 1.0;
95  r = ( h - twothird ) / onesixth;
96  g = 0.0;
97  }
98  else if( h > fivesixth && h <= 1.0) // red/blue
99  {
100  r = 1.0;
101  b = ( 1.0 - h ) / onesixth;
102  g = 0.0;
103  }
104  else // red/green
105  {
106  r = 1.0;
107  g = h / onesixth;
108  b = 0.0;
109  }
110 
111  // add Saturation to the equation.
112  r = ( s * r + ( 1.0 - s ) ) * v;
113  g = ( s * g + ( 1.0 - s ) ) * v;
114  b = ( s * b + ( 1.0 - s ) ) * v;
115 
116  return WColor( r, g, b, 1.0f );
117 }
118 
119 WColor inverseColor( const WColor& other )
120 {
121  return WColor( std::abs( 1.0f - other[0] ), std::abs( 1.0f - other[1] ), std::abs( 1.0f - other[2] ), other[3] );
122 }