Color.cpp 7.9 KB

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