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.

451 lines
15KB

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