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.

452 lines
15KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. namespace ColourHelpers
  18. {
  19. static uint8 floatToUInt8 (const float n) noexcept
  20. {
  21. // the multiplier has a small extra amount to allow for FP rounding errors.
  22. return n <= 0.0f ? 0 : (n >= 1.0f ? 255 : (uint8) (n * 255.1f));
  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 col) noexcept : argb (col)
  139. {
  140. }
  141. Colour::Colour (const uint8 red, const uint8 green, const uint8 blue) noexcept
  142. {
  143. argb.setARGB (0xff, red, green, blue);
  144. }
  145. Colour Colour::fromRGB (const uint8 red, const uint8 green, const uint8 blue) noexcept
  146. {
  147. return Colour (red, green, blue);
  148. }
  149. Colour::Colour (const uint8 red, const uint8 green, const uint8 blue, const uint8 alpha) noexcept
  150. {
  151. argb.setARGB (alpha, red, green, blue);
  152. }
  153. Colour Colour::fromRGBA (const uint8 red, const uint8 green, const uint8 blue, const uint8 alpha) noexcept
  154. {
  155. return Colour (red, green, blue, alpha);
  156. }
  157. Colour::Colour (const uint8 red, const uint8 green, const uint8 blue, const float alpha) noexcept
  158. {
  159. argb.setARGB (ColourHelpers::floatToUInt8 (alpha), red, green, blue);
  160. }
  161. Colour Colour::fromFloatRGBA (const float red, const float green, const float blue, const float alpha) noexcept
  162. {
  163. return Colour (ColourHelpers::floatToUInt8 (red),
  164. ColourHelpers::floatToUInt8 (green),
  165. ColourHelpers::floatToUInt8 (blue), alpha);
  166. }
  167. Colour::Colour (const float hue, const float saturation, const float brightness, const float alpha) noexcept
  168. : argb (ColourHelpers::HSB::toRGB (hue, saturation, brightness, ColourHelpers::floatToUInt8 (alpha)))
  169. {
  170. }
  171. Colour Colour::fromHSV (const float hue, const float saturation, const float brightness, const float alpha) noexcept
  172. {
  173. return Colour (hue, saturation, brightness, alpha);
  174. }
  175. Colour::Colour (const float hue, const float saturation, const float brightness, const uint8 alpha) noexcept
  176. : argb (ColourHelpers::HSB::toRGB (hue, saturation, brightness, alpha))
  177. {
  178. }
  179. Colour::~Colour() noexcept
  180. {
  181. }
  182. //==============================================================================
  183. const PixelARGB Colour::getPixelARGB() const noexcept
  184. {
  185. PixelARGB p (argb);
  186. p.premultiply();
  187. return p;
  188. }
  189. uint32 Colour::getARGB() const noexcept
  190. {
  191. return argb.getARGB();
  192. }
  193. //==============================================================================
  194. bool Colour::isTransparent() const noexcept
  195. {
  196. return getAlpha() == 0;
  197. }
  198. bool Colour::isOpaque() const noexcept
  199. {
  200. return getAlpha() == 0xff;
  201. }
  202. Colour Colour::withAlpha (const uint8 newAlpha) const noexcept
  203. {
  204. PixelARGB newCol (argb);
  205. newCol.setAlpha (newAlpha);
  206. return Colour (newCol.getARGB());
  207. }
  208. Colour Colour::withAlpha (const float newAlpha) const noexcept
  209. {
  210. jassert (newAlpha >= 0 && newAlpha <= 1.0f);
  211. PixelARGB newCol (argb);
  212. newCol.setAlpha (ColourHelpers::floatToUInt8 (newAlpha));
  213. return Colour (newCol.getARGB());
  214. }
  215. Colour Colour::withMultipliedAlpha (const float alphaMultiplier) const noexcept
  216. {
  217. jassert (alphaMultiplier >= 0);
  218. PixelARGB newCol (argb);
  219. newCol.setAlpha ((uint8) jmin (0xff, roundToInt (alphaMultiplier * newCol.getAlpha())));
  220. return Colour (newCol.getARGB());
  221. }
  222. //==============================================================================
  223. Colour Colour::overlaidWith (Colour src) const noexcept
  224. {
  225. const int destAlpha = getAlpha();
  226. if (destAlpha <= 0)
  227. return src;
  228. const int invA = 0xff - (int) src.getAlpha();
  229. const int resA = 0xff - (((0xff - destAlpha) * invA) >> 8);
  230. if (resA <= 0)
  231. return *this;
  232. const int da = (invA * destAlpha) / resA;
  233. return Colour ((uint8) (src.getRed() + ((((int) getRed() - src.getRed()) * da) >> 8)),
  234. (uint8) (src.getGreen() + ((((int) getGreen() - src.getGreen()) * da) >> 8)),
  235. (uint8) (src.getBlue() + ((((int) getBlue() - src.getBlue()) * da) >> 8)),
  236. (uint8) resA);
  237. }
  238. Colour Colour::interpolatedWith (Colour other, float proportionOfOther) const noexcept
  239. {
  240. if (proportionOfOther <= 0)
  241. return *this;
  242. if (proportionOfOther >= 1.0f)
  243. return other;
  244. PixelARGB c1 (getPixelARGB());
  245. const PixelARGB c2 (other.getPixelARGB());
  246. c1.tween (c2, (uint32) roundToInt (proportionOfOther * 255.0f));
  247. c1.unpremultiply();
  248. return Colour (c1.getARGB());
  249. }
  250. //==============================================================================
  251. float Colour::getFloatRed() const noexcept { return getRed() / 255.0f; }
  252. float Colour::getFloatGreen() const noexcept { return getGreen() / 255.0f; }
  253. float Colour::getFloatBlue() const noexcept { return getBlue() / 255.0f; }
  254. float Colour::getFloatAlpha() const noexcept { return getAlpha() / 255.0f; }
  255. //==============================================================================
  256. void Colour::getHSB (float& h, float& s, float& v) const noexcept
  257. {
  258. const ColourHelpers::HSB hsb (*this);
  259. h = hsb.hue;
  260. s = hsb.saturation;
  261. v = hsb.brightness;
  262. }
  263. float Colour::getHue() const noexcept { return ColourHelpers::HSB (*this).hue; }
  264. float Colour::getSaturation() const noexcept { return ColourHelpers::HSB (*this).saturation; }
  265. float Colour::getBrightness() const noexcept { return ColourHelpers::HSB (*this).brightness; }
  266. Colour Colour::withHue (float h) const noexcept { ColourHelpers::HSB hsb (*this); hsb.hue = h; return hsb.toColour (*this); }
  267. Colour Colour::withSaturation (float s) const noexcept { ColourHelpers::HSB hsb (*this); hsb.saturation = s; return hsb.toColour (*this); }
  268. Colour Colour::withBrightness (float v) const noexcept { ColourHelpers::HSB hsb (*this); hsb.brightness = v; return hsb.toColour (*this); }
  269. //==============================================================================
  270. Colour Colour::withRotatedHue (const float amountToRotate) const noexcept
  271. {
  272. ColourHelpers::HSB hsb (*this);
  273. hsb.hue += amountToRotate;
  274. return hsb.toColour (*this);
  275. }
  276. Colour Colour::withMultipliedSaturation (const float amount) const noexcept
  277. {
  278. ColourHelpers::HSB hsb (*this);
  279. hsb.saturation = jmin (1.0f, hsb.saturation * amount);
  280. return hsb.toColour (*this);
  281. }
  282. Colour Colour::withMultipliedBrightness (const float amount) const noexcept
  283. {
  284. ColourHelpers::HSB hsb (*this);
  285. hsb.brightness = jmin (1.0f, hsb.brightness * amount);
  286. return hsb.toColour (*this);
  287. }
  288. //==============================================================================
  289. Colour Colour::brighter (float amount) const noexcept
  290. {
  291. amount = 1.0f / (1.0f + amount);
  292. return Colour ((uint8) (255 - (amount * (255 - getRed()))),
  293. (uint8) (255 - (amount * (255 - getGreen()))),
  294. (uint8) (255 - (amount * (255 - getBlue()))),
  295. getAlpha());
  296. }
  297. Colour Colour::darker (float amount) const noexcept
  298. {
  299. amount = 1.0f / (1.0f + amount);
  300. return Colour ((uint8) (amount * getRed()),
  301. (uint8) (amount * getGreen()),
  302. (uint8) (amount * getBlue()),
  303. getAlpha());
  304. }
  305. //==============================================================================
  306. Colour Colour::greyLevel (const float brightness) noexcept
  307. {
  308. const uint8 level = ColourHelpers::floatToUInt8 (brightness);
  309. return Colour (level, level, level);
  310. }
  311. //==============================================================================
  312. Colour Colour::contrasting (const float amount) const noexcept
  313. {
  314. return overlaidWith ((ColourHelpers::getPerceivedBrightness (*this) >= 0.5f
  315. ? Colours::black
  316. : Colours::white).withAlpha (amount));
  317. }
  318. Colour Colour::contrasting (Colour target, float minContrast) const noexcept
  319. {
  320. const ColourHelpers::YIQ bg (*this);
  321. ColourHelpers::YIQ fg (target);
  322. if (std::abs (bg.y - fg.y) >= minContrast)
  323. return target;
  324. const float y1 = jmax (0.0f, bg.y - minContrast);
  325. const float y2 = jmin (1.0f, bg.y + minContrast);
  326. fg.y = (std::abs (y1 - bg.y) > std::abs (y2 - bg.y)) ? y1 : y2;
  327. return fg.toColour();
  328. }
  329. Colour Colour::contrasting (Colour colour1,
  330. Colour colour2) noexcept
  331. {
  332. const float b1 = ColourHelpers::getPerceivedBrightness (colour1);
  333. const float b2 = ColourHelpers::getPerceivedBrightness (colour2);
  334. float best = 0.0f;
  335. float bestDist = 0.0f;
  336. for (float i = 0.0f; i < 1.0f; i += 0.02f)
  337. {
  338. const float d1 = std::abs (i - b1);
  339. const float d2 = std::abs (i - b2);
  340. const float dist = jmin (d1, d2, 1.0f - d1, 1.0f - d2);
  341. if (dist > bestDist)
  342. {
  343. best = i;
  344. bestDist = dist;
  345. }
  346. }
  347. return colour1.overlaidWith (colour2.withMultipliedAlpha (0.5f))
  348. .withBrightness (best);
  349. }
  350. //==============================================================================
  351. String Colour::toString() const
  352. {
  353. return String::toHexString ((int) argb.getARGB());
  354. }
  355. Colour Colour::fromString (StringRef encodedColourString)
  356. {
  357. return Colour ((uint32) CharacterFunctions::HexParser<int>::parse (encodedColourString.text));
  358. }
  359. String Colour::toDisplayString (const bool includeAlphaValue) const
  360. {
  361. return String::toHexString ((int) (argb.getARGB() & (includeAlphaValue ? 0xffffffff : 0xffffff)))
  362. .paddedLeft ('0', includeAlphaValue ? 8 : 6)
  363. .toUpperCase();
  364. }