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 #ifndef COLOR_4F_H_
00026 #define COLOR_4F_H_
00027
00028 #include <Core/System/Math.h>
00029
00030 namespace Lamp{
00031
00032 class Color3c;
00033 class Color4c;
00034 class Color3f;
00035
00036
00037
00038
00039
00040
00041
00042 class Color4f{
00043 public:
00044
00045
00046
00047
00048 union{
00049
00050 struct{
00051
00052 float r;
00053
00054 float g;
00055
00056 float b;
00057
00058 float a;
00059 };
00060
00061
00062 struct{
00063
00064 float h;
00065
00066 float s;
00067
00068 float v;
00069
00070 float a;
00071 };
00072
00073
00074 float array[4];
00075 };
00076
00077
00078
00079
00080
00081 static const Color4f white;
00082
00083
00084 static const Color4f gray;
00085
00086
00087 static const Color4f black;
00088
00089
00090 static const Color4f red;
00091
00092
00093 static const Color4f green;
00094
00095
00096 static const Color4f blue;
00097
00098
00099 static const Color4f yellow;
00100
00101
00102 static const Color4f cyan;
00103
00104
00105 static const Color4f magenta;
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 Color4f(){}
00116
00117
00118
00119
00120
00121
00122
00123
00124 inline Color4f(float sourceR, float sourceG, float sourceB,
00125 float sourceA = 1.f) :
00126 r(sourceR), g(sourceG), b(sourceB), a(sourceA){
00127 }
00128
00129
00130
00131
00132
00133 explicit Color4f(const Color3c& source);
00134
00135
00136
00137
00138
00139 explicit Color4f(const Color4c& source);
00140
00141
00142
00143
00144
00145 explicit Color4f(const Color3f& source);
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 inline void set(float sourceR, float sourceG, float sourceB,
00158 float sourceA = 1.f){
00159 r = sourceR;
00160 g = sourceG;
00161 b = sourceB;
00162 a = sourceA;
00163 }
00164
00165
00166
00167
00168
00169 void set(const Color3c& source);
00170
00171
00172
00173
00174
00175 void set(const Color4c& source);
00176
00177
00178
00179
00180
00181 void set(const Color3f& source);
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 inline Color4f operator +(const Color4f& addColor) const{
00192 return Color4f(
00193 r + addColor.r, g + addColor.g, b + addColor.b, a + addColor.a);
00194 }
00195
00196
00197
00198
00199
00200
00201 inline Color4f operator -(const Color4f& subColor) const{
00202 return Color4f(
00203 r - subColor.r, g - subColor.g, b - subColor.b, a - subColor.a);
00204 }
00205
00206
00207
00208
00209
00210
00211 inline Color4f operator *(const Color4f& mulColor) const{
00212 return Color4f(
00213 r * mulColor.r, g * mulColor.g, b * mulColor.b, a * mulColor.a);
00214 }
00215
00216
00217
00218
00219
00220
00221 inline Color4f operator *(float mulValue) const{
00222 return Color4f(
00223 r * mulValue, g * mulValue, b * mulValue, a * mulValue);
00224 }
00225
00226
00227
00228
00229
00230
00231
00232 inline friend Color4f operator *(float mulValue, const Color4f& mulColor){
00233 return Color4f(
00234 mulColor.r * mulValue, mulColor.g * mulValue,
00235 mulColor.b * mulValue, mulColor.a * mulValue);
00236 }
00237
00238
00239
00240
00241
00242 inline Color4f operator +() const{ return *this; }
00243
00244
00245
00246
00247
00248 inline Color4f operator -() const{ return Color4f(-r, -g, -b, -a); }
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258 inline Color4f& operator +=(const Color4f& addColor){
00259 r += addColor.r;
00260 g += addColor.g;
00261 b += addColor.b;
00262 a += addColor.a;
00263 return (*this);
00264 }
00265
00266
00267
00268
00269
00270
00271 inline Color4f& operator -=(const Color4f& subColor){
00272 r -= subColor.r;
00273 g -= subColor.g;
00274 b -= subColor.b;
00275 a -= subColor.a;
00276 return (*this);
00277 }
00278
00279
00280
00281
00282
00283
00284 inline Color4f& operator *=(const Color4f& mulColor){
00285 r *= mulColor.r;
00286 g *= mulColor.g;
00287 b *= mulColor.b;
00288 a *= mulColor.a;
00289 return (*this);
00290 }
00291
00292
00293
00294
00295
00296
00297 inline Color4f& operator *=(float mulValue){
00298 r *= mulValue;
00299 g *= mulValue;
00300 b *= mulValue;
00301 a *= mulValue;
00302 return (*this);
00303 }
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314 inline Color4f& clamp(float lower = 0.f, float upper = 1.f){
00315 Assert(upper > lower);
00316 if(r > upper){ r = upper; }
00317 else if(r < lower){ r = lower; }
00318 if(g > upper){ g = upper; }
00319 else if(g < lower){ g = lower; }
00320 if(b > upper){ b = upper; }
00321 else if(b < lower){ b = lower; }
00322 if(a > upper){ a = upper; }
00323 else if(a < lower){ a = lower; }
00324 return (*this);
00325 }
00326
00327
00328
00329
00330
00331
00332 inline Color4f& lowerClamp(float lower = 0.f){
00333 if(r < lower){ r = lower; }
00334 if(g < lower){ g = lower; }
00335 if(b < lower){ b = lower; }
00336 if(a < lower){ a = lower; }
00337 return (*this);
00338 }
00339
00340
00341
00342
00343
00344
00345 inline Color4f& upperClamp(float upper = 1.f){
00346 if(r > upper){ r = upper; }
00347 if(g > upper){ g = upper; }
00348 if(b > upper){ b = upper; }
00349 if(a > upper){ a = upper; }
00350 return (*this);
00351 }
00352
00353
00354
00355
00356
00357
00358
00359 inline Color4f& negative(){
00360 set(1.f - r, 1.f - g, 1.f - b, a);
00361 return (*this);
00362 }
00363
00364
00365
00366
00367
00368
00369 inline void setHSV(const Color4f& hsv){
00370 float alpha = hsv.a;
00371 float value = hsv.v;
00372 if(hsv.s <= Math::epsilon){
00373 set(value, value, value, alpha);
00374 return;
00375 }
00376 float hue = hsv.h * 6.f;
00377 float integer = Math::floor(hue);
00378 float decimal = hue - integer;
00379 float temp0 = value * (1.f - hsv.s);
00380 float temp1 = value * (1.f - (hsv.s * decimal));
00381 float temp2 = value * (1.f - (hsv.s * (1.f - decimal)));
00382 if(integer < 1.f){
00383 set(value, temp2, temp0, alpha);
00384 }else if(integer < 2.f){
00385 set(temp1, value, temp0, alpha);
00386 }else if(integer < 3.f){
00387 set(temp0, value, temp2, alpha);
00388 }else if(integer < 4.f){
00389 set(temp0, temp1, value, alpha);
00390 }else if(integer < 5.f){
00391 set(temp2, temp0, value, alpha);
00392 }else{
00393 set(value, temp0, temp1, alpha);
00394 }
00395 }
00396
00397
00398
00399
00400
00401 inline Color4f getHSV() const{
00402 Color4f hsv;
00403 hsv.a = a;
00404
00405 float maximum = Math::maximum(r, Math::maximum(g, b));
00406 hsv.v = maximum;
00407
00408 float minimum = Math::minimum(r, Math::minimum(g, b));
00409 float chroma = maximum - minimum;
00410 if(maximum <= Math::epsilon){
00411 hsv.s = 0.f;
00412 hsv.h = 0.f;
00413 return hsv;
00414 }else{
00415 hsv.s = chroma / maximum;
00416 }
00417 if(chroma <= Math::epsilon){
00418 hsv.h = 0.f;
00419 return hsv;
00420 }
00421 float hueScale = (1 / chroma) * 0.1666667f;
00422 if(r == maximum){
00423 hsv.h = (g - b) * hueScale;
00424 }else if(g == maximum){
00425 hsv.h = (b - r) * hueScale + 0.3333333f;
00426 }else{
00427 hsv.h = (r - g) * hueScale + 0.6666667f;
00428 }
00429 if(hsv.h < 0.f){ hsv.h += 1.f; }
00430 return hsv;
00431 }
00432
00433
00434
00435
00436
00437
00438 inline float getHue() const{
00439 float maximum = Math::maximum(r, Math::maximum(g, b));
00440 if(maximum <= Math::epsilon){ return 0.f; }
00441 float minimum = Math::minimum(r, Math::minimum(g, b));
00442 float chroma = maximum - minimum;
00443 if(chroma <= Math::epsilon){ return 0.f; }
00444 float hueScale = (1 / chroma) * 0.16666667f;
00445 float hue;
00446 if(r == maximum){
00447 hue = (g - b) * hueScale;
00448 }else if(g == maximum){
00449 hue = (b - r) * hueScale + 0.33333333f;
00450 }else{
00451 hue = (r - g) * hueScale + 0.66666667f;
00452 }
00453 if(hue < 0.f){ hue += 1.f; }
00454 }
00455
00456
00457
00458
00459
00460 inline float getSaturation() const{
00461 float maximum = Math::maximum(r, Math::maximum(g, b));
00462 if(maximum <= Math::epsilon){ return 0.f; }
00463 float minimum = Math::minimum(r, Math::minimum(g, b));
00464 float chroma = maximum - minimum;
00465 return chroma / maximum;
00466 }
00467
00468
00469
00470
00471
00472 inline float getValue() const{
00473 return Math::maximum(r, Math::maximum(g, b));
00474 }
00475
00476
00477
00478
00479
00480 inline float getLuminance() const{
00481 return (r * 0.298912f + g * 0.586611f + b * 0.114477f);
00482 }
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492 inline static Color4f lerp(
00493 const Color4f& source, const Color4f& target, float alpha){
00494 float beta = 1.f - alpha;
00495 Color4f result;
00496 result.r = source.r * beta + target.r * alpha;
00497 result.g = source.g * beta + target.g * alpha;
00498 result.b = source.b * beta + target.b * alpha;
00499 result.a = source.a * beta + target.a * alpha;
00500 return result;
00501 }
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511 inline bool operator ==(const Color4f& target) const{
00512 return ((r == target.r) && (g == target.g) &&
00513 (b == target.b) && (a == target.a));
00514 }
00515
00516
00517
00518
00519
00520
00521
00522 inline bool epsilonEquals(const Color4f& target, float epsilon) const{
00523 Assert(epsilon >= 0.f);
00524 return (
00525 (Math::abs(r - target.r) <= epsilon) &&
00526 (Math::abs(g - target.g) <= epsilon) &&
00527 (Math::abs(b - target.b) <= epsilon) &&
00528 (Math::abs(a - target.a) <= epsilon));
00529 }
00530
00531
00532
00533
00534
00535
00536 inline bool operator !=(const Color4f& target) const{
00537 return ((r != target.r) || (g != target.g) ||
00538 (b != target.b) || (a != target.a));
00539 }
00540
00541
00542
00543
00544
00545
00546
00547 inline bool notEpsilonEquals(const Color4f& target, float epsilon) const{
00548 Assert(epsilon >= 0.f);
00549 return (
00550 (Math::abs(r - target.r) > epsilon) ||
00551 (Math::abs(g - target.g) > epsilon) ||
00552 (Math::abs(b - target.b) > epsilon) ||
00553 (Math::abs(a - target.a) > epsilon));
00554 }
00555
00556
00557
00558
00559
00560
00561
00562
00563 inline String toString() const{
00564 String returnString;
00565 returnString.format("( %.8f, %.8f, %.8f, %.8f )", r, g, b, a);
00566 return returnString;
00567 }
00568
00569
00570 private:
00571
00572 };
00573
00574
00575 }
00576 #endif // End of COLOR_4F_H_
00577