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.

410 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /**
  17. Represents a colour, also including a transparency value.
  18. The colour is stored internally as unsigned 8-bit red, green, blue and alpha values.
  19. @tags{Graphics}
  20. */
  21. class JUCE_API Colour final
  22. {
  23. public:
  24. //==============================================================================
  25. /** Creates a transparent black colour. */
  26. Colour() = default;
  27. /** Creates a copy of another Colour object. */
  28. Colour (const Colour&) = default;
  29. /** Creates a colour from a 32-bit ARGB value.
  30. The format of this number is:
  31. ((alpha << 24) | (red << 16) | (green << 8) | blue).
  32. All components in the range 0x00 to 0xff.
  33. An alpha of 0x00 is completely transparent, alpha of 0xff is opaque.
  34. @see getPixelARGB
  35. */
  36. explicit Colour (uint32 argb) noexcept;
  37. /** Creates an opaque colour using 8-bit red, green and blue values */
  38. Colour (uint8 red,
  39. uint8 green,
  40. uint8 blue) noexcept;
  41. /** Creates an opaque colour using 8-bit red, green and blue values */
  42. static Colour fromRGB (uint8 red,
  43. uint8 green,
  44. uint8 blue) noexcept;
  45. /** Creates a colour using 8-bit red, green, blue and alpha values. */
  46. Colour (uint8 red,
  47. uint8 green,
  48. uint8 blue,
  49. uint8 alpha) noexcept;
  50. /** Creates a colour using 8-bit red, green, blue and alpha values. */
  51. static Colour fromRGBA (uint8 red,
  52. uint8 green,
  53. uint8 blue,
  54. uint8 alpha) noexcept;
  55. /** Creates a colour from 8-bit red, green, and blue values, and a floating-point alpha.
  56. Alpha of 0.0 is transparent, alpha of 1.0f is opaque.
  57. Values outside the valid range will be clipped.
  58. */
  59. Colour (uint8 red,
  60. uint8 green,
  61. uint8 blue,
  62. float alpha) noexcept;
  63. /** Creates a colour using floating point red, green, blue and alpha values.
  64. Numbers outside the range 0..1 will be clipped.
  65. */
  66. static Colour fromFloatRGBA (float red,
  67. float green,
  68. float blue,
  69. float alpha) noexcept;
  70. /** Creates a colour using floating point hue, saturation and brightness values, and an 8-bit alpha.
  71. The floating point values must be between 0.0 and 1.0.
  72. An alpha of 0x00 is completely transparent, alpha of 0xff is opaque.
  73. Values outside the valid range will be clipped.
  74. */
  75. Colour (float hue,
  76. float saturation,
  77. float brightness,
  78. uint8 alpha) noexcept;
  79. /** Creates a colour using floating point hue, saturation, brightness and alpha values.
  80. All values must be between 0.0 and 1.0.
  81. Numbers outside the valid range will be clipped.
  82. */
  83. Colour (float hue,
  84. float saturation,
  85. float brightness,
  86. float alpha) noexcept;
  87. /** Creates a colour using floating point hue, saturation, brightness and alpha values.
  88. All values must be between 0.0 and 1.0.
  89. Numbers outside the valid range will be clipped.
  90. */
  91. static Colour fromHSV (float hue,
  92. float saturation,
  93. float brightness,
  94. float alpha) noexcept;
  95. /** Creates a colour using floating point hue, saturation, lightness and alpha values.
  96. All values must be between 0.0 and 1.0.
  97. Numbers outside the valid range will be clipped.
  98. */
  99. static Colour fromHSL (float hue,
  100. float saturation,
  101. float lightness,
  102. float alpha) noexcept;
  103. /** Creates a colour using a PixelARGB object. This function assumes that the argb pixel is
  104. not premultiplied.
  105. */
  106. Colour (PixelARGB argb) noexcept;
  107. /** Creates a colour using a PixelRGB object.
  108. */
  109. Colour (PixelRGB rgb) noexcept;
  110. /** Creates a colour using a PixelAlpha object.
  111. */
  112. Colour (PixelAlpha alpha) noexcept;
  113. /** Destructor. */
  114. ~Colour() = default;
  115. /** Copies another Colour object. */
  116. Colour& operator= (const Colour&) = default;
  117. /** Compares two colours. */
  118. bool operator== (const Colour& other) const noexcept;
  119. /** Compares two colours. */
  120. bool operator!= (const Colour& other) const noexcept;
  121. //==============================================================================
  122. /** Returns the red component of this colour.
  123. @returns a value between 0x00 and 0xff.
  124. */
  125. uint8 getRed() const noexcept { return argb.getRed(); }
  126. /** Returns the green component of this colour.
  127. @returns a value between 0x00 and 0xff.
  128. */
  129. uint8 getGreen() const noexcept { return argb.getGreen(); }
  130. /** Returns the blue component of this colour.
  131. @returns a value between 0x00 and 0xff.
  132. */
  133. uint8 getBlue() const noexcept { return argb.getBlue(); }
  134. /** Returns the red component of this colour as a floating point value.
  135. @returns a value between 0.0 and 1.0
  136. */
  137. float getFloatRed() const noexcept;
  138. /** Returns the green component of this colour as a floating point value.
  139. @returns a value between 0.0 and 1.0
  140. */
  141. float getFloatGreen() const noexcept;
  142. /** Returns the blue component of this colour as a floating point value.
  143. @returns a value between 0.0 and 1.0
  144. */
  145. float getFloatBlue() const noexcept;
  146. /** Returns a premultiplied ARGB pixel object that represents this colour.
  147. */
  148. const PixelARGB getPixelARGB() const noexcept;
  149. /** Returns a 32-bit integer that represents this colour.
  150. The format of this number is:
  151. ((alpha << 24) | (red << 16) | (green << 8) | blue).
  152. */
  153. uint32 getARGB() const noexcept;
  154. //==============================================================================
  155. /** Returns the colour's alpha (opacity).
  156. Alpha of 0x00 is completely transparent, 0xff is completely opaque.
  157. */
  158. uint8 getAlpha() const noexcept { return argb.getAlpha(); }
  159. /** Returns the colour's alpha (opacity) as a floating point value.
  160. Alpha of 0.0 is completely transparent, 1.0 is completely opaque.
  161. */
  162. float getFloatAlpha() const noexcept;
  163. /** Returns true if this colour is completely opaque.
  164. Equivalent to (getAlpha() == 0xff).
  165. */
  166. bool isOpaque() const noexcept;
  167. /** Returns true if this colour is completely transparent.
  168. Equivalent to (getAlpha() == 0x00).
  169. */
  170. bool isTransparent() const noexcept;
  171. /** Returns a colour that's the same colour as this one, but with a new alpha value. */
  172. Colour withAlpha (uint8 newAlpha) const noexcept;
  173. /** Returns a colour that's the same colour as this one, but with a new alpha value. */
  174. Colour withAlpha (float newAlpha) const noexcept;
  175. /** Returns a colour that's the same colour as this one, but with a modified alpha value.
  176. The new colour's alpha will be this object's alpha multiplied by the value passed-in.
  177. */
  178. Colour withMultipliedAlpha (float alphaMultiplier) const noexcept;
  179. //==============================================================================
  180. /** Returns a colour that is the result of alpha-compositing a new colour over this one.
  181. If the foreground colour is semi-transparent, it is blended onto this colour accordingly.
  182. */
  183. Colour overlaidWith (Colour foregroundColour) const noexcept;
  184. /** Returns a colour that lies somewhere between this one and another.
  185. If amountOfOther is zero, the result is 100% this colour, if amountOfOther
  186. is 1.0, the result is 100% of the other colour.
  187. */
  188. Colour interpolatedWith (Colour other, float proportionOfOther) const noexcept;
  189. //==============================================================================
  190. /** Returns the colour's hue component.
  191. The value returned is in the range 0.0 to 1.0
  192. */
  193. float getHue() const noexcept;
  194. /** Returns the colour's saturation component.
  195. The value returned is in the range 0.0 to 1.0
  196. */
  197. float getSaturation() const noexcept;
  198. /** Returns the colour's saturation component as represented in the HSL colour space.
  199. The value returned is in the range 0.0 to 1.0
  200. */
  201. float getSaturationHSL() const noexcept;
  202. /** Returns the colour's brightness component.
  203. The value returned is in the range 0.0 to 1.0
  204. */
  205. float getBrightness() const noexcept;
  206. /** Returns the colour's lightness component.
  207. The value returned is in the range 0.0 to 1.0
  208. */
  209. float getLightness() const noexcept;
  210. /** Returns a skewed brightness value, adjusted to better reflect the way the human
  211. eye responds to different colour channels. This makes it better than getBrightness()
  212. for comparing differences in brightness.
  213. */
  214. float getPerceivedBrightness() const noexcept;
  215. /** Returns the colour's hue, saturation and brightness components all at once.
  216. The values returned are in the range 0.0 to 1.0
  217. */
  218. void getHSB (float& hue,
  219. float& saturation,
  220. float& brightness) const noexcept;
  221. /** Returns the colour's hue, saturation and lightness components all at once.
  222. The values returned are in the range 0.0 to 1.0
  223. */
  224. void getHSL (float& hue,
  225. float& saturation,
  226. float& lightness) const noexcept;
  227. //==============================================================================
  228. /** Returns a copy of this colour with a different hue. */
  229. Colour withHue (float newHue) const noexcept;
  230. /** Returns a copy of this colour with a different saturation. */
  231. Colour withSaturation (float newSaturation) const noexcept;
  232. /** Returns a copy of this colour with a different saturation in the HSL colour space. */
  233. Colour withSaturationHSL (float newSaturation) const noexcept;
  234. /** Returns a copy of this colour with a different brightness.
  235. @see brighter, darker, withMultipliedBrightness
  236. */
  237. Colour withBrightness (float newBrightness) const noexcept;
  238. /** Returns a copy of this colour with a different lightness.
  239. @see lighter, darker, withMultipliedLightness
  240. */
  241. Colour withLightness (float newLightness) const noexcept;
  242. /** Returns a copy of this colour with its hue rotated.
  243. The new colour's hue is ((this->getHue() + amountToRotate) % 1.0)
  244. @see brighter, darker, withMultipliedBrightness
  245. */
  246. Colour withRotatedHue (float amountToRotate) const noexcept;
  247. /** Returns a copy of this colour with its saturation multiplied by the given value.
  248. The new colour's saturation is (this->getSaturation() * multiplier)
  249. (the result is clipped to legal limits).
  250. */
  251. Colour withMultipliedSaturation (float multiplier) const noexcept;
  252. /** Returns a copy of this colour with its saturation multiplied by the given value.
  253. The new colour's saturation is (this->getSaturation() * multiplier)
  254. (the result is clipped to legal limits).
  255. This will be in the HSL colour space.
  256. */
  257. Colour withMultipliedSaturationHSL (float multiplier) const noexcept;
  258. /** Returns a copy of this colour with its brightness multiplied by the given value.
  259. The new colour's brightness is (this->getBrightness() * multiplier)
  260. (the result is clipped to legal limits).
  261. */
  262. Colour withMultipliedBrightness (float amount) const noexcept;
  263. /** Returns a copy of this colour with its lightness multiplied by the given value.
  264. The new colour's lightness is (this->lightness() * multiplier)
  265. (the result is clipped to legal limits).
  266. */
  267. Colour withMultipliedLightness (float amount) const noexcept;
  268. //==============================================================================
  269. /** Returns a brighter version of this colour.
  270. @param amountBrighter how much brighter to make it - a value from 0 to 1.0 where 0 is
  271. unchanged, and higher values make it brighter
  272. @see withMultipliedBrightness
  273. */
  274. Colour brighter (float amountBrighter = 0.4f) const noexcept;
  275. /** Returns a darker version of this colour.
  276. @param amountDarker how much darker to make it - a value from 0 to 1.0 where 0 is
  277. unchanged, and higher values make it darker
  278. @see withMultipliedBrightness
  279. */
  280. Colour darker (float amountDarker = 0.4f) const noexcept;
  281. //==============================================================================
  282. /** Returns a colour that will be clearly visible against this colour.
  283. The amount parameter indicates how contrasting the new colour should
  284. be, so e.g. Colours::black.contrasting (0.1f) will return a colour
  285. that's just a little bit lighter; Colours::black.contrasting (1.0f) will
  286. return white; Colours::white.contrasting (1.0f) will return black, etc.
  287. */
  288. Colour contrasting (float amount = 1.0f) const noexcept;
  289. /** Returns a colour that is as close as possible to a target colour whilst
  290. still being in contrast to this one.
  291. The colour that is returned will be the targetColour, but with its luminosity
  292. nudged up or down so that it differs from the luminosity of this colour
  293. by at least the amount specified by minLuminosityDiff.
  294. */
  295. Colour contrasting (Colour targetColour, float minLuminosityDiff) const noexcept;
  296. /** Returns a colour that contrasts against two colours.
  297. Looks for a colour that contrasts with both of the colours passed-in.
  298. Handy for things like choosing a highlight colour in text editors, etc.
  299. */
  300. static Colour contrasting (Colour colour1,
  301. Colour colour2) noexcept;
  302. //==============================================================================
  303. /** Returns an opaque shade of grey.
  304. @param brightness the level of grey to return - 0 is black, 1.0 is white
  305. */
  306. static Colour greyLevel (float brightness) noexcept;
  307. //==============================================================================
  308. /** Returns a stringified version of this colour.
  309. The string can be turned back into a colour using the fromString() method.
  310. */
  311. String toString() const;
  312. /** Reads the colour from a string that was created with toString(). */
  313. static Colour fromString (StringRef encodedColourString);
  314. /** Returns the colour as a hex string in the form RRGGBB or AARRGGBB. */
  315. String toDisplayString (bool includeAlphaValue) const;
  316. private:
  317. //==============================================================================
  318. PixelARGB argb { 0, 0, 0, 0 };
  319. };
  320. } // namespace juce