PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmath/v3color.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 153 lines | 106 code | 22 blank | 25 comment | 18 complexity | faa42b2831c08883ec35b8e36593b688 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file v3color.cpp
  3. * @brief LLColor3 class implementation.
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "v3color.h"
  28. #include "v4color.h"
  29. #include "v4math.h"
  30. LLColor3 LLColor3::white(1.0f, 1.0f, 1.0f);
  31. LLColor3 LLColor3::black(0.0f, 0.0f, 0.0f);
  32. LLColor3 LLColor3::grey (0.5f, 0.5f, 0.5f);
  33. LLColor3::LLColor3(const LLColor4 &a)
  34. {
  35. mV[0] = a.mV[0];
  36. mV[1] = a.mV[1];
  37. mV[2] = a.mV[2];
  38. }
  39. LLColor3::LLColor3(const LLVector4 &a)
  40. {
  41. mV[0] = a.mV[0];
  42. mV[1] = a.mV[1];
  43. mV[2] = a.mV[2];
  44. }
  45. LLColor3::LLColor3(const LLSD &sd)
  46. {
  47. setValue(sd);
  48. }
  49. const LLColor3& LLColor3::operator=(const LLColor4 &a)
  50. {
  51. mV[0] = a.mV[0];
  52. mV[1] = a.mV[1];
  53. mV[2] = a.mV[2];
  54. return (*this);
  55. }
  56. std::ostream& operator<<(std::ostream& s, const LLColor3 &a)
  57. {
  58. s << "{ " << a.mV[VX] << ", " << a.mV[VY] << ", " << a.mV[VZ] << " }";
  59. return s;
  60. }
  61. static F32 hueToRgb ( F32 val1In, F32 val2In, F32 valHUeIn )
  62. {
  63. if ( valHUeIn < 0.0f ) valHUeIn += 1.0f;
  64. if ( valHUeIn > 1.0f ) valHUeIn -= 1.0f;
  65. if ( ( 6.0f * valHUeIn ) < 1.0f ) return ( val1In + ( val2In - val1In ) * 6.0f * valHUeIn );
  66. if ( ( 2.0f * valHUeIn ) < 1.0f ) return ( val2In );
  67. if ( ( 3.0f * valHUeIn ) < 2.0f ) return ( val1In + ( val2In - val1In ) * ( ( 2.0f / 3.0f ) - valHUeIn ) * 6.0f );
  68. return ( val1In );
  69. }
  70. void LLColor3::setHSL ( F32 hValIn, F32 sValIn, F32 lValIn)
  71. {
  72. if ( sValIn < 0.00001f )
  73. {
  74. mV[VRED] = lValIn;
  75. mV[VGREEN] = lValIn;
  76. mV[VBLUE] = lValIn;
  77. }
  78. else
  79. {
  80. F32 interVal1;
  81. F32 interVal2;
  82. if ( lValIn < 0.5f )
  83. interVal2 = lValIn * ( 1.0f + sValIn );
  84. else
  85. interVal2 = ( lValIn + sValIn ) - ( sValIn * lValIn );
  86. interVal1 = 2.0f * lValIn - interVal2;
  87. mV[VRED] = hueToRgb ( interVal1, interVal2, hValIn + ( 1.f / 3.f ) );
  88. mV[VGREEN] = hueToRgb ( interVal1, interVal2, hValIn );
  89. mV[VBLUE] = hueToRgb ( interVal1, interVal2, hValIn - ( 1.f / 3.f ) );
  90. }
  91. }
  92. void LLColor3::calcHSL(F32* hue, F32* saturation, F32* luminance) const
  93. {
  94. F32 var_R = mV[VRED];
  95. F32 var_G = mV[VGREEN];
  96. F32 var_B = mV[VBLUE];
  97. F32 var_Min = ( var_R < ( var_G < var_B ? var_G : var_B ) ? var_R : ( var_G < var_B ? var_G : var_B ) );
  98. F32 var_Max = ( var_R > ( var_G > var_B ? var_G : var_B ) ? var_R : ( var_G > var_B ? var_G : var_B ) );
  99. F32 del_Max = var_Max - var_Min;
  100. F32 L = ( var_Max + var_Min ) / 2.0f;
  101. F32 H = 0.0f;
  102. F32 S = 0.0f;
  103. if ( del_Max == 0.0f )
  104. {
  105. H = 0.0f;
  106. S = 0.0f;
  107. }
  108. else
  109. {
  110. if ( L < 0.5 )
  111. S = del_Max / ( var_Max + var_Min );
  112. else
  113. S = del_Max / ( 2.0f - var_Max - var_Min );
  114. F32 del_R = ( ( ( var_Max - var_R ) / 6.0f ) + ( del_Max / 2.0f ) ) / del_Max;
  115. F32 del_G = ( ( ( var_Max - var_G ) / 6.0f ) + ( del_Max / 2.0f ) ) / del_Max;
  116. F32 del_B = ( ( ( var_Max - var_B ) / 6.0f ) + ( del_Max / 2.0f ) ) / del_Max;
  117. if ( var_R >= var_Max )
  118. H = del_B - del_G;
  119. else
  120. if ( var_G >= var_Max )
  121. H = ( 1.0f / 3.0f ) + del_R - del_B;
  122. else
  123. if ( var_B >= var_Max )
  124. H = ( 2.0f / 3.0f ) + del_G - del_R;
  125. if ( H < 0.0f ) H += 1.0f;
  126. if ( H > 1.0f ) H -= 1.0f;
  127. }
  128. if (hue) *hue = H;
  129. if (saturation) *saturation = S;
  130. if (luminance) *luminance = L;
  131. }