Color.cpp 7.5 KB

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