Color.cpp 7.8 KB

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