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.

412 lines
14KB

  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. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. namespace ColourHelpers
  21. {
  22. uint8 floatToUInt8 (const float n) noexcept
  23. {
  24. return n <= 0.0f ? 0 : (n >= 1.0f ? 255 : (uint8) (n * 255.0f));
  25. }
  26. // This is an adjusted brightness value, based on the way the human
  27. // eye responds to different colour channels..
  28. float getPerceivedBrightness (float r, float g, float b) noexcept
  29. {
  30. return std::sqrt (r * r * 0.241f
  31. + g * g * 0.691f
  32. + b * b * 0.068f);
  33. }
  34. }
  35. //==============================================================================
  36. struct HSB
  37. {
  38. HSB (const 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 (const 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. else return PixelARGB (alpha, intV, x, (uint8) roundToInt (v * (1.0f - s * f)));
  95. }
  96. float hue, saturation, brightness;
  97. };
  98. //==============================================================================
  99. Colour::Colour() noexcept
  100. : argb (0)
  101. {
  102. }
  103. Colour::Colour (const Colour& other) noexcept
  104. : argb (other.argb)
  105. {
  106. }
  107. Colour& Colour::operator= (const Colour& other) noexcept
  108. {
  109. argb = other.argb;
  110. return *this;
  111. }
  112. bool Colour::operator== (const Colour& other) const noexcept { return argb.getARGB() == other.argb.getARGB(); }
  113. bool Colour::operator!= (const Colour& other) const noexcept { return argb.getARGB() != other.argb.getARGB(); }
  114. //==============================================================================
  115. Colour::Colour (const uint32 argb_) noexcept
  116. : argb (argb_)
  117. {
  118. }
  119. Colour::Colour (const uint8 red, const uint8 green, const uint8 blue) noexcept
  120. {
  121. argb.setARGB (0xff, red, green, blue);
  122. }
  123. Colour Colour::fromRGB (const uint8 red, const uint8 green, const uint8 blue) noexcept
  124. {
  125. return Colour (red, green, blue);
  126. }
  127. Colour::Colour (const uint8 red, const uint8 green, const uint8 blue, const uint8 alpha) noexcept
  128. {
  129. argb.setARGB (alpha, red, green, blue);
  130. }
  131. Colour Colour::fromRGBA (const uint8 red, const uint8 green, const uint8 blue, const uint8 alpha) noexcept
  132. {
  133. return Colour (red, green, blue, alpha);
  134. }
  135. Colour::Colour (const uint8 red, const uint8 green, const uint8 blue, const float alpha) noexcept
  136. {
  137. argb.setARGB (ColourHelpers::floatToUInt8 (alpha), red, green, blue);
  138. }
  139. Colour Colour::fromFloatRGBA (const float red, const float green, const float blue, const float alpha) noexcept
  140. {
  141. return Colour (ColourHelpers::floatToUInt8 (red), ColourHelpers::floatToUInt8 (green), ColourHelpers::floatToUInt8 (blue), alpha);
  142. }
  143. Colour::Colour (const float hue, const float saturation, const float brightness, const float alpha) noexcept
  144. : argb (HSB::toRGB (hue, saturation, brightness, ColourHelpers::floatToUInt8 (alpha)))
  145. {
  146. }
  147. Colour Colour::fromHSV (const float hue, const float saturation, const float brightness, const float alpha) noexcept
  148. {
  149. return Colour (hue, saturation, brightness, alpha);
  150. }
  151. Colour::Colour (const float hue, const float saturation, const float brightness, const uint8 alpha) noexcept
  152. : argb (HSB::toRGB (hue, saturation, brightness, alpha))
  153. {
  154. }
  155. Colour::~Colour() noexcept
  156. {
  157. }
  158. //==============================================================================
  159. const PixelARGB Colour::getPixelARGB() const noexcept
  160. {
  161. PixelARGB p (argb);
  162. p.premultiply();
  163. return p;
  164. }
  165. uint32 Colour::getARGB() const noexcept
  166. {
  167. return argb.getARGB();
  168. }
  169. //==============================================================================
  170. bool Colour::isTransparent() const noexcept
  171. {
  172. return getAlpha() == 0;
  173. }
  174. bool Colour::isOpaque() const noexcept
  175. {
  176. return getAlpha() == 0xff;
  177. }
  178. Colour Colour::withAlpha (const uint8 newAlpha) const noexcept
  179. {
  180. PixelARGB newCol (argb);
  181. newCol.setAlpha (newAlpha);
  182. return Colour (newCol.getARGB());
  183. }
  184. Colour Colour::withAlpha (const float newAlpha) const noexcept
  185. {
  186. jassert (newAlpha >= 0 && newAlpha <= 1.0f);
  187. PixelARGB newCol (argb);
  188. newCol.setAlpha (ColourHelpers::floatToUInt8 (newAlpha));
  189. return Colour (newCol.getARGB());
  190. }
  191. Colour Colour::withMultipliedAlpha (const float alphaMultiplier) const noexcept
  192. {
  193. jassert (alphaMultiplier >= 0);
  194. PixelARGB newCol (argb);
  195. newCol.setAlpha ((uint8) jmin (0xff, roundToInt (alphaMultiplier * newCol.getAlpha())));
  196. return Colour (newCol.getARGB());
  197. }
  198. //==============================================================================
  199. Colour Colour::overlaidWith (const Colour& src) const noexcept
  200. {
  201. const int destAlpha = getAlpha();
  202. if (destAlpha <= 0)
  203. return src;
  204. const int invA = 0xff - (int) src.getAlpha();
  205. const int resA = 0xff - (((0xff - destAlpha) * invA) >> 8);
  206. if (resA <= 0)
  207. return *this;
  208. const int da = (invA * destAlpha) / resA;
  209. return Colour ((uint8) (src.getRed() + ((((int) getRed() - src.getRed()) * da) >> 8)),
  210. (uint8) (src.getGreen() + ((((int) getGreen() - src.getGreen()) * da) >> 8)),
  211. (uint8) (src.getBlue() + ((((int) getBlue() - src.getBlue()) * da) >> 8)),
  212. (uint8) resA);
  213. }
  214. Colour Colour::interpolatedWith (const Colour& other, float proportionOfOther) const noexcept
  215. {
  216. if (proportionOfOther <= 0)
  217. return *this;
  218. if (proportionOfOther >= 1.0f)
  219. return other;
  220. PixelARGB c1 (getPixelARGB());
  221. const PixelARGB c2 (other.getPixelARGB());
  222. c1.tween (c2, (uint32) roundToInt (proportionOfOther * 255.0f));
  223. c1.unpremultiply();
  224. return Colour (c1.getARGB());
  225. }
  226. //==============================================================================
  227. float Colour::getFloatRed() const noexcept { return getRed() / 255.0f; }
  228. float Colour::getFloatGreen() const noexcept { return getGreen() / 255.0f; }
  229. float Colour::getFloatBlue() const noexcept { return getBlue() / 255.0f; }
  230. float Colour::getFloatAlpha() const noexcept { return getAlpha() / 255.0f; }
  231. //==============================================================================
  232. void Colour::getHSB (float& h, float& s, float& v) const noexcept
  233. {
  234. const HSB hsb (*this);
  235. h = hsb.hue;
  236. s = hsb.saturation;
  237. v = hsb.brightness;
  238. }
  239. float Colour::getHue() const noexcept { return HSB (*this).hue; }
  240. float Colour::getSaturation() const noexcept { return HSB (*this).saturation; }
  241. float Colour::getBrightness() const noexcept { return HSB (*this).brightness; }
  242. Colour Colour::withHue (float h) const noexcept { HSB hsb (*this); hsb.hue = h; return hsb.toColour (*this); }
  243. Colour Colour::withSaturation (float s) const noexcept { HSB hsb (*this); hsb.saturation = s; return hsb.toColour (*this); }
  244. Colour Colour::withBrightness (float v) const noexcept { HSB hsb (*this); hsb.brightness = v; return hsb.toColour (*this); }
  245. //==============================================================================
  246. Colour Colour::withRotatedHue (const float amountToRotate) const noexcept
  247. {
  248. HSB hsb (*this);
  249. hsb.hue += amountToRotate;
  250. return hsb.toColour (*this);
  251. }
  252. Colour Colour::withMultipliedSaturation (const float amount) const noexcept
  253. {
  254. HSB hsb (*this);
  255. hsb.saturation = jmin (1.0f, hsb.saturation * amount);
  256. return hsb.toColour (*this);
  257. }
  258. Colour Colour::withMultipliedBrightness (const float amount) const noexcept
  259. {
  260. HSB hsb (*this);
  261. hsb.brightness = jmin (1.0f, hsb.brightness * amount);
  262. return hsb.toColour (*this);
  263. }
  264. //==============================================================================
  265. Colour Colour::brighter (float amount) const noexcept
  266. {
  267. amount = 1.0f / (1.0f + amount);
  268. return Colour ((uint8) (255 - (amount * (255 - getRed()))),
  269. (uint8) (255 - (amount * (255 - getGreen()))),
  270. (uint8) (255 - (amount * (255 - getBlue()))),
  271. getAlpha());
  272. }
  273. Colour Colour::darker (float amount) const noexcept
  274. {
  275. amount = 1.0f / (1.0f + amount);
  276. return Colour ((uint8) (amount * getRed()),
  277. (uint8) (amount * getGreen()),
  278. (uint8) (amount * getBlue()),
  279. getAlpha());
  280. }
  281. //==============================================================================
  282. Colour Colour::greyLevel (const float brightness) noexcept
  283. {
  284. const uint8 level = ColourHelpers::floatToUInt8 (brightness);
  285. return Colour (level, level, level);
  286. }
  287. //==============================================================================
  288. Colour Colour::contrasting (const float amount) const noexcept
  289. {
  290. return overlaidWith ((ColourHelpers::getPerceivedBrightness (getFloatRed(), getFloatGreen(), getFloatBlue()) >= 0.5f
  291. ? Colours::black
  292. : Colours::white).withAlpha (amount));
  293. }
  294. Colour Colour::contrasting (const Colour& colour1,
  295. const Colour& colour2) noexcept
  296. {
  297. const float b1 = colour1.getBrightness();
  298. const float b2 = colour2.getBrightness();
  299. float best = 0.0f;
  300. float bestDist = 0.0f;
  301. for (float i = 0.0f; i < 1.0f; i += 0.02f)
  302. {
  303. const float d1 = std::abs (i - b1);
  304. const float d2 = std::abs (i - b2);
  305. const float dist = jmin (d1, d2, 1.0f - d1, 1.0f - d2);
  306. if (dist > bestDist)
  307. {
  308. best = i;
  309. bestDist = dist;
  310. }
  311. }
  312. return colour1.overlaidWith (colour2.withMultipliedAlpha (0.5f))
  313. .withBrightness (best);
  314. }
  315. //==============================================================================
  316. String Colour::toString() const
  317. {
  318. return String::toHexString ((int) argb.getARGB());
  319. }
  320. Colour Colour::fromString (const String& encodedColourString)
  321. {
  322. return Colour ((uint32) encodedColourString.getHexValue32());
  323. }
  324. String Colour::toDisplayString (const bool includeAlphaValue) const
  325. {
  326. return String::toHexString ((int) (argb.getARGB() & (includeAlphaValue ? 0xffffffff : 0xffffff)))
  327. .paddedLeft ('0', includeAlphaValue ? 8 : 6)
  328. .toUpperCase();
  329. }
  330. END_JUCE_NAMESPACE