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.

448 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 ((col >> 24) & 0xff, (col >> 16) & 0xff, (col >> 8) & 0xff, col & 0xff)
  119. {
  120. }
  121. Colour::Colour (const uint8 red, const uint8 green, const uint8 blue) noexcept
  122. {
  123. argb.setARGB (0xff, red, green, blue);
  124. }
  125. Colour Colour::fromRGB (const uint8 red, const uint8 green, const uint8 blue) noexcept
  126. {
  127. return Colour (red, green, blue);
  128. }
  129. Colour::Colour (const uint8 red, const uint8 green, const uint8 blue, const uint8 alpha) noexcept
  130. {
  131. argb.setARGB (alpha, red, green, blue);
  132. }
  133. Colour Colour::fromRGBA (const uint8 red, const uint8 green, const uint8 blue, const uint8 alpha) noexcept
  134. {
  135. return Colour (red, green, blue, alpha);
  136. }
  137. Colour::Colour (const uint8 red, const uint8 green, const uint8 blue, const float alpha) noexcept
  138. {
  139. argb.setARGB (ColourHelpers::floatToUInt8 (alpha), red, green, blue);
  140. }
  141. Colour Colour::fromFloatRGBA (const float red, const float green, const float blue, const float alpha) noexcept
  142. {
  143. return Colour (ColourHelpers::floatToUInt8 (red),
  144. ColourHelpers::floatToUInt8 (green),
  145. ColourHelpers::floatToUInt8 (blue), alpha);
  146. }
  147. Colour::Colour (const float hue, const float saturation, const float brightness, const float alpha) noexcept
  148. : argb (ColourHelpers::HSB::toRGB (hue, saturation, brightness, ColourHelpers::floatToUInt8 (alpha)))
  149. {
  150. }
  151. Colour Colour::fromHSV (const float hue, const float saturation, const float brightness, const float alpha) noexcept
  152. {
  153. return Colour (hue, saturation, brightness, alpha);
  154. }
  155. Colour::Colour (const float hue, const float saturation, const float brightness, const uint8 alpha) noexcept
  156. : argb (ColourHelpers::HSB::toRGB (hue, saturation, brightness, alpha))
  157. {
  158. }
  159. Colour::Colour (PixelARGB argb_) noexcept
  160. : argb (argb_)
  161. {
  162. }
  163. Colour::Colour (PixelRGB rgb) noexcept
  164. : argb (Colour (rgb.getInARGBMaskOrder()).argb)
  165. {
  166. }
  167. Colour::Colour (PixelAlpha alpha) noexcept
  168. : argb (Colour (alpha.getInARGBMaskOrder()).argb)
  169. {
  170. }
  171. //==============================================================================
  172. const PixelARGB Colour::getPixelARGB() const noexcept
  173. {
  174. PixelARGB p (argb);
  175. p.premultiply();
  176. return p;
  177. }
  178. uint32 Colour::getARGB() const noexcept
  179. {
  180. return argb.getInARGBMaskOrder();
  181. }
  182. //==============================================================================
  183. bool Colour::isTransparent() const noexcept
  184. {
  185. return getAlpha() == 0;
  186. }
  187. bool Colour::isOpaque() const noexcept
  188. {
  189. return getAlpha() == 0xff;
  190. }
  191. Colour Colour::withAlpha (const uint8 newAlpha) const noexcept
  192. {
  193. PixelARGB newCol (argb);
  194. newCol.setAlpha (newAlpha);
  195. return Colour (newCol);
  196. }
  197. Colour Colour::withAlpha (const float newAlpha) const noexcept
  198. {
  199. jassert (newAlpha >= 0 && newAlpha <= 1.0f);
  200. PixelARGB newCol (argb);
  201. newCol.setAlpha (ColourHelpers::floatToUInt8 (newAlpha));
  202. return Colour (newCol);
  203. }
  204. Colour Colour::withMultipliedAlpha (const float alphaMultiplier) const noexcept
  205. {
  206. jassert (alphaMultiplier >= 0);
  207. PixelARGB newCol (argb);
  208. newCol.setAlpha ((uint8) jmin (0xff, roundToInt (alphaMultiplier * newCol.getAlpha())));
  209. return Colour (newCol);
  210. }
  211. //==============================================================================
  212. Colour Colour::overlaidWith (Colour src) const noexcept
  213. {
  214. const int destAlpha = getAlpha();
  215. if (destAlpha <= 0)
  216. return src;
  217. const int invA = 0xff - (int) src.getAlpha();
  218. const int resA = 0xff - (((0xff - destAlpha) * invA) >> 8);
  219. if (resA <= 0)
  220. return *this;
  221. const int da = (invA * destAlpha) / resA;
  222. return Colour ((uint8) (src.getRed() + ((((int) getRed() - src.getRed()) * da) >> 8)),
  223. (uint8) (src.getGreen() + ((((int) getGreen() - src.getGreen()) * da) >> 8)),
  224. (uint8) (src.getBlue() + ((((int) getBlue() - src.getBlue()) * da) >> 8)),
  225. (uint8) resA);
  226. }
  227. Colour Colour::interpolatedWith (Colour other, float proportionOfOther) const noexcept
  228. {
  229. if (proportionOfOther <= 0)
  230. return *this;
  231. if (proportionOfOther >= 1.0f)
  232. return other;
  233. PixelARGB c1 (getPixelARGB());
  234. const PixelARGB c2 (other.getPixelARGB());
  235. c1.tween (c2, (uint32) roundToInt (proportionOfOther * 255.0f));
  236. c1.unpremultiply();
  237. return Colour (c1);
  238. }
  239. //==============================================================================
  240. float Colour::getFloatRed() const noexcept { return getRed() / 255.0f; }
  241. float Colour::getFloatGreen() const noexcept { return getGreen() / 255.0f; }
  242. float Colour::getFloatBlue() const noexcept { return getBlue() / 255.0f; }
  243. float Colour::getFloatAlpha() const noexcept { return getAlpha() / 255.0f; }
  244. //==============================================================================
  245. void Colour::getHSB (float& h, float& s, float& v) const noexcept
  246. {
  247. const ColourHelpers::HSB hsb (*this);
  248. h = hsb.hue;
  249. s = hsb.saturation;
  250. v = hsb.brightness;
  251. }
  252. float Colour::getHue() const noexcept { return ColourHelpers::HSB (*this).hue; }
  253. float Colour::getSaturation() const noexcept { return ColourHelpers::HSB (*this).saturation; }
  254. float Colour::getBrightness() const noexcept { return ColourHelpers::HSB (*this).brightness; }
  255. Colour Colour::withHue (float h) const noexcept { ColourHelpers::HSB hsb (*this); hsb.hue = h; return hsb.toColour (*this); }
  256. Colour Colour::withSaturation (float s) const noexcept { ColourHelpers::HSB hsb (*this); hsb.saturation = s; return hsb.toColour (*this); }
  257. Colour Colour::withBrightness (float v) const noexcept { ColourHelpers::HSB hsb (*this); hsb.brightness = v; return hsb.toColour (*this); }
  258. float Colour::getPerceivedBrightness() const noexcept
  259. {
  260. return std::sqrt (0.241f * square (getFloatRed())
  261. + 0.691f * square (getFloatGreen())
  262. + 0.068f * square (getFloatBlue()));
  263. }
  264. //==============================================================================
  265. Colour Colour::withRotatedHue (const float amountToRotate) const noexcept
  266. {
  267. ColourHelpers::HSB hsb (*this);
  268. hsb.hue += amountToRotate;
  269. return hsb.toColour (*this);
  270. }
  271. Colour Colour::withMultipliedSaturation (const float amount) const noexcept
  272. {
  273. ColourHelpers::HSB hsb (*this);
  274. hsb.saturation = jmin (1.0f, hsb.saturation * amount);
  275. return hsb.toColour (*this);
  276. }
  277. Colour Colour::withMultipliedBrightness (const float amount) const noexcept
  278. {
  279. ColourHelpers::HSB hsb (*this);
  280. hsb.brightness = jmin (1.0f, hsb.brightness * amount);
  281. return hsb.toColour (*this);
  282. }
  283. //==============================================================================
  284. Colour Colour::brighter (float amount) const noexcept
  285. {
  286. amount = 1.0f / (1.0f + amount);
  287. return Colour ((uint8) (255 - (amount * (255 - getRed()))),
  288. (uint8) (255 - (amount * (255 - getGreen()))),
  289. (uint8) (255 - (amount * (255 - getBlue()))),
  290. getAlpha());
  291. }
  292. Colour Colour::darker (float amount) const noexcept
  293. {
  294. amount = 1.0f / (1.0f + amount);
  295. return Colour ((uint8) (amount * getRed()),
  296. (uint8) (amount * getGreen()),
  297. (uint8) (amount * getBlue()),
  298. getAlpha());
  299. }
  300. //==============================================================================
  301. Colour Colour::greyLevel (const float brightness) noexcept
  302. {
  303. const uint8 level = ColourHelpers::floatToUInt8 (brightness);
  304. return Colour (level, level, level);
  305. }
  306. //==============================================================================
  307. Colour Colour::contrasting (const float amount) const noexcept
  308. {
  309. return overlaidWith ((getPerceivedBrightness() >= 0.5f
  310. ? Colours::black
  311. : Colours::white).withAlpha (amount));
  312. }
  313. Colour Colour::contrasting (Colour target, float minContrast) const noexcept
  314. {
  315. const ColourHelpers::YIQ bg (*this);
  316. ColourHelpers::YIQ fg (target);
  317. if (std::abs (bg.y - fg.y) >= minContrast)
  318. return target;
  319. const float y1 = jmax (0.0f, bg.y - minContrast);
  320. const float y2 = jmin (1.0f, bg.y + minContrast);
  321. fg.y = (std::abs (y1 - bg.y) > std::abs (y2 - bg.y)) ? y1 : y2;
  322. return fg.toColour();
  323. }
  324. Colour Colour::contrasting (Colour colour1,
  325. Colour colour2) noexcept
  326. {
  327. const float b1 = colour1.getPerceivedBrightness();
  328. const float b2 = colour2.getPerceivedBrightness();
  329. float best = 0.0f;
  330. float bestDist = 0.0f;
  331. for (float i = 0.0f; i < 1.0f; i += 0.02f)
  332. {
  333. const float d1 = std::abs (i - b1);
  334. const float d2 = std::abs (i - b2);
  335. const float dist = jmin (d1, d2, 1.0f - d1, 1.0f - d2);
  336. if (dist > bestDist)
  337. {
  338. best = i;
  339. bestDist = dist;
  340. }
  341. }
  342. return colour1.overlaidWith (colour2.withMultipliedAlpha (0.5f))
  343. .withBrightness (best);
  344. }
  345. //==============================================================================
  346. String Colour::toString() const
  347. {
  348. return String::toHexString ((int) argb.getInARGBMaskOrder());
  349. }
  350. Colour Colour::fromString (StringRef encodedColourString)
  351. {
  352. return Colour ((uint32) CharacterFunctions::HexParser<int>::parse (encodedColourString.text));
  353. }
  354. String Colour::toDisplayString (const bool includeAlphaValue) const
  355. {
  356. return String::toHexString ((int) (argb.getInARGBMaskOrder() & (includeAlphaValue ? 0xffffffff : 0xffffff)))
  357. .paddedLeft ('0', includeAlphaValue ? 8 : 6)
  358. .toUpperCase();
  359. }
  360. } // namespace juce