Color.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. //
  2. // Copyright (c) 2008-2017 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 Atomic
  27. {
  28. unsigned Color::ToUInt() const
  29. {
  30. unsigned r = (unsigned)Clamp(((int)(r_ * 255.0f)), 0, 255);
  31. unsigned g = (unsigned)Clamp(((int)(g_ * 255.0f)), 0, 255);
  32. unsigned b = (unsigned)Clamp(((int)(b_ * 255.0f)), 0, 255);
  33. unsigned a = (unsigned)Clamp(((int)(a_ * 255.0f)), 0, 255);
  34. return (a << 24) | (b << 16) | (g << 8) | r;
  35. }
  36. Vector3 Color::ToHSL() const
  37. {
  38. float min, max;
  39. Bounds(&min, &max, true);
  40. float h = Hue(min, max);
  41. float s = SaturationHSL(min, max);
  42. float l = (max + min) * 0.5f;
  43. return Vector3(h, s, l);
  44. }
  45. Vector3 Color::ToHSV() const
  46. {
  47. float min, max;
  48. Bounds(&min, &max, true);
  49. float h = Hue(min, max);
  50. float s = SaturationHSV(min, max);
  51. float v = max;
  52. return Vector3(h, s, v);
  53. }
  54. void Color::FromHSL(float h, float s, float l, float a)
  55. {
  56. float c;
  57. if (l < 0.5f)
  58. c = (1.0f + (2.0f * l - 1.0f)) * s;
  59. else
  60. c = (1.0f - (2.0f * l - 1.0f)) * s;
  61. float m = l - 0.5f * c;
  62. FromHCM(h, c, m);
  63. a_ = a;
  64. }
  65. void Color::FromHSV(float h, float s, float v, float a)
  66. {
  67. float c = v * s;
  68. float m = v - c;
  69. FromHCM(h, c, m);
  70. a_ = a;
  71. }
  72. float Color::Chroma() const
  73. {
  74. float min, max;
  75. Bounds(&min, &max, true);
  76. return max - min;
  77. }
  78. float Color::Hue() const
  79. {
  80. float min, max;
  81. Bounds(&min, &max, true);
  82. return Hue(min, max);
  83. }
  84. float Color::SaturationHSL() const
  85. {
  86. float min, max;
  87. Bounds(&min, &max, true);
  88. return SaturationHSL(min, max);
  89. }
  90. float Color::SaturationHSV() const
  91. {
  92. float min, max;
  93. Bounds(&min, &max, true);
  94. return SaturationHSV(min, max);
  95. }
  96. float Color::Lightness() const
  97. {
  98. float min, max;
  99. Bounds(&min, &max, true);
  100. return (max + min) * 0.5f;
  101. }
  102. void Color::Bounds(float* min, float* max, bool clipped) const
  103. {
  104. assert(min && max);
  105. if (r_ > g_)
  106. {
  107. if (g_ > b_) // r > g > b
  108. {
  109. *max = r_;
  110. *min = b_;
  111. }
  112. else // r > g && g <= b
  113. {
  114. *max = r_ > b_ ? r_ : b_;
  115. *min = g_;
  116. }
  117. }
  118. else
  119. {
  120. if (b_ > g_) // r <= g < b
  121. {
  122. *max = b_;
  123. *min = r_;
  124. }
  125. else // r <= g && b <= g
  126. {
  127. *max = g_;
  128. *min = r_ < b_ ? r_ : b_;
  129. }
  130. }
  131. if (clipped)
  132. {
  133. *max = *max > 1.0f ? 1.0f : (*max < 0.0f ? 0.0f : *max);
  134. *min = *min > 1.0f ? 1.0f : (*min < 0.0f ? 0.0f : *min);
  135. }
  136. }
  137. float Color::MaxRGB() const
  138. {
  139. if (r_ > g_)
  140. return (r_ > b_) ? r_ : b_;
  141. else
  142. return (g_ > b_) ? g_ : b_;
  143. }
  144. float Color::MinRGB() const
  145. {
  146. if (r_ < g_)
  147. return (r_ < b_) ? r_ : b_;
  148. else
  149. return (g_ < b_) ? g_ : b_;
  150. }
  151. float Color::Range() const
  152. {
  153. float min, max;
  154. Bounds(&min, &max);
  155. return max - min;
  156. }
  157. void Color::Clip(bool clipAlpha)
  158. {
  159. r_ = (r_ > 1.0f) ? 1.0f : ((r_ < 0.0f) ? 0.0f : r_);
  160. g_ = (g_ > 1.0f) ? 1.0f : ((g_ < 0.0f) ? 0.0f : g_);
  161. b_ = (b_ > 1.0f) ? 1.0f : ((b_ < 0.0f) ? 0.0f : b_);
  162. if (clipAlpha)
  163. a_ = (a_ > 1.0f) ? 1.0f : ((a_ < 0.0f) ? 0.0f : a_);
  164. }
  165. void Color::Invert(bool invertAlpha)
  166. {
  167. r_ = 1.0f - r_;
  168. g_ = 1.0f - g_;
  169. b_ = 1.0f - b_;
  170. if (invertAlpha)
  171. a_ = 1.0f - a_;
  172. }
  173. Color Color::Lerp(const Color& rhs, float t) const
  174. {
  175. float invT = 1.0f - t;
  176. return Color(
  177. r_ * invT + rhs.r_ * t,
  178. g_ * invT + rhs.g_ * t,
  179. b_ * invT + rhs.b_ * t,
  180. a_ * invT + rhs.a_ * t
  181. );
  182. }
  183. String Color::ToString() const
  184. {
  185. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  186. sprintf(tempBuffer, "%g %g %g %g", r_, g_, b_, a_);
  187. return String(tempBuffer);
  188. }
  189. float Color::Hue(float min, float max) const
  190. {
  191. float chroma = max - min;
  192. // If chroma equals zero, hue is undefined
  193. if (chroma <= M_EPSILON)
  194. return 0.0f;
  195. // Calculate and return hue
  196. if (Atomic::Equals(g_, max))
  197. return (b_ + 2.0f * chroma - r_) / (6.0f * chroma);
  198. else if (Atomic::Equals(b_, max))
  199. return (4.0f * chroma - g_ + r_) / (6.0f * chroma);
  200. else
  201. {
  202. float r = (g_ - b_) / (6.0f * chroma);
  203. return (r < 0.0f) ? 1.0f + r : ((r >= 1.0f) ? r - 1.0f : r);
  204. }
  205. }
  206. float Color::SaturationHSV(float min, float max) const
  207. {
  208. // Avoid div-by-zero: result undefined
  209. if (max <= M_EPSILON)
  210. return 0.0f;
  211. // Saturation equals chroma:value ratio
  212. return 1.0f - (min / max);
  213. }
  214. float Color::SaturationHSL(float min, float max) const
  215. {
  216. // Avoid div-by-zero: result undefined
  217. if (max <= M_EPSILON || min >= 1.0f - M_EPSILON)
  218. return 0.0f;
  219. // Chroma = max - min, lightness = (max + min) * 0.5
  220. float hl = (max + min);
  221. if (hl <= 1.0f)
  222. return (max - min) / hl;
  223. else
  224. return (min - max) / (hl - 2.0f);
  225. }
  226. void Color::FromHCM(float h, float c, float m)
  227. {
  228. if (h < 0.0f || h >= 1.0f)
  229. h -= floorf(h);
  230. float hs = h * 6.0f;
  231. float x = c * (1.0f - Atomic::Abs(fmodf(hs, 2.0f) - 1.0f));
  232. // Reconstruct r', g', b' from hue
  233. if (hs < 2.0f)
  234. {
  235. b_ = 0.0f;
  236. if (hs < 1.0f)
  237. {
  238. g_ = x;
  239. r_ = c;
  240. }
  241. else
  242. {
  243. g_ = c;
  244. r_ = x;
  245. }
  246. }
  247. else if (hs < 4.0f)
  248. {
  249. r_ = 0.0f;
  250. if (hs < 3.0f)
  251. {
  252. g_ = c;
  253. b_ = x;
  254. }
  255. else
  256. {
  257. g_ = x;
  258. b_ = c;
  259. }
  260. }
  261. else
  262. {
  263. g_ = 0.0f;
  264. if (hs < 5.0f)
  265. {
  266. r_ = x;
  267. b_ = c;
  268. }
  269. else
  270. {
  271. r_ = c;
  272. b_ = x;
  273. }
  274. }
  275. r_ += m;
  276. g_ += m;
  277. b_ += m;
  278. }
  279. /// ATOMIC BEGIN
  280. unsigned Color::ToUIntArgb() const
  281. {
  282. unsigned r = (unsigned)Clamp(((int)(r_ * 255.0f)), 0, 255);
  283. unsigned g = (unsigned)Clamp(((int)(g_ * 255.0f)), 0, 255);
  284. unsigned b = (unsigned)Clamp(((int)(b_ * 255.0f)), 0, 255);
  285. unsigned a = (unsigned)Clamp(((int)(a_ * 255.0f)), 0, 255);
  286. return (a << 24) | (r << 16) | (g << 8) | b;
  287. }
  288. /// ATOMIC END
  289. const Color Color::WHITE;
  290. const Color Color::GRAY(0.5f, 0.5f, 0.5f);
  291. const Color Color::BLACK(0.0f, 0.0f, 0.0f);
  292. const Color Color::RED(1.0f, 0.0f, 0.0f);
  293. const Color Color::GREEN(0.0f, 1.0f, 0.0f);
  294. const Color Color::BLUE(0.0f, 0.0f, 1.0f);
  295. const Color Color::CYAN(0.0f, 1.0f, 1.0f);
  296. const Color Color::MAGENTA(1.0f, 0.0f, 1.0f);
  297. const Color Color::YELLOW(1.0f, 1.0f, 0.0f);
  298. const Color Color::TRANSPARENT(0.0f, 0.0f, 0.0f, 0.0f);
  299. }