Color.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Math/Color.h"
  24. #include <cstdio>
  25. #include "../DebugNew.h"
  26. namespace Urho3D
  27. {
  28. unsigned Color::ToUInt() const
  29. {
  30. auto r = (unsigned)Clamp(((int)(r_ * 255.0f)), 0, 255);
  31. auto g = (unsigned)Clamp(((int)(g_ * 255.0f)), 0, 255);
  32. auto b = (unsigned)Clamp(((int)(b_ * 255.0f)), 0, 255);
  33. auto a = (unsigned)Clamp(((int)(a_ * 255.0f)), 0, 255);
  34. return (a << 24u) | (b << 16u) | (g << 8u) | r;
  35. }
  36. unsigned Color::ToUIntMask(const ChannelMask& mask) const
  37. {
  38. const auto max = static_cast<double>(M_MAX_UNSIGNED);
  39. const auto r = static_cast<unsigned>(Clamp(static_cast<double>(r_) * mask.r_, 0.0, max)) & mask.r_;
  40. const auto g = static_cast<unsigned>(Clamp(static_cast<double>(g_) * mask.g_, 0.0, max)) & mask.g_;
  41. const auto b = static_cast<unsigned>(Clamp(static_cast<double>(b_) * mask.b_, 0.0, max)) & mask.b_;
  42. const auto a = static_cast<unsigned>(Clamp(static_cast<double>(a_) * mask.a_, 0.0, max)) & mask.a_;
  43. return r | g | b | a;
  44. }
  45. Vector3 Color::ToHSL() const
  46. {
  47. float min, max;
  48. Bounds(&min, &max, true);
  49. float h = Hue(min, max);
  50. float s = SaturationHSL(min, max);
  51. float l = (max + min) * 0.5f;
  52. return Vector3(h, s, l);
  53. }
  54. Vector3 Color::ToHSV() const
  55. {
  56. float min, max;
  57. Bounds(&min, &max, true);
  58. float h = Hue(min, max);
  59. float s = SaturationHSV(min, max);
  60. float v = max;
  61. return Vector3(h, s, v);
  62. }
  63. void Color::FromUInt(unsigned color)
  64. {
  65. a_ = ((color >> 24u) & 0xffu) / 255.0f;
  66. b_ = ((color >> 16u) & 0xffu) / 255.0f;
  67. g_ = ((color >> 8u) & 0xffu) / 255.0f;
  68. r_ = ((color >> 0u) & 0xffu) / 255.0f;
  69. }
  70. void Color::FromUIntMask(unsigned color, const ChannelMask& mask)
  71. {
  72. // Channel offset is irrelevant during division, but double should be used to avoid precision loss.
  73. r_ = !mask.r_ ? 0.0f : static_cast<float>((color & mask.r_) / static_cast<double>(mask.r_));
  74. g_ = !mask.g_ ? 0.0f : static_cast<float>((color & mask.g_) / static_cast<double>(mask.g_));
  75. b_ = !mask.b_ ? 0.0f : static_cast<float>((color & mask.b_) / static_cast<double>(mask.b_));
  76. a_ = !mask.a_ ? 1.0f : static_cast<float>((color & mask.a_) / static_cast<double>(mask.a_));
  77. }
  78. void Color::FromHSL(float h, float s, float l, float a)
  79. {
  80. float c;
  81. if (l < 0.5f)
  82. c = (1.0f + (2.0f * l - 1.0f)) * s;
  83. else
  84. c = (1.0f - (2.0f * l - 1.0f)) * s;
  85. float m = l - 0.5f * c;
  86. FromHCM(h, c, m);
  87. a_ = a;
  88. }
  89. void Color::FromHSV(float h, float s, float v, float a)
  90. {
  91. float c = v * s;
  92. float m = v - c;
  93. FromHCM(h, c, m);
  94. a_ = a;
  95. }
  96. float Color::Chroma() const
  97. {
  98. float min, max;
  99. Bounds(&min, &max, true);
  100. return max - min;
  101. }
  102. float Color::Hue() const
  103. {
  104. float min, max;
  105. Bounds(&min, &max, true);
  106. return Hue(min, max);
  107. }
  108. float Color::SaturationHSL() const
  109. {
  110. float min, max;
  111. Bounds(&min, &max, true);
  112. return SaturationHSL(min, max);
  113. }
  114. float Color::SaturationHSV() const
  115. {
  116. float min, max;
  117. Bounds(&min, &max, true);
  118. return SaturationHSV(min, max);
  119. }
  120. float Color::Lightness() const
  121. {
  122. float min, max;
  123. Bounds(&min, &max, true);
  124. return (max + min) * 0.5f;
  125. }
  126. void Color::Bounds(float* min, float* max, bool clipped) const
  127. {
  128. assert(min && max);
  129. if (r_ > g_)
  130. {
  131. if (g_ > b_) // r > g > b
  132. {
  133. *max = r_;
  134. *min = b_;
  135. }
  136. else // r > g && g <= b
  137. {
  138. *max = r_ > b_ ? r_ : b_;
  139. *min = g_;
  140. }
  141. }
  142. else
  143. {
  144. if (b_ > g_) // r <= g < b
  145. {
  146. *max = b_;
  147. *min = r_;
  148. }
  149. else // r <= g && b <= g
  150. {
  151. *max = g_;
  152. *min = r_ < b_ ? r_ : b_;
  153. }
  154. }
  155. if (clipped)
  156. {
  157. *max = *max > 1.0f ? 1.0f : (*max < 0.0f ? 0.0f : *max);
  158. *min = *min > 1.0f ? 1.0f : (*min < 0.0f ? 0.0f : *min);
  159. }
  160. }
  161. float Color::MaxRGB() const
  162. {
  163. if (r_ > g_)
  164. return (r_ > b_) ? r_ : b_;
  165. else
  166. return (g_ > b_) ? g_ : b_;
  167. }
  168. float Color::MinRGB() const
  169. {
  170. if (r_ < g_)
  171. return (r_ < b_) ? r_ : b_;
  172. else
  173. return (g_ < b_) ? g_ : b_;
  174. }
  175. float Color::Range() const
  176. {
  177. float min, max;
  178. Bounds(&min, &max);
  179. return max - min;
  180. }
  181. void Color::Clip(bool clipAlpha)
  182. {
  183. r_ = (r_ > 1.0f) ? 1.0f : ((r_ < 0.0f) ? 0.0f : r_);
  184. g_ = (g_ > 1.0f) ? 1.0f : ((g_ < 0.0f) ? 0.0f : g_);
  185. b_ = (b_ > 1.0f) ? 1.0f : ((b_ < 0.0f) ? 0.0f : b_);
  186. if (clipAlpha)
  187. a_ = (a_ > 1.0f) ? 1.0f : ((a_ < 0.0f) ? 0.0f : a_);
  188. }
  189. void Color::Invert(bool invertAlpha)
  190. {
  191. r_ = 1.0f - r_;
  192. g_ = 1.0f - g_;
  193. b_ = 1.0f - b_;
  194. if (invertAlpha)
  195. a_ = 1.0f - a_;
  196. }
  197. Color Color::Lerp(const Color& rhs, float t) const
  198. {
  199. float invT = 1.0f - t;
  200. return Color(
  201. r_ * invT + rhs.r_ * t,
  202. g_ * invT + rhs.g_ * t,
  203. b_ * invT + rhs.b_ * t,
  204. a_ * invT + rhs.a_ * t
  205. );
  206. }
  207. String Color::ToString() const
  208. {
  209. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  210. sprintf(tempBuffer, "%g %g %g %g", r_, g_, b_, a_);
  211. return String(tempBuffer);
  212. }
  213. float Color::Hue(float min, float max) const
  214. {
  215. float chroma = max - min;
  216. // If chroma equals zero, hue is undefined
  217. if (chroma <= M_EPSILON)
  218. return 0.0f;
  219. // Calculate and return hue
  220. if (Urho3D::Equals(g_, max))
  221. return (b_ + 2.0f * chroma - r_) / (6.0f * chroma);
  222. else if (Urho3D::Equals(b_, max))
  223. return (4.0f * chroma - g_ + r_) / (6.0f * chroma);
  224. else
  225. {
  226. float r = (g_ - b_) / (6.0f * chroma);
  227. return (r < 0.0f) ? 1.0f + r : ((r >= 1.0f) ? r - 1.0f : r);
  228. }
  229. }
  230. float Color::SaturationHSV(float min, float max) const
  231. {
  232. // Avoid div-by-zero: result undefined
  233. if (max <= M_EPSILON)
  234. return 0.0f;
  235. // Saturation equals chroma:value ratio
  236. return 1.0f - (min / max);
  237. }
  238. float Color::SaturationHSL(float min, float max) const
  239. {
  240. // Avoid div-by-zero: result undefined
  241. if (max <= M_EPSILON || min >= 1.0f - M_EPSILON)
  242. return 0.0f;
  243. // Chroma = max - min, lightness = (max + min) * 0.5
  244. float hl = (max + min);
  245. if (hl <= 1.0f)
  246. return (max - min) / hl;
  247. else
  248. return (min - max) / (hl - 2.0f);
  249. }
  250. void Color::FromHCM(float h, float c, float m)
  251. {
  252. if (h < 0.0f || h >= 1.0f)
  253. h -= floorf(h);
  254. float hs = h * 6.0f;
  255. float x = c * (1.0f - Urho3D::Abs(fmodf(hs, 2.0f) - 1.0f));
  256. // Reconstruct r', g', b' from hue
  257. if (hs < 2.0f)
  258. {
  259. b_ = 0.0f;
  260. if (hs < 1.0f)
  261. {
  262. g_ = x;
  263. r_ = c;
  264. }
  265. else
  266. {
  267. g_ = c;
  268. r_ = x;
  269. }
  270. }
  271. else if (hs < 4.0f)
  272. {
  273. r_ = 0.0f;
  274. if (hs < 3.0f)
  275. {
  276. g_ = c;
  277. b_ = x;
  278. }
  279. else
  280. {
  281. g_ = x;
  282. b_ = c;
  283. }
  284. }
  285. else
  286. {
  287. g_ = 0.0f;
  288. if (hs < 5.0f)
  289. {
  290. r_ = x;
  291. b_ = c;
  292. }
  293. else
  294. {
  295. r_ = c;
  296. b_ = x;
  297. }
  298. }
  299. r_ += m;
  300. g_ += m;
  301. b_ += m;
  302. }
  303. const Color::ChannelMask Color::ABGR{ 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 };
  304. const Color::ChannelMask Color::ARGB{ 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 };
  305. const Color Color::WHITE;
  306. const Color Color::GRAY(0.5f, 0.5f, 0.5f);
  307. const Color Color::BLACK(0.0f, 0.0f, 0.0f);
  308. const Color Color::RED(1.0f, 0.0f, 0.0f);
  309. const Color Color::GREEN(0.0f, 1.0f, 0.0f);
  310. const Color Color::BLUE(0.0f, 0.0f, 1.0f);
  311. const Color Color::CYAN(0.0f, 1.0f, 1.0f);
  312. const Color Color::MAGENTA(1.0f, 0.0f, 1.0f);
  313. const Color Color::YELLOW(1.0f, 1.0f, 0.0f);
  314. const Color Color::TRANSPARENT_BLACK(0.0f, 0.0f, 0.0f, 0.0f);
  315. }