Rigs of Rods 2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
ColorGradient.cpp
Go to the documentation of this file.
1/*
2--------------------------------------------------------------------------------
3This source file is part of SkyX.
4Visit http://www.paradise-studios.net/products/skyx/
5
6Copyright (C) 2009-2012 Xavier Vergu�n Gonz�lez <xavyiy@gmail.com>
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt.
21--------------------------------------------------------------------------------
22*/
23
24#include "ColorGradient.h"
25
26namespace SkyX
27{
29 : mMalFormed(true)
30 {
31 }
32
36
37 const Ogre::Vector3 ColorGradient::getColor(const Ogre::Real& p) const
38 {
39 if (mMalFormed)
40 {
41 SkyXLOG("Mal-formed ColorGradient");
42 return Ogre::Vector3(0,0,0);
43 }
44
45 if (CFrameVector.size() == 0)
46 {
47 return Ogre::Vector3(0,0,0);
48 }
49 else if (CFrameVector.size() == 1)
50 {
51 return CFrameVector.at(0).first;
52 }
53
54 std::pair<int, Ogre::Real> minBound, maxBound;
55
56 // Min value
57 minBound.first = 0;
58 minBound.second = -1;
59 for (unsigned int k = 0; k < CFrameVector.size(); k++)
60 {
61 if (CFrameVector.at(k).second < p && CFrameVector.at(k).second > minBound.second)
62 {
63 minBound.first = k;
64 minBound.second = CFrameVector.at(k).second;
65 }
66 }
67
68 // Max value
69 maxBound.first = 0;
70 maxBound.second = 2;
71 for (unsigned int k = 0; k < CFrameVector.size(); k++)
72 {
73 if (CFrameVector.at(k).second > p && CFrameVector.at(k).second < maxBound.second)
74 {
75 maxBound.first = k;
76 maxBound.second = CFrameVector.at(k).second;
77 }
78 }
79
80 float range = maxBound.second - minBound.second,
81 rangepoint = (p - minBound.second) / range;
82
83 return CFrameVector.at(minBound.first).first*(1-rangepoint) + CFrameVector.at(maxBound.first).first*rangepoint;
84 }
85
86 const bool ColorGradient::_checkBounds() const
87 {
88 std::pair<bool, bool> existbounds;
89 existbounds.first = false; existbounds.second = false;
90
91 for (unsigned int k = 0; k < CFrameVector.size(); k++)
92 {
93 if (CFrameVector.at(k).second == 0)
94 {
95 // More than one min bound
96 if (existbounds.first)
97 {
98 return false;
99 }
100
101 existbounds.first = true;
102 }
103
104 if (CFrameVector.at(k).second < 0 || CFrameVector.at(k).second > 1)
105 {
106 return false;
107 }
108 }
109
110 for (unsigned int k = 0; k < CFrameVector.size(); k++)
111 {
112 if (CFrameVector.at(k).second == 1)
113 {
114 // More than one min bound
115 if (existbounds.second)
116 {
117 return false;
118 }
119
120 existbounds.second = true;
121 }
122 }
123
124 if (!existbounds.first || !existbounds.second)
125 {
126 return false;
127 }
128
129 return true;
130 }
131}
~ColorGradient()
Destructor.
bool mMalFormed
Mal formed color gradient?
const Ogre::Vector3 getColor(const Ogre::Real &p) const
Get color value.
std::vector< ColorFrame > CFrameVector
Color frame vector.
ColorGradient()
Constructor.
const bool _checkBounds() const
Check bounds.
#define SkyXLOG(msg)
Include external headers.