Audio plugin host https://kx.studio/carla
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.

juce_Colour.cpp 15KB

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