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.h 14KB

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