Angel 3.2
A 2D Game Prototyping Engine
Color.cpp
1 
2 // Copyright (C) 2008-2013, Shane Liesegang
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of the copyright holder nor the names of any
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
29 
30 #include "stdafx.h"
31 #include "../Infrastructure/Color.h"
32 
33 #include "../Util/MathUtil.h"
34 #include <cstdio>
35 
37 {
38  //default color is white
39  R = G = B = A = 1.0f;
40 }
41 
42 Color::Color(float r, float g, float b, float a, bool clamp)
43 {
44  R = r;
45  G = g;
46  B = b;
47  A = a; // (Very Ayn Rand of us)
48 
49  if (clamp)
50  {
51  ClampMe();
52  }
53 }
54 
55 Color Color::FromInts(int r, int g, int b, int a, bool clamp)
56 {
57  Color forReturn;
58  forReturn.R = (float)r / 255.0f;
59  forReturn.G = (float)g / 255.0f;
60  forReturn.B = (float)b / 255.0f;
61  forReturn.A = (float)a / 255.0f;
62 
63  if (clamp)
64  {
65  forReturn.ClampMe();
66  }
67 
68  return forReturn;
69 }
70 
71 Color Color::FromHexString(String hexString)
72 {
73  Color forReturn;
74  bool isHex = false;
75  if (hexString[0] == '#')
76  {
77  hexString = hexString.substr(1);
78  isHex = true;
79  }
80  if ( (hexString.substr(0, 2) == "0x") || (hexString.substr(0, 2) == "0X") )
81  {
82  hexString = hexString.substr(2);
83  isHex = true;
84  }
85 
86  if (isHex)
87  {
88  if ( (hexString.length() != 3) && (hexString.length() != 6) )
89  {
90  //invalid length, set to white
91  return Color();
92  }
93 
94  if (hexString.length() == 3)
95  {
96  //expand to 6
97  String newString = "";
98  newString += hexString.substr(0,1) + hexString.substr(0,1);
99  newString += hexString.substr(1,1) + hexString.substr(1,1);
100  newString += hexString.substr(2,1) + hexString.substr(2,1);
101  hexString = newString;
102  }
103 
104  String rString = hexString.substr(0,2);
105  String gString = hexString.substr(2,2);
106  String bString = hexString.substr(4,2);
107 
108  int rInt = HexToInteger(rString);
109  int gInt = HexToInteger(gString);
110  int bInt = HexToInteger(bString);
111 
112  forReturn.R = (float)rInt / 255.0f;
113  forReturn.G = (float)gInt / 255.0f;
114  forReturn.B = (float)bInt / 255.0f;
115  forReturn.A = 1.0f;
116 
117  forReturn.ClampMe();
118  return forReturn;
119  }
120  else
121  {
122  // no support for other string-defined colors -- to white with you
123  return Color();
124  }
125 }
126 
127 int Color::HexToInteger(String hex)
128 {
129  return strtol(hex.c_str(), NULL, 16);
130 }
131 
132 void Color::ClampMe()
133 {
134  R = MathUtil::Clamp(R, 0.0f, 1.0f);
135  G = MathUtil::Clamp(G, 0.0f, 1.0f);
136  B = MathUtil::Clamp(B, 0.0f, 1.0f);
137  A = MathUtil::Clamp(A, 0.0f, 1.0f);
138 }
139 
140 bool Color::operator==(const Color &c) const
141 {
142  return R == c.R && G == c.G && B == c.B && A == c.A;
143 }
144 
145 bool Color::operator!=(const Color &c) const
146 {
147  return !(*this == c);
148 }
149 
150 Color Color::operator-(const Color &c) const
151 {
152  return Color(R - c.R, G - c.G, B - c.B, A - c.A, false);
153 }
154 
155 Color Color::operator+(const Color &c) const
156 {
157  return Color(R + c.R, G + c.G, B + c.B, A + c.A, false);
158 }
159 
160 Color Color::operator/(float divider) const
161 {
162  return Color(R / divider, G / divider, B / divider, A / divider, false);
163 }
164 
165 Color Color::operator*(float scaleFactor) const
166 {
167  return Color(R * scaleFactor, G * scaleFactor, B * scaleFactor, A * scaleFactor, false);
168 }