llcolor4u.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * @file llcolor4u.cpp
  3. * @brief LLColor4U class implementation.
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "llcolor4u.h"
  34. // LLColor4U
  35. LLColor4U LLColor4U::white(255, 255, 255, 255);
  36. LLColor4U LLColor4U::black(0, 0, 0, 255);
  37. LLColor4U LLColor4U::red(255, 0, 0, 255);
  38. LLColor4U LLColor4U::green(0, 255, 0, 255);
  39. LLColor4U LLColor4U::blue(0, 0, 255, 255);
  40. std::ostream& operator<<(std::ostream& s, const LLColor4U& a)
  41. {
  42. s << "{ " << (S32)a.mV[VX] << ", " << (S32)a.mV[VY] << ", " << (S32)a.mV[VZ]
  43. << ", " << (S32)a.mV[VW] << " }";
  44. return s;
  45. }
  46. //static
  47. bool LLColor4U::parseColor4U(const std::string& buf, LLColor4U* value,
  48. bool strict)
  49. {
  50. if (buf.empty() || value == NULL)
  51. {
  52. return false;
  53. }
  54. U32 v[4];
  55. S32 count = sscanf(buf.c_str(), "%u, %u, %u, %u", v, v + 1, v + 2, v + 3);
  56. if ((count != 4 && strict) || count < 3)
  57. {
  58. // Try this format
  59. count = sscanf(buf.c_str(), "%u %u %u %u", v, v + 1, v + 2, v + 3);
  60. }
  61. if ((count != 4 && strict) || count < 3)
  62. {
  63. return false;
  64. }
  65. if (count == 3)
  66. {
  67. // Default to opaque
  68. v[3] = U8_MAX;
  69. }
  70. for (S32 i = 0; i < 4; ++i)
  71. {
  72. if (v[i] > U8_MAX)
  73. {
  74. return false;
  75. }
  76. }
  77. value->set(U8(v[0]), U8(v[1]), U8(v[2]), U8(v[3]));
  78. return true;
  79. }
  80. void LLColor4U::setVecScaleClamp(const LLColor4& color)
  81. {
  82. F32 color_scale_factor = 255.f;
  83. F32 max_color = llmax(color.mV[0], color.mV[1], color.mV[2]);
  84. if (max_color > 1.f)
  85. {
  86. color_scale_factor /= max_color;
  87. }
  88. constexpr S32 MAX_COLOR = 255;
  89. S32 r = ll_roundp(color.mV[0] * color_scale_factor);
  90. if (r > MAX_COLOR)
  91. {
  92. r = MAX_COLOR;
  93. }
  94. else if (r < 0)
  95. {
  96. r = 0;
  97. }
  98. mV[0] = r;
  99. S32 g = ll_roundp(color.mV[1] * color_scale_factor);
  100. if (g > MAX_COLOR)
  101. {
  102. g = MAX_COLOR;
  103. }
  104. else if (g < 0)
  105. {
  106. g = 0;
  107. }
  108. mV[1] = g;
  109. S32 b = ll_roundp(color.mV[2] * color_scale_factor);
  110. if (b > MAX_COLOR)
  111. {
  112. b = MAX_COLOR;
  113. }
  114. else if (b < 0)
  115. {
  116. b = 0;
  117. }
  118. mV[2] = b;
  119. // Alpha should not be scaled, just clamped...
  120. S32 a = ll_roundp(color.mV[3] * MAX_COLOR);
  121. if (a > MAX_COLOR)
  122. {
  123. a = MAX_COLOR;
  124. }
  125. else if (a < 0)
  126. {
  127. a = 0;
  128. }
  129. mV[3] = a;
  130. }
  131. void LLColor4U::setVecScaleClamp(const LLColor3& color)
  132. {
  133. F32 color_scale_factor = 255.f;
  134. F32 max_color = llmax(color.mV[0], color.mV[1], color.mV[2]);
  135. if (max_color > 1.f)
  136. {
  137. color_scale_factor /= max_color;
  138. }
  139. constexpr S32 MAX_COLOR = 255;
  140. S32 r = ll_roundp(color.mV[0] * color_scale_factor);
  141. if (r > MAX_COLOR)
  142. {
  143. r = MAX_COLOR;
  144. }
  145. else if (r < 0)
  146. {
  147. r = 0;
  148. }
  149. mV[0] = r;
  150. S32 g = ll_roundp(color.mV[1] * color_scale_factor);
  151. if (g > MAX_COLOR)
  152. {
  153. g = MAX_COLOR;
  154. }
  155. else if (g < 0)
  156. {
  157. g = 0;
  158. }
  159. mV[1] = g;
  160. S32 b = ll_roundp(color.mV[2] * color_scale_factor);
  161. if (b > MAX_COLOR)
  162. {
  163. b = MAX_COLOR;
  164. }
  165. if (b < 0)
  166. {
  167. b = 0;
  168. }
  169. mV[2] = b;
  170. mV[3] = 255;
  171. }