The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

453 lines
15KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. namespace ColourHelpers
  19. {
  20. static uint8 floatToUInt8 (const float n) noexcept
  21. {
  22. return n <= 0.0f ? 0 : (n >= 1.0f ? 255 : (uint8) (n * 255.0f));
  23. }
  24. // This is an adjusted brightness value, based on the way the human
  25. // eye responds to different colour channels..
  26. static float getPerceivedBrightness (Colour c) noexcept
  27. {
  28. const float r = c.getFloatRed();
  29. const float g = c.getFloatGreen();
  30. const float b = c.getFloatBlue();
  31. return std::sqrt (r * r * 0.241f
  32. + g * g * 0.691f
  33. + b * b * 0.068f);
  34. }
  35. //==============================================================================
  36. struct HSB
  37. {
  38. HSB (Colour col) noexcept
  39. {
  40. const int r = col.getRed();
  41. const int g = col.getGreen();
  42. const int b = col.getBlue();
  43. const int hi = jmax (r, g, b);
  44. const int lo = jmin (r, g, b);
  45. if (hi != 0)
  46. {
  47. saturation = (hi - lo) / (float) hi;
  48. if (saturation > 0)
  49. {
  50. const float invDiff = 1.0f / (hi - lo);
  51. const float red = (hi - r) * invDiff;
  52. const float green = (hi - g) * invDiff;
  53. const float blue = (hi - b) * invDiff;
  54. if (r == hi)
  55. hue = blue - green;
  56. else if (g == hi)
  57. hue = 2.0f + red - blue;
  58. else
  59. hue = 4.0f + green - red;
  60. hue *= 1.0f / 6.0f;
  61. if (hue < 0)
  62. ++hue;
  63. }
  64. else
  65. {
  66. hue = 0;
  67. }
  68. }
  69. else
  70. {
  71. saturation = hue = 0;
  72. }
  73. brightness = hi / 255.0f;
  74. }
  75. Colour toColour (Colour original) const noexcept
  76. {
  77. return Colour (hue, saturation, brightness, original.getAlpha());
  78. }
  79. static PixelARGB toRGB (float h, float s, float v, const uint8 alpha) noexcept
  80. {
  81. v = jlimit (0.0f, 255.0f, v * 255.0f);
  82. const uint8 intV = (uint8) roundToInt (v);
  83. if (s <= 0)
  84. return PixelARGB (alpha, intV, intV, intV);
  85. s = jmin (1.0f, s);
  86. h = (h - std::floor (h)) * 6.0f + 0.00001f; // need a small adjustment to compensate for rounding errors
  87. const float f = h - std::floor (h);
  88. const uint8 x = (uint8) roundToInt (v * (1.0f - s));
  89. if (h < 1.0f) return PixelARGB (alpha, intV, (uint8) roundToInt (v * (1.0f - (s * (1.0f - f)))), x);
  90. if (h < 2.0f) return PixelARGB (alpha, (uint8) roundToInt (v * (1.0f - s * f)), intV, x);
  91. if (h < 3.0f) return PixelARGB (alpha, x, intV, (uint8) roundToInt (v * (1.0f - (s * (1.0f - f)))));
  92. if (h < 4.0f) return PixelARGB (alpha, x, (uint8) roundToInt (v * (1.0f - s * f)), intV);
  93. if (h < 5.0f) return PixelARGB (alpha, (uint8) roundToInt (v * (1.0f - (s * (1.0f - f)))), x, intV);
  94. return PixelARGB (alpha, intV, x, (uint8) roundToInt (v * (1.0f - s * f)));
  95. }
  96. float hue, saturation, brightness;
  97. };
  98. //==============================================================================
  99. struct YIQ
  100. {
  101. YIQ (Colour c) noexcept
  102. {
  103. const float r = c.getFloatRed();
  104. const float g = c.getFloatGreen();
  105. const float b = c.getFloatBlue();
  106. y = 0.2999f * r + 0.5870f * g + 0.1140f * b;
  107. i = 0.5957f * r - 0.2744f * g - 0.3212f * b;
  108. q = 0.2114f * r - 0.5225f * g - 0.3113f * b;
  109. alpha = c.getFloatAlpha();
  110. }
  111. Colour toColour() const noexcept
  112. {
  113. return Colour::fromFloatRGBA (y + 0.9563f * i + 0.6210f * q,
  114. y - 0.2721f * i - 0.6474f * q,
  115. y - 1.1070f * i + 1.7046f * q,
  116. alpha);
  117. }
  118. float y, i, q, alpha;
  119. };
  120. }
  121. //==============================================================================
  122. Colour::Colour() noexcept
  123. : argb (0)
  124. {
  125. }
  126. Colour::Colour (const Colour& other) noexcept
  127. : argb (other.argb)
  128. {
  129. }
  130. Colour& Colour::operator= (const Colour& other) noexcept
  131. {
  132. argb = other.argb;
  133. return *this;
  134. }
  135. bool Colour::operator== (const Colour& other) const noexcept { return argb.getARGB() == other.argb.getARGB(); }
  136. bool Colour::operator!= (const Colour& other) const noexcept { return argb.getARGB() != other.argb.getARGB(); }
  137. //==============================================================================
  138. Colour::Colour (const uint32 argb_) noexcept
  139. : argb (argb_)
  140. {
  141. }
  142. Colour::Colour (const uint8 red, const uint8 green, const uint8 blue) noexcept
  143. {
  144. argb.setARGB (0xff, red, green, blue);
  145. }
  146. Colour Colour::fromRGB (const uint8 red, const uint8 green, const uint8 blue) noexcept
  147. {
  148. return Colour (red, green, blue);
  149. }
  150. Colour::Colour (const uint8 red, const uint8 green, const uint8 blue, const uint8 alpha) noexcept
  151. {
  152. argb.setARGB (alpha, red, green, blue);
  153. }
  154. Colour Colour::fromRGBA (const uint8 red, const uint8 green, const uint8 blue, const uint8 alpha) noexcept
  155. {
  156. return Colour (red, green, blue, alpha);
  157. }
  158. Colour::Colour (const uint8 red, const uint8 green, const uint8 blue, const float alpha) noexcept
  159. {
  160. argb.setARGB (ColourHelpers::floatToUInt8 (alpha), red, green, blue);
  161. }
  162. Colour Colour::fromFloatRGBA (const float red, const float green, const float blue, const float alpha) noexcept
  163. {
  164. return Colour (ColourHelpers::floatToUInt8 (red),
  165. ColourHelpers::floatToUInt8 (green),
  166. ColourHelpers::floatToUInt8 (blue), alpha);
  167. }
  168. Colour::Colour (const float hue, const float saturation, const float brightness, const float alpha) noexcept
  169. : argb (ColourHelpers::HSB::toRGB (hue, saturation, brightness, ColourHelpers::floatToUInt8 (alpha)))
  170. {
  171. }
  172. Colour Colour::fromHSV (const float hue, const float saturation, const float brightness, const float alpha) noexcept
  173. {
  174. return Colour (hue, saturation, brightness, alpha);
  175. }
  176. Colour::Colour (const float hue, const float saturation, const float brightness, const uint8 alpha) noexcept
  177. : argb (ColourHelpers::HSB::toRGB (hue, saturation, brightness, alpha))
  178. {
  179. }
  180. Colour::~Colour() noexcept
  181. {
  182. }
  183. //==============================================================================
  184. const PixelARGB Colour::getPixelARGB() const noexcept
  185. {
  186. PixelARGB p (argb);
  187. p.premultiply();
  188. return p;
  189. }
  190. uint32 Colour::getARGB() const noexcept
  191. {
  192. return argb.getARGB();
  193. }
  194. //==============================================================================
  195. bool Colour::isTransparent() const noexcept
  196. {
  197. return getAlpha() == 0;
  198. }
  199. bool Colour::isOpaque() const noexcept
  200. {
  201. return getAlpha() == 0xff;
  202. }
  203. Colour Colour::withAlpha (const uint8 newAlpha) const noexcept
  204. {
  205. PixelARGB newCol (argb);
  206. newCol.setAlpha (newAlpha);
  207. return Colour (newCol.getARGB());
  208. }
  209. Colour Colour::withAlpha (const float newAlpha) const noexcept
  210. {
  211. jassert (newAlpha >= 0 && newAlpha <= 1.0f);
  212. PixelARGB newCol (argb);
  213. newCol.setAlpha (ColourHelpers::floatToUInt8 (newAlpha));
  214. return Colour (newCol.getARGB());
  215. }
  216. Colour Colour::withMultipliedAlpha (const float alphaMultiplier) const noexcept
  217. {
  218. jassert (alphaMultiplier >= 0);
  219. PixelARGB newCol (argb);
  220. newCol.setAlpha ((uint8) jmin (0xff, roundToInt (alphaMultiplier * newCol.getAlpha())));
  221. return Colour (newCol.getARGB());
  222. }
  223. //==============================================================================
  224. Colour Colour::overlaidWith (Colour src) const noexcept
  225. {
  226. const int destAlpha = getAlpha();
  227. if (destAlpha <= 0)
  228. return src;
  229. const int invA = 0xff - (int) src.getAlpha();
  230. const int resA = 0xff - (((0xff - destAlpha) * invA) >> 8);
  231. if (resA <= 0)
  232. return *this;
  233. const int da = (invA * destAlpha) / resA;
  234. return Colour ((uint8) (src.getRed() + ((((int) getRed() - src.getRed()) * da) >> 8)),
  235. (uint8) (src.getGreen() + ((((int) getGreen() - src.getGreen()) * da) >> 8)),
  236. (uint8) (src.getBlue() + ((((int) getBlue() - src.getBlue()) * da) >> 8)),
  237. (uint8) resA);
  238. }
  239. Colour Colour::interpolatedWith (Colour other, float proportionOfOther) const noexcept
  240. {
  241. if (proportionOfOther <= 0)
  242. return *this;
  243. if (proportionOfOther >= 1.0f)
  244. return other;
  245. PixelARGB c1 (getPixelARGB());
  246. const PixelARGB c2 (other.getPixelARGB());
  247. c1.tween (c2, (uint32) roundToInt (proportionOfOther * 255.0f));
  248. c1.unpremultiply();
  249. return Colour (c1.getARGB());
  250. }
  251. //==============================================================================
  252. float Colour::getFloatRed() const noexcept { return getRed() / 255.0f; }
  253. float Colour::getFloatGreen() const noexcept { return getGreen() / 255.0f; }
  254. float Colour::getFloatBlue() const noexcept { return getBlue() / 255.0f; }
  255. float Colour::getFloatAlpha() const noexcept { return getAlpha() / 255.0f; }
  256. //==============================================================================
  257. void Colour::getHSB (float& h, float& s, float& v) const noexcept
  258. {
  259. const ColourHelpers::HSB hsb (*this);
  260. h = hsb.hue;
  261. s = hsb.saturation;
  262. v = hsb.brightness;
  263. }
  264. float Colour::getHue() const noexcept { return ColourHelpers::HSB (*this).hue; }
  265. float Colour::getSaturation() const noexcept { return ColourHelpers::HSB (*this).saturation; }
  266. float Colour::getBrightness() const noexcept { return ColourHelpers::HSB (*this).brightness; }
  267. Colour Colour::withHue (float h) const noexcept { ColourHelpers::HSB hsb (*this); hsb.hue = h; return hsb.toColour (*this); }
  268. Colour Colour::withSaturation (float s) const noexcept { ColourHelpers::HSB hsb (*this); hsb.saturation = s; return hsb.toColour (*this); }
  269. Colour Colour::withBrightness (float v) const noexcept { ColourHelpers::HSB hsb (*this); hsb.brightness = v; return hsb.toColour (*this); }
  270. //==============================================================================
  271. Colour Colour::withRotatedHue (const float amountToRotate) const noexcept
  272. {
  273. ColourHelpers::HSB hsb (*this);
  274. hsb.hue += amountToRotate;
  275. return hsb.toColour (*this);
  276. }
  277. Colour Colour::withMultipliedSaturation (const float amount) const noexcept
  278. {
  279. ColourHelpers::HSB hsb (*this);
  280. hsb.saturation = jmin (1.0f, hsb.saturation * amount);
  281. return hsb.toColour (*this);
  282. }
  283. Colour Colour::withMultipliedBrightness (const float amount) const noexcept
  284. {
  285. ColourHelpers::HSB hsb (*this);
  286. hsb.brightness = jmin (1.0f, hsb.brightness * amount);
  287. return hsb.toColour (*this);
  288. }
  289. //==============================================================================
  290. Colour Colour::brighter (float amount) const noexcept
  291. {
  292. amount = 1.0f / (1.0f + amount);
  293. return Colour ((uint8) (255 - (amount * (255 - getRed()))),
  294. (uint8) (255 - (amount * (255 - getGreen()))),
  295. (uint8) (255 - (amount * (255 - getBlue()))),
  296. getAlpha());
  297. }
  298. Colour Colour::darker (float amount) const noexcept
  299. {
  300. amount = 1.0f / (1.0f + amount);
  301. return Colour ((uint8) (amount * getRed()),
  302. (uint8) (amount * getGreen()),
  303. (uint8) (amount * getBlue()),
  304. getAlpha());
  305. }
  306. //==============================================================================
  307. Colour Colour::greyLevel (const float brightness) noexcept
  308. {
  309. const uint8 level = ColourHelpers::floatToUInt8 (brightness);
  310. return Colour (level, level, level);
  311. }
  312. //==============================================================================
  313. Colour Colour::contrasting (const float amount) const noexcept
  314. {
  315. return overlaidWith ((ColourHelpers::getPerceivedBrightness (*this) >= 0.5f
  316. ? Colours::black
  317. : Colours::white).withAlpha (amount));
  318. }
  319. Colour Colour::contrasting (Colour target, float minContrast) const noexcept
  320. {
  321. const ColourHelpers::YIQ bg (*this);
  322. ColourHelpers::YIQ fg (target);
  323. if (std::abs (bg.y - fg.y) >= minContrast)
  324. return target;
  325. const float y1 = jmax (0.0f, bg.y - minContrast);
  326. const float y2 = jmin (1.0f, bg.y + minContrast);
  327. fg.y = (std::abs (y1 - bg.y) > std::abs (y2 - bg.y)) ? y1 : y2;
  328. return fg.toColour();
  329. }
  330. Colour Colour::contrasting (Colour colour1,
  331. Colour colour2) noexcept
  332. {
  333. const float b1 = ColourHelpers::getPerceivedBrightness (colour1);
  334. const float b2 = ColourHelpers::getPerceivedBrightness (colour2);
  335. float best = 0.0f;
  336. float bestDist = 0.0f;
  337. for (float i = 0.0f; i < 1.0f; i += 0.02f)
  338. {
  339. const float d1 = std::abs (i - b1);
  340. const float d2 = std::abs (i - b2);
  341. const float dist = jmin (d1, d2, 1.0f - d1, 1.0f - d2);
  342. if (dist > bestDist)
  343. {
  344. best = i;
  345. bestDist = dist;
  346. }
  347. }
  348. return colour1.overlaidWith (colour2.withMultipliedAlpha (0.5f))
  349. .withBrightness (best);
  350. }
  351. //==============================================================================
  352. String Colour::toString() const
  353. {
  354. return String::toHexString ((int) argb.getARGB());
  355. }
  356. Colour Colour::fromString (const String& encodedColourString)
  357. {
  358. return Colour ((uint32) encodedColourString.getHexValue32());
  359. }
  360. String Colour::toDisplayString (const bool includeAlphaValue) const
  361. {
  362. return String::toHexString ((int) (argb.getARGB() & (includeAlphaValue ? 0xffffffff : 0xffffff)))
  363. .paddedLeft ('0', includeAlphaValue ? 8 : 6)
  364. .toUpperCase();
  365. }