00001
00032 #include "linden_common.h"
00033
00034 #include "llboost.h"
00035
00036 #include "v4color.h"
00037 #include "v4coloru.h"
00038 #include "v3color.h"
00039
00040 #include "llmath.h"
00041
00042
00043
00045
00046 LLColor4 LLColor4::red( 1.f, 0.f, 0.f, 1.f);
00047 LLColor4 LLColor4::green( 0.f, 1.f, 0.f, 1.f);
00048 LLColor4 LLColor4::blue( 0.f, 0.f, 1.f, 1.f);
00049 LLColor4 LLColor4::black( 0.f, 0.f, 0.f, 1.f);
00050 LLColor4 LLColor4::yellow( 1.f, 1.f, 0.f, 1.f);
00051 LLColor4 LLColor4::magenta( 1.0f, 0.0f, 1.0f, 1.0f);
00052 LLColor4 LLColor4::cyan( 0.0f, 1.0f, 1.0f, 1.0f);
00053 LLColor4 LLColor4::white( 1.f, 1.f, 1.f, 1.f);
00054 LLColor4 LLColor4::smoke( 0.5f, 0.5f, 0.5f, 0.5f);
00055 LLColor4 LLColor4::grey( 0.5f, 0.5f, 0.5f, 1.0f);
00056 LLColor4 LLColor4::orange( 1.f, 0.5, 0.f, 1.f );
00057 LLColor4 LLColor4::purple( 0.6f, 0.2f, 0.8f, 1.0f);
00058 LLColor4 LLColor4::pink( 1.0f, 0.5f, 0.8f, 1.0f);
00059 LLColor4 LLColor4::transparent( 0.f, 0.f, 0.f, 0.f );
00060
00062
00063 LLColor4 LLColor4::grey1(0.8f, 0.8f, 0.8f, 1.0f);
00064 LLColor4 LLColor4::grey2(0.6f, 0.6f, 0.6f, 1.0f);
00065 LLColor4 LLColor4::grey3(0.4f, 0.4f, 0.4f, 1.0f);
00066 LLColor4 LLColor4::grey4(0.3f, 0.3f, 0.3f, 1.0f);
00067
00068 LLColor4 LLColor4::red1(1.0f, 0.0f, 0.0f, 1.0f);
00069 LLColor4 LLColor4::red2(0.6f, 0.0f, 0.0f, 1.0f);
00070 LLColor4 LLColor4::red3(1.0f, 0.2f, 0.2f, 1.0f);
00071 LLColor4 LLColor4::red4(0.5f, 0.1f, 0.1f, 1.0f);
00072 LLColor4 LLColor4::red5(0.8f, 0.1f, 0.0f, 1.0f);
00073
00074 LLColor4 LLColor4::green1(0.0f, 1.0f, 0.0f, 1.0f);
00075 LLColor4 LLColor4::green2(0.0f, 0.6f, 0.0f, 1.0f);
00076 LLColor4 LLColor4::green3(0.0f, 0.4f, 0.0f, 1.0f);
00077 LLColor4 LLColor4::green4(0.0f, 1.0f, 0.4f, 1.0f);
00078 LLColor4 LLColor4::green5(0.2f, 0.6f, 0.4f, 1.0f);
00079 LLColor4 LLColor4::green6(0.4f, 0.6f, 0.2f, 1.0f);
00080
00081 LLColor4 LLColor4::blue1(0.0f, 0.0f, 1.0f, 1.0f);
00082 LLColor4 LLColor4::blue2(0.0f, 0.4f, 1.0f, 1.0f);
00083 LLColor4 LLColor4::blue3(0.2f, 0.2f, 0.8f, 1.0f);
00084 LLColor4 LLColor4::blue4(0.0f, 0.0f, 0.6f, 1.0f);
00085 LLColor4 LLColor4::blue5(0.4f, 0.2f, 1.0f, 1.0f);
00086 LLColor4 LLColor4::blue6(0.4f, 0.5f, 1.0f, 1.0f);
00087
00088 LLColor4 LLColor4::yellow1(1.0f, 1.0f, 0.0f, 1.0f);
00089 LLColor4 LLColor4::yellow2(0.6f, 0.6f, 0.0f, 1.0f);
00090 LLColor4 LLColor4::yellow3(0.8f, 1.0f, 0.2f, 1.0f);
00091 LLColor4 LLColor4::yellow4(1.0f, 1.0f, 0.4f, 1.0f);
00092 LLColor4 LLColor4::yellow5(0.6f, 0.4f, 0.2f, 1.0f);
00093 LLColor4 LLColor4::yellow6(1.0f, 0.8f, 0.4f, 1.0f);
00094 LLColor4 LLColor4::yellow7(0.8f, 0.8f, 0.0f, 1.0f);
00095 LLColor4 LLColor4::yellow8(0.8f, 0.8f, 0.2f, 1.0f);
00096 LLColor4 LLColor4::yellow9(0.8f, 0.8f, 0.4f, 1.0f);
00097
00098 LLColor4 LLColor4::orange1(1.0f, 0.8f, 0.0f, 1.0f);
00099 LLColor4 LLColor4::orange2(1.0f, 0.6f, 0.0f, 1.0f);
00100 LLColor4 LLColor4::orange3(1.0f, 0.4f, 0.2f, 1.0f);
00101 LLColor4 LLColor4::orange4(0.8f, 0.4f, 0.0f, 1.0f);
00102 LLColor4 LLColor4::orange5(0.9f, 0.5f, 0.0f, 1.0f);
00103 LLColor4 LLColor4::orange6(1.0f, 0.8f, 0.2f, 1.0f);
00104
00105 LLColor4 LLColor4::magenta1(1.0f, 0.0f, 1.0f, 1.0f);
00106 LLColor4 LLColor4::magenta2(0.6f, 0.2f, 0.4f, 1.0f);
00107 LLColor4 LLColor4::magenta3(1.0f, 0.4f, 0.6f, 1.0f);
00108 LLColor4 LLColor4::magenta4(1.0f, 0.2f, 0.8f, 1.0f);
00109
00110 LLColor4 LLColor4::purple1(0.6f, 0.2f, 0.8f, 1.0f);
00111 LLColor4 LLColor4::purple2(0.8f, 0.2f, 1.0f, 1.0f);
00112 LLColor4 LLColor4::purple3(0.6f, 0.0f, 1.0f, 1.0f);
00113 LLColor4 LLColor4::purple4(0.4f, 0.0f, 0.8f, 1.0f);
00114 LLColor4 LLColor4::purple5(0.6f, 0.0f, 0.8f, 1.0f);
00115 LLColor4 LLColor4::purple6(0.8f, 0.0f, 0.6f, 1.0f);
00116
00117 LLColor4 LLColor4::pink1(1.0f, 0.5f, 0.8f, 1.0f);
00118 LLColor4 LLColor4::pink2(1.0f, 0.8f, 0.9f, 1.0f);
00119
00120 LLColor4 LLColor4::cyan1(0.0f, 1.0f, 1.0f, 1.0f);
00121 LLColor4 LLColor4::cyan2(0.4f, 0.8f, 0.8f, 1.0f);
00122 LLColor4 LLColor4::cyan3(0.0f, 1.0f, 0.6f, 1.0f);
00123 LLColor4 LLColor4::cyan4(0.6f, 1.0f, 1.0f, 1.0f);
00124 LLColor4 LLColor4::cyan5(0.2f, 0.6f, 1.0f, 1.0f);
00125 LLColor4 LLColor4::cyan6(0.2f, 0.6f, 0.6f, 1.0f);
00126
00128
00129
00130 LLColor4::operator const LLColor4U() const
00131 {
00132 return LLColor4U(
00133 (U8)llclampb(llround(mV[VRED]*255.f)),
00134 (U8)llclampb(llround(mV[VGREEN]*255.f)),
00135 (U8)llclampb(llround(mV[VBLUE]*255.f)),
00136 (U8)llclampb(llround(mV[VALPHA]*255.f)));
00137 }
00138
00139 LLColor4::LLColor4(const LLColor3 &vec, F32 a)
00140 {
00141 mV[VX] = vec.mV[VX];
00142 mV[VY] = vec.mV[VY];
00143 mV[VZ] = vec.mV[VZ];
00144 mV[VW] = a;
00145 }
00146
00147 LLColor4::LLColor4(const LLColor4U& color4u)
00148 {
00149 const F32 SCALE = 1.f/255.f;
00150 mV[VX] = color4u.mV[VX] * SCALE;
00151 mV[VY] = color4u.mV[VY] * SCALE;
00152 mV[VZ] = color4u.mV[VZ] * SCALE;
00153 mV[VW] = color4u.mV[VW] * SCALE;
00154 }
00155
00156 const LLColor4& LLColor4::setVec(const LLColor4U& color4u)
00157 {
00158 const F32 SCALE = 1.f/255.f;
00159 mV[VX] = color4u.mV[VX] * SCALE;
00160 mV[VY] = color4u.mV[VY] * SCALE;
00161 mV[VZ] = color4u.mV[VZ] * SCALE;
00162 mV[VW] = color4u.mV[VW] * SCALE;
00163 return (*this);
00164 }
00165
00166 const LLColor4& LLColor4::setVec(const LLColor3 &vec)
00167 {
00168 mV[VX] = vec.mV[VX];
00169 mV[VY] = vec.mV[VY];
00170 mV[VZ] = vec.mV[VZ];
00171
00172
00173
00174
00175 return (*this);
00176 }
00177
00178 const LLColor4& LLColor4::setVec(const LLColor3 &vec, F32 a)
00179 {
00180 mV[VX] = vec.mV[VX];
00181 mV[VY] = vec.mV[VY];
00182 mV[VZ] = vec.mV[VZ];
00183 mV[VW] = a;
00184 return (*this);
00185 }
00186
00187 const LLColor4& LLColor4::operator=(const LLColor3 &a)
00188 {
00189 mV[VX] = a.mV[VX];
00190 mV[VY] = a.mV[VY];
00191 mV[VZ] = a.mV[VZ];
00192
00193
00194 mV[VW] = 1.f;
00195 return (*this);
00196 }
00197
00198
00199 std::ostream& operator<<(std::ostream& s, const LLColor4 &a)
00200 {
00201 s << "{ " << a.mV[VX] << ", " << a.mV[VY] << ", " << a.mV[VZ] << ", " << a.mV[VW] << " }";
00202 return s;
00203 }
00204
00205 bool operator==(const LLColor4 &a, const LLColor3 &b)
00206 {
00207 return ( (a.mV[VX] == b.mV[VX])
00208 &&(a.mV[VY] == b.mV[VY])
00209 &&(a.mV[VZ] == b.mV[VZ]));
00210 }
00211
00212 bool operator!=(const LLColor4 &a, const LLColor3 &b)
00213 {
00214 return ( (a.mV[VX] != b.mV[VX])
00215 ||(a.mV[VY] != b.mV[VY])
00216 ||(a.mV[VZ] != b.mV[VZ]));
00217 }
00218
00219 LLColor3 vec4to3(const LLColor4 &vec)
00220 {
00221 LLColor3 temp(vec.mV[VX], vec.mV[VY], vec.mV[VZ]);
00222 return temp;
00223 }
00224
00225 LLColor4 vec3to4(const LLColor3 &vec)
00226 {
00227 LLColor3 temp(vec.mV[VX], vec.mV[VY], vec.mV[VZ]);
00228 return temp;
00229 }
00230
00231 void LLColor4::calcHSL(F32* hue, F32* saturation, F32* luminance) const
00232 {
00233 F32 var_R = mV[VRED];
00234 F32 var_G = mV[VGREEN];
00235 F32 var_B = mV[VBLUE];
00236
00237 F32 var_Min = ( var_R < ( var_G < var_B ? var_G : var_B ) ? var_R : ( var_G < var_B ? var_G : var_B ) );
00238 F32 var_Max = ( var_R > ( var_G > var_B ? var_G : var_B ) ? var_R : ( var_G > var_B ? var_G : var_B ) );
00239
00240 F32 del_Max = var_Max - var_Min;
00241
00242 F32 L = ( var_Max + var_Min ) / 2.0f;
00243 F32 H = 0.0f;
00244 F32 S = 0.0f;
00245
00246 if ( del_Max == 0.0f )
00247 {
00248 H = 0.0f;
00249 S = 0.0f;
00250 }
00251 else
00252 {
00253 if ( L < 0.5 )
00254 S = del_Max / ( var_Max + var_Min );
00255 else
00256 S = del_Max / ( 2.0f - var_Max - var_Min );
00257
00258 F32 del_R = ( ( ( var_Max - var_R ) / 6.0f ) + ( del_Max / 2.0f ) ) / del_Max;
00259 F32 del_G = ( ( ( var_Max - var_G ) / 6.0f ) + ( del_Max / 2.0f ) ) / del_Max;
00260 F32 del_B = ( ( ( var_Max - var_B ) / 6.0f ) + ( del_Max / 2.0f ) ) / del_Max;
00261
00262 if ( var_R >= var_Max )
00263 H = del_B - del_G;
00264 else
00265 if ( var_G >= var_Max )
00266 H = ( 1.0f / 3.0f ) + del_R - del_B;
00267 else
00268 if ( var_B >= var_Max )
00269 H = ( 2.0f / 3.0f ) + del_G - del_R;
00270
00271 if ( H < 0.0f ) H += 1.0f;
00272 if ( H > 1.0f ) H -= 1.0f;
00273 }
00274
00275 if (hue) *hue = H;
00276 if (saturation) *saturation = S;
00277 if (luminance) *luminance = L;
00278 }
00279
00280
00281 BOOL LLColor4::parseColor(const char* buf, LLColor4* color)
00282 {
00283 if( buf == NULL || buf[0] == '\0' || color == NULL)
00284 {
00285 return FALSE;
00286 }
00287
00288 LLString full_string(buf);
00289
00290 boost_tokenizer tokens(full_string, boost::char_separator<char>(", "));
00291 boost_tokenizer::iterator token_iter = tokens.begin();
00292 if (token_iter == tokens.end())
00293 {
00294 return FALSE;
00295 }
00296
00297
00298
00299 LLString color_name( (*token_iter) );
00300 ++token_iter;
00301
00302 if (token_iter != tokens.end())
00303 {
00304
00305 LLColor4 v;
00306 LLString::convertToF32( color_name, v.mV[VX] );
00307 LLString::convertToF32( *token_iter, v.mV[VY] );
00308 v.mV[VZ] = 0.0f;
00309 v.mV[VW] = 1.0f;
00310
00311 ++token_iter;
00312 if (token_iter == tokens.end())
00313 {
00314
00315 llwarns << "LLColor4::parseColor() malformed color " << full_string << llendl;
00316 }
00317 else
00318 {
00319
00320 LLString::convertToF32( *token_iter, v.mV[VZ] );
00321
00322 ++token_iter;
00323 if (token_iter != tokens.end())
00324 {
00325
00326 LLString::convertToF32( *token_iter, v.mV[VW] );
00327 }
00328 }
00329
00330
00331 if (v.mV[VX] > 1.f || v.mV[VY] > 1.f || v.mV[VZ] > 1.f || v.mV[VW] > 1.f)
00332 {
00333 v = v * (1.f / 255.f);
00334 }
00335 color->setVec( v );
00336 }
00337 else
00338 {
00339
00340 if ( "red" == color_name )
00341 {
00342 color->setVec(LLColor4::red);
00343 }
00344 else if ( "red1" == color_name )
00345 {
00346 color->setVec(LLColor4::red1);
00347 }
00348 else if ( "red2" == color_name )
00349 {
00350 color->setVec(LLColor4::red2);
00351 }
00352 else if ( "red3" == color_name )
00353 {
00354 color->setVec(LLColor4::red3);
00355 }
00356 else if ( "red4" == color_name )
00357 {
00358 color->setVec(LLColor4::red4);
00359 }
00360 else if ( "red5" == color_name )
00361 {
00362 color->setVec(LLColor4::red5);
00363 }
00364 else if( "green" == color_name )
00365 {
00366 color->setVec(LLColor4::green);
00367 }
00368 else if( "green1" == color_name )
00369 {
00370 color->setVec(LLColor4::green1);
00371 }
00372 else if( "green2" == color_name )
00373 {
00374 color->setVec(LLColor4::green2);
00375 }
00376 else if( "green3" == color_name )
00377 {
00378 color->setVec(LLColor4::green3);
00379 }
00380 else if( "green4" == color_name )
00381 {
00382 color->setVec(LLColor4::green4);
00383 }
00384 else if( "green5" == color_name )
00385 {
00386 color->setVec(LLColor4::green5);
00387 }
00388 else if( "green6" == color_name )
00389 {
00390 color->setVec(LLColor4::green6);
00391 }
00392 else if( "blue" == color_name )
00393 {
00394 color->setVec(LLColor4::blue);
00395 }
00396 else if( "blue1" == color_name )
00397 {
00398 color->setVec(LLColor4::blue1);
00399 }
00400 else if( "blue2" == color_name )
00401 {
00402 color->setVec(LLColor4::blue2);
00403 }
00404 else if( "blue3" == color_name )
00405 {
00406 color->setVec(LLColor4::blue3);
00407 }
00408 else if( "blue4" == color_name )
00409 {
00410 color->setVec(LLColor4::blue4);
00411 }
00412 else if( "blue5" == color_name )
00413 {
00414 color->setVec(LLColor4::blue5);
00415 }
00416 else if( "blue6" == color_name )
00417 {
00418 color->setVec(LLColor4::blue6);
00419 }
00420 else if( "black" == color_name )
00421 {
00422 color->setVec(LLColor4::black);
00423 }
00424 else if( "white" == color_name )
00425 {
00426 color->setVec(LLColor4::white);
00427 }
00428 else if( "yellow" == color_name )
00429 {
00430 color->setVec(LLColor4::yellow);
00431 }
00432 else if( "yellow1" == color_name )
00433 {
00434 color->setVec(LLColor4::yellow1);
00435 }
00436 else if( "yellow2" == color_name )
00437 {
00438 color->setVec(LLColor4::yellow2);
00439 }
00440 else if( "yellow3" == color_name )
00441 {
00442 color->setVec(LLColor4::yellow3);
00443 }
00444 else if( "yellow4" == color_name )
00445 {
00446 color->setVec(LLColor4::yellow4);
00447 }
00448 else if( "yellow5" == color_name )
00449 {
00450 color->setVec(LLColor4::yellow5);
00451 }
00452 else if( "yellow6" == color_name )
00453 {
00454 color->setVec(LLColor4::yellow6);
00455 }
00456 else if( "magenta" == color_name )
00457 {
00458 color->setVec(LLColor4::magenta);
00459 }
00460 else if( "magenta1" == color_name )
00461 {
00462 color->setVec(LLColor4::magenta1);
00463 }
00464 else if( "magenta2" == color_name )
00465 {
00466 color->setVec(LLColor4::magenta2);
00467 }
00468 else if( "magenta3" == color_name )
00469 {
00470 color->setVec(LLColor4::magenta3);
00471 }
00472 else if( "magenta4" == color_name )
00473 {
00474 color->setVec(LLColor4::magenta4);
00475 }
00476 else if( "purple" == color_name )
00477 {
00478 color->setVec(LLColor4::purple);
00479 }
00480 else if( "purple1" == color_name )
00481 {
00482 color->setVec(LLColor4::purple1);
00483 }
00484 else if( "purple2" == color_name )
00485 {
00486 color->setVec(LLColor4::purple2);
00487 }
00488 else if( "purple3" == color_name )
00489 {
00490 color->setVec(LLColor4::purple3);
00491 }
00492 else if( "purple4" == color_name )
00493 {
00494 color->setVec(LLColor4::purple4);
00495 }
00496 else if( "purple5" == color_name )
00497 {
00498 color->setVec(LLColor4::purple5);
00499 }
00500 else if( "purple6" == color_name )
00501 {
00502 color->setVec(LLColor4::purple6);
00503 }
00504 else if( "pink" == color_name )
00505 {
00506 color->setVec(LLColor4::pink);
00507 }
00508 else if( "pink1" == color_name )
00509 {
00510 color->setVec(LLColor4::pink1);
00511 }
00512 else if( "pink2" == color_name )
00513 {
00514 color->setVec(LLColor4::pink2);
00515 }
00516 else if( "cyan" == color_name )
00517 {
00518 color->setVec(LLColor4::cyan);
00519 }
00520 else if( "cyan1" == color_name )
00521 {
00522 color->setVec(LLColor4::cyan1);
00523 }
00524 else if( "cyan2" == color_name )
00525 {
00526 color->setVec(LLColor4::cyan2);
00527 }
00528 else if( "cyan3" == color_name )
00529 {
00530 color->setVec(LLColor4::cyan3);
00531 }
00532 else if( "cyan4" == color_name )
00533 {
00534 color->setVec(LLColor4::cyan4);
00535 }
00536 else if( "cyan5" == color_name )
00537 {
00538 color->setVec(LLColor4::cyan5);
00539 }
00540 else if( "cyan6" == color_name )
00541 {
00542 color->setVec(LLColor4::cyan6);
00543 }
00544 else if( "smoke" == color_name )
00545 {
00546 color->setVec(LLColor4::smoke);
00547 }
00548 else if( "grey" == color_name )
00549 {
00550 color->setVec(LLColor4::grey);
00551 }
00552 else if( "grey1" == color_name )
00553 {
00554 color->setVec(LLColor4::grey1);
00555 }
00556 else if( "grey2" == color_name )
00557 {
00558 color->setVec(LLColor4::grey2);
00559 }
00560 else if( "grey3" == color_name )
00561 {
00562 color->setVec(LLColor4::grey3);
00563 }
00564 else if( "grey4" == color_name )
00565 {
00566 color->setVec(LLColor4::grey4);
00567 }
00568 else if( "orange" == color_name )
00569 {
00570 color->setVec(LLColor4::orange);
00571 }
00572 else if( "orange1" == color_name )
00573 {
00574 color->setVec(LLColor4::orange1);
00575 }
00576 else if( "orange2" == color_name )
00577 {
00578 color->setVec(LLColor4::orange2);
00579 }
00580 else if( "orange3" == color_name )
00581 {
00582 color->setVec(LLColor4::orange3);
00583 }
00584 else if( "orange4" == color_name )
00585 {
00586 color->setVec(LLColor4::orange4);
00587 }
00588 else if( "orange5" == color_name )
00589 {
00590 color->setVec(LLColor4::orange5);
00591 }
00592 else if( "orange6" == color_name )
00593 {
00594 color->setVec(LLColor4::orange6);
00595 }
00596 else if ( "clear" == color_name )
00597 {
00598 color->setVec(0.f, 0.f, 0.f, 0.f);
00599 }
00600 else
00601 {
00602 llwarns << "invalid color " << color_name << llendl;
00603 }
00604 }
00605
00606 return TRUE;
00607 }
00608
00609
00610 BOOL LLColor4::parseColor4(const char* buf, LLColor4* value)
00611 {
00612 if( buf == NULL || buf[0] == '\0' || value == NULL)
00613 {
00614 return FALSE;
00615 }
00616
00617 LLColor4 v;
00618 S32 count = sscanf( buf, "%f, %f, %f, %f", v.mV + 0, v.mV + 1, v.mV + 2, v.mV + 3 );
00619 if (1 == count )
00620 {
00621
00622 count = sscanf( buf, "%f %f %f %f", v.mV + 0, v.mV + 1, v.mV + 2, v.mV + 3 );
00623 }
00624 if( 4 == count )
00625 {
00626 value->setVec( v );
00627 return TRUE;
00628 }
00629
00630 return FALSE;
00631 }
00632
00633