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

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