JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_Colour.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29namespace ColourHelpers
30{
31 static uint8 floatToUInt8 (float n) noexcept
32 {
33 return n <= 0.0f ? 0 : (n >= 1.0f ? 255 : (uint8) roundToInt (n * 255.0f));
34 }
35
36 static float getHue (Colour col)
37 {
38 auto r = (int) col.getRed();
39 auto g = (int) col.getGreen();
40 auto b = (int) col.getBlue();
41
42 auto hi = jmax (r, g, b);
43 auto lo = jmin (r, g, b);
44
45 float hue = 0.0f;
46
47 if (hi > 0 && ! exactlyEqual (hi, lo))
48 {
49 auto invDiff = 1.0f / (float) (hi - lo);
50
51 auto red = (float) (hi - r) * invDiff;
52 auto green = (float) (hi - g) * invDiff;
53 auto blue = (float) (hi - b) * invDiff;
54
55 if (r == hi) hue = blue - green;
56 else if (g == hi) hue = 2.0f + red - blue;
57 else hue = 4.0f + green - red;
58
59 hue *= 1.0f / 6.0f;
60
61 if (hue < 0.0f)
62 hue += 1.0f;
63 }
64
65 return hue;
66 }
67
68 //==============================================================================
69 struct HSL
70 {
71 HSL (Colour col) noexcept
72 {
73 auto r = (int) col.getRed();
74 auto g = (int) col.getGreen();
75 auto b = (int) col.getBlue();
76
77 auto hi = jmax (r, g, b);
78 auto lo = jmin (r, g, b);
79
80 if (hi < 0)
81 return;
82
83 lightness = ((float) (hi + lo) / 2.0f) / 255.0f;
84
85 if (lightness <= 0.0f)
86 return;
87
88 hue = getHue (col);
89
90 if (1.0f <= lightness)
91 return;
92
93 auto denominator = 1.0f - std::abs ((2.0f * lightness) - 1.0f);
94 saturation = ((float) (hi - lo) / 255.0f) / denominator;
95 }
96
97 Colour toColour (Colour original) const noexcept
98 {
99 return Colour::fromHSL (hue, saturation, lightness, original.getAlpha());
100 }
101
102 static PixelARGB toRGB (float h, float s, float l, uint8 alpha) noexcept
103 {
104 auto v = l < 0.5f ? l * (1.0f + s) : l + s - (l * s);
105
106 if (approximatelyEqual (v, 0.0f))
107 return PixelARGB (alpha, 0, 0, 0);
108
109 auto min = (2.0f * l) - v;
110 auto sv = (v - min) / v;
111
112 h = ((h - std::floor (h)) * 360.0f) / 60.0f;
113 auto f = h - std::floor (h);
114 auto vsf = v * sv * f;
115 auto mid1 = min + vsf;
116 auto mid2 = v - vsf;
117
118 if (h < 1.0f) return PixelARGB (alpha, floatToUInt8 (v), floatToUInt8 (mid1), floatToUInt8 (min));
119 else if (h < 2.0f) return PixelARGB (alpha, floatToUInt8 (mid2), floatToUInt8 (v), floatToUInt8 (min));
120 else if (h < 3.0f) return PixelARGB (alpha, floatToUInt8 (min), floatToUInt8 (v), floatToUInt8 (mid1));
121 else if (h < 4.0f) return PixelARGB (alpha, floatToUInt8 (min), floatToUInt8 (mid2), floatToUInt8 (v));
122 else if (h < 5.0f) return PixelARGB (alpha, floatToUInt8 (mid1), floatToUInt8 (min), floatToUInt8 (v));
123 else if (h < 6.0f) return PixelARGB (alpha, floatToUInt8 (v), floatToUInt8 (min), floatToUInt8 (mid2));
124
125 return PixelARGB (alpha, 0, 0, 0);
126 }
127
128 float hue = 0.0f, saturation = 0.0f, lightness = 0.0f;
129 };
130
131 //==============================================================================
132 struct HSB
133 {
134 HSB (Colour col) noexcept
135 {
136 auto r = (int) col.getRed();
137 auto g = (int) col.getGreen();
138 auto b = (int) col.getBlue();
139
140 auto hi = jmax (r, g, b);
141 auto lo = jmin (r, g, b);
142
143 if (hi > 0)
144 {
145 saturation = (float) (hi - lo) / (float) hi;
146
147 if (saturation > 0.0f)
148 hue = getHue (col);
149
150 brightness = (float) hi / 255.0f;
151 }
152 }
153
154 Colour toColour (Colour original) const noexcept
155 {
156 return Colour (hue, saturation, brightness, original.getAlpha());
157 }
158
159 static PixelARGB toRGB (float h, float s, float v, uint8 alpha) noexcept
160 {
161 v = jlimit (0.0f, 255.0f, v * 255.0f);
162 auto intV = (uint8) roundToInt (v);
163
164 if (s <= 0)
165 return PixelARGB (alpha, intV, intV, intV);
166
167 s = jmin (1.0f, s);
168 h = ((h - std::floor (h)) * 360.0f) / 60.0f;
169 auto f = h - std::floor (h);
170 auto x = (uint8) roundToInt (v * (1.0f - s));
171
172 if (h < 1.0f) return PixelARGB (alpha, intV, (uint8) roundToInt (v * (1.0f - (s * (1.0f - f)))), x);
173 if (h < 2.0f) return PixelARGB (alpha, (uint8) roundToInt (v * (1.0f - s * f)), intV, x);
174 if (h < 3.0f) return PixelARGB (alpha, x, intV, (uint8) roundToInt (v * (1.0f - (s * (1.0f - f)))));
175 if (h < 4.0f) return PixelARGB (alpha, x, (uint8) roundToInt (v * (1.0f - s * f)), intV);
176 if (h < 5.0f) return PixelARGB (alpha, (uint8) roundToInt (v * (1.0f - (s * (1.0f - f)))), x, intV);
177 return PixelARGB (alpha, intV, x, (uint8) roundToInt (v * (1.0f - s * f)));
178 }
179
180 float hue = 0.0f, saturation = 0.0f, brightness = 0.0f;
181 };
182
183 //==============================================================================
184 struct YIQ
185 {
186 YIQ (Colour c) noexcept
187 {
188 auto r = c.getFloatRed();
189 auto g = c.getFloatGreen();
190 auto b = c.getFloatBlue();
191
192 y = 0.2999f * r + 0.5870f * g + 0.1140f * b;
193 i = 0.5957f * r - 0.2744f * g - 0.3212f * b;
194 q = 0.2114f * r - 0.5225f * g - 0.3113f * b;
195 alpha = c.getFloatAlpha();
196 }
197
198 Colour toColour() const noexcept
199 {
200 return Colour::fromFloatRGBA (y + 0.9563f * i + 0.6210f * q,
201 y - 0.2721f * i - 0.6474f * q,
202 y - 1.1070f * i + 1.7046f * q,
203 alpha);
204 }
205
206 float y = 0.0f, i = 0.0f, q = 0.0f, alpha = 0.0f;
207 };
208}
209
210//==============================================================================
211bool Colour::operator== (const Colour& other) const noexcept { return argb.getNativeARGB() == other.argb.getNativeARGB(); }
212bool Colour::operator!= (const Colour& other) const noexcept { return argb.getNativeARGB() != other.argb.getNativeARGB(); }
213
214//==============================================================================
216 : argb (static_cast<uint8> ((col >> 24) & 0xff),
217 static_cast<uint8> ((col >> 16) & 0xff),
218 static_cast<uint8> ((col >> 8) & 0xff),
219 static_cast<uint8> (col & 0xff))
220{
221}
222
223Colour::Colour (uint8 red, uint8 green, uint8 blue) noexcept
224{
225 argb.setARGB (0xff, red, green, blue);
226}
227
228Colour Colour::fromRGB (uint8 red, uint8 green, uint8 blue) noexcept
229{
230 return Colour (red, green, blue);
231}
232
233Colour::Colour (uint8 red, uint8 green, uint8 blue, uint8 alpha) noexcept
234{
235 argb.setARGB (alpha, red, green, blue);
236}
237
238Colour Colour::fromRGBA (uint8 red, uint8 green, uint8 blue, uint8 alpha) noexcept
239{
240 return Colour (red, green, blue, alpha);
241}
242
243Colour::Colour (uint8 red, uint8 green, uint8 blue, float alpha) noexcept
244{
245 argb.setARGB (ColourHelpers::floatToUInt8 (alpha), red, green, blue);
246}
247
248Colour Colour::fromFloatRGBA (float red, float green, float blue, float alpha) noexcept
249{
250 return Colour (ColourHelpers::floatToUInt8 (red),
251 ColourHelpers::floatToUInt8 (green),
252 ColourHelpers::floatToUInt8 (blue), alpha);
253}
254
255Colour::Colour (float hue, float saturation, float brightness, float alpha) noexcept
256 : argb (ColourHelpers::HSB::toRGB (hue, saturation, brightness, ColourHelpers::floatToUInt8 (alpha)))
257{
258}
259
260Colour Colour::fromHSV (float hue, float saturation, float brightness, float alpha) noexcept
261{
262 return Colour (hue, saturation, brightness, alpha);
263}
264
265Colour Colour::fromHSL (float hue, float saturation, float lightness, float alpha) noexcept
266{
268 hslColour.argb = ColourHelpers::HSL::toRGB (hue, saturation, lightness, ColourHelpers::floatToUInt8 (alpha));
269
270 return hslColour;
271}
272
273Colour::Colour (float hue, float saturation, float brightness, uint8 alpha) noexcept
274 : argb (ColourHelpers::HSB::toRGB (hue, saturation, brightness, alpha))
275{
276}
277
279 : argb (argb_)
280{
281}
282
284 : argb (Colour (rgb.getInARGBMaskOrder()).argb)
285{
286}
287
289 : argb (Colour (alpha.getInARGBMaskOrder()).argb)
290{
291}
292
293//==============================================================================
295{
296 PixelARGB p (argb);
297 p.premultiply();
298 return p;
299}
300
302{
303 return argb;
304}
305
307{
308 return argb.getInARGBMaskOrder();
309}
310
311//==============================================================================
313{
314 return getAlpha() == 0;
315}
316
317bool Colour::isOpaque() const noexcept
318{
319 return getAlpha() == 0xff;
320}
321
323{
324 PixelARGB newCol (argb);
325 newCol.setAlpha (newAlpha);
326 return Colour (newCol);
327}
328
329Colour Colour::withAlpha (float newAlpha) const noexcept
330{
331 jassert (newAlpha >= 0 && newAlpha <= 1.0f);
332
333 PixelARGB newCol (argb);
334 newCol.setAlpha (ColourHelpers::floatToUInt8 (newAlpha));
335 return Colour (newCol);
336}
337
339{
341
342 PixelARGB newCol (argb);
343 newCol.setAlpha ((uint8) jmin (0xff, roundToInt (alphaMultiplier * newCol.getAlpha())));
344 return Colour (newCol);
345}
346
347//==============================================================================
349{
350 auto destAlpha = getAlpha();
351
352 if (destAlpha <= 0)
353 return src;
354
355 auto invA = 0xff - (int) src.getAlpha();
356 auto resA = 0xff - (((0xff - destAlpha) * invA) >> 8);
357
358 if (resA <= 0)
359 return *this;
360
361 auto da = (invA * destAlpha) / resA;
362
363 return Colour ((uint8) (src.getRed() + ((((int) getRed() - src.getRed()) * da) >> 8)),
364 (uint8) (src.getGreen() + ((((int) getGreen() - src.getGreen()) * da) >> 8)),
365 (uint8) (src.getBlue() + ((((int) getBlue() - src.getBlue()) * da) >> 8)),
366 (uint8) resA);
367}
368
370{
371 if (proportionOfOther <= 0)
372 return *this;
373
374 if (proportionOfOther >= 1.0f)
375 return other;
376
377 PixelARGB c1 (getPixelARGB());
378 PixelARGB c2 (other.getPixelARGB());
379 c1.tween (c2, (uint32) roundToInt (proportionOfOther * 255.0f));
380 c1.unpremultiply();
381
382 return Colour (c1);
383}
384
385//==============================================================================
386float Colour::getFloatRed() const noexcept { return getRed() / 255.0f; }
387float Colour::getFloatGreen() const noexcept { return getGreen() / 255.0f; }
388float Colour::getFloatBlue() const noexcept { return getBlue() / 255.0f; }
389float Colour::getFloatAlpha() const noexcept { return getAlpha() / 255.0f; }
390
391//==============================================================================
392void Colour::getHSB (float& h, float& s, float& v) const noexcept
393{
394 ColourHelpers::HSB hsb (*this);
395 h = hsb.hue;
396 s = hsb.saturation;
397 v = hsb.brightness;
398}
399
400void Colour::getHSL (float& h, float& s, float& l) const noexcept
401{
402 ColourHelpers::HSL hsl (*this);
403 h = hsl.hue;
404 s = hsl.saturation;
405 l = hsl.lightness;
406}
407
408float Colour::getHue() const noexcept { return ColourHelpers::HSB (*this).hue; }
409float Colour::getSaturation() const noexcept { return ColourHelpers::HSB (*this).saturation; }
410float Colour::getBrightness() const noexcept { return ColourHelpers::HSB (*this).brightness; }
411
412float Colour::getSaturationHSL() const noexcept { return ColourHelpers::HSL (*this).saturation; }
413float Colour::getLightness() const noexcept { return ColourHelpers::HSL (*this).lightness; }
414
415Colour Colour::withHue (float h) const noexcept { ColourHelpers::HSB hsb (*this); hsb.hue = h; return hsb.toColour (*this); }
416Colour Colour::withSaturation (float s) const noexcept { ColourHelpers::HSB hsb (*this); hsb.saturation = s; return hsb.toColour (*this); }
417Colour Colour::withBrightness (float v) const noexcept { ColourHelpers::HSB hsb (*this); hsb.brightness = v; return hsb.toColour (*this); }
418
419Colour Colour::withSaturationHSL (float s) const noexcept { ColourHelpers::HSL hsl (*this); hsl.saturation = s; return hsl.toColour (*this); }
420Colour Colour::withLightness (float l) const noexcept { ColourHelpers::HSL hsl (*this); hsl.lightness = l; return hsl.toColour (*this); }
421
423{
424 return std::sqrt (0.241f * square (getFloatRed())
425 + 0.691f * square (getFloatGreen())
426 + 0.068f * square (getFloatBlue()));
427}
428
429//==============================================================================
431{
432 ColourHelpers::HSB hsb (*this);
433 hsb.hue += amountToRotate;
434 return hsb.toColour (*this);
435}
436
438{
439 ColourHelpers::HSB hsb (*this);
440 hsb.saturation = jmin (1.0f, hsb.saturation * amount);
441 return hsb.toColour (*this);
442}
443
445{
446 ColourHelpers::HSL hsl (*this);
447 hsl.saturation = jmin (1.0f, hsl.saturation * amount);
448 return hsl.toColour (*this);
449}
450
452{
453 ColourHelpers::HSB hsb (*this);
454 hsb.brightness = jmin (1.0f, hsb.brightness * amount);
455 return hsb.toColour (*this);
456}
457
459{
460 ColourHelpers::HSL hsl (*this);
461 hsl.lightness = jmin (1.0f, hsl.lightness * amount);
462 return hsl.toColour (*this);
463}
464
465//==============================================================================
466Colour Colour::brighter (float amount) const noexcept
467{
468 jassert (amount >= 0.0f);
469 amount = 1.0f / (1.0f + amount);
470
471 return Colour ((uint8) (255 - (amount * (255 - getRed()))),
472 (uint8) (255 - (amount * (255 - getGreen()))),
473 (uint8) (255 - (amount * (255 - getBlue()))),
474 getAlpha());
475}
476
477Colour Colour::darker (float amount) const noexcept
478{
479 jassert (amount >= 0.0f);
480 amount = 1.0f / (1.0f + amount);
481
482 return Colour ((uint8) (amount * getRed()),
483 (uint8) (amount * getGreen()),
484 (uint8) (amount * getBlue()),
485 getAlpha());
486}
487
488//==============================================================================
489Colour Colour::greyLevel (float brightness) noexcept
490{
491 auto level = ColourHelpers::floatToUInt8 (brightness);
492 return Colour (level, level, level);
493}
494
495//==============================================================================
496Colour Colour::contrasting (float amount) const noexcept
497{
498 return overlaidWith ((getPerceivedBrightness() >= 0.5f
499 ? Colours::black
500 : Colours::white).withAlpha (amount));
501}
502
503Colour Colour::contrasting (Colour target, float minContrast) const noexcept
504{
505 ColourHelpers::YIQ bg (*this);
506 ColourHelpers::YIQ fg (target);
507
508 if (std::abs (bg.y - fg.y) >= minContrast)
509 return target;
510
511 auto y1 = jmax (0.0f, bg.y - minContrast);
512 auto y2 = jmin (1.0f, bg.y + minContrast);
513 fg.y = (std::abs (y1 - bg.y) > std::abs (y2 - bg.y)) ? y1 : y2;
514
515 return fg.toColour();
516}
517
519 Colour colour2) noexcept
520{
522 auto b2 = colour2.getPerceivedBrightness();
523 float best = 0.0f, bestDist = 0.0f;
524
525 for (float i = 0.0f; i < 1.0f; i += 0.02f)
526 {
527 auto d1 = std::abs (i - b1);
528 auto d2 = std::abs (i - b2);
529 auto dist = jmin (d1, d2, 1.0f - d1, 1.0f - d2);
530
531 if (dist > bestDist)
532 {
533 best = i;
534 bestDist = dist;
535 }
536 }
537
538 return colour1.overlaidWith (colour2.withMultipliedAlpha (0.5f))
539 .withBrightness (best);
540}
541
542//==============================================================================
544{
545 return String::toHexString ((int) argb.getInARGBMaskOrder());
546}
547
552
554{
555 return String::toHexString ((int) (argb.getInARGBMaskOrder() & (includeAlphaValue ? 0xffffffff : 0xffffff)))
556 .paddedLeft ('0', includeAlphaValue ? 8 : 6)
557 .toUpperCase();
558}
559
560
561//==============================================================================
562//==============================================================================
563#if JUCE_UNIT_TESTS
564
565class ColourTests final : public UnitTest
566{
567public:
570 {}
571
572 void runTest() override
573 {
574 auto testColour = [this] (Colour colour,
576 uint8 expectedAlpha = 255, float expectedFloatAlpha = 1.0f)
577 {
578 expectEquals (colour.getRed(), expectedRed);
579 expectEquals (colour.getGreen(), expectedGreen);
580 expectEquals (colour.getBlue(), expectedBlue);
581 expectEquals (colour.getAlpha(), expectedAlpha);
582 expectEquals (colour.getFloatAlpha(), expectedFloatAlpha);
583 };
584
585 beginTest ("Constructors");
586 {
587 Colour c1;
588 testColour (c1, (uint8) 0, (uint8) 0, (uint8) 0, (uint8) 0, 0.0f);
589
590 Colour c2 ((uint32) 0);
591 testColour (c2, (uint8) 0, (uint8) 0, (uint8) 0, (uint8) 0, 0.0f);
592
593 Colour c3 ((uint32) 0xffffffff);
594 testColour (c3, (uint8) 255, (uint8) 255, (uint8) 255, (uint8) 255, 1.0f);
595
596 Colour c4 (0, 0, 0);
597 testColour (c4, (uint8) 0, (uint8) 0, (uint8) 0, (uint8) 255, 1.0f);
598
599 Colour c5 (255, 255, 255);
600 testColour (c5, (uint8) 255, (uint8) 255, (uint8) 255, (uint8) 255, 1.0f);
601
602 Colour c6 ((uint8) 0, (uint8) 0, (uint8) 0, (uint8) 0);
603 testColour (c6, (uint8) 0, (uint8) 0, (uint8) 0, (uint8) 0, 0.0f);
604
605 Colour c7 ((uint8) 255, (uint8) 255, (uint8) 255, (uint8) 255);
606 testColour (c7, (uint8) 255, (uint8) 255, (uint8) 255, (uint8) 255, 1.0f);
607
608 Colour c8 ((uint8) 0, (uint8) 0, (uint8) 0, 0.0f);
609 testColour (c8, (uint8) 0, (uint8) 0, (uint8) 0, (uint8) 0, 0.0f);
610
611 Colour c9 ((uint8) 255, (uint8) 255, (uint8) 255, 1.0f);
612 testColour (c9, (uint8) 255, (uint8) 255, (uint8) 255, (uint8) 255, 1.0f);
613 }
614
615 beginTest ("HSV");
616 {
617 // black
618 testColour (Colour::fromHSV (0.0f, 0.0f, 0.0f, 1.0f), 0, 0, 0);
619 // white
620 testColour (Colour::fromHSV (0.0f, 0.0f, 1.0f, 1.0f), 255, 255, 255);
621 // red
622 testColour (Colour::fromHSV (0.0f, 1.0f, 1.0f, 1.0f), 255, 0, 0);
623 testColour (Colour::fromHSV (1.0f, 1.0f, 1.0f, 1.0f), 255, 0, 0);
624 // lime
625 testColour (Colour::fromHSV (120 / 360.0f, 1.0f, 1.0f, 1.0f), 0, 255, 0);
626 // blue
627 testColour (Colour::fromHSV (240 / 360.0f, 1.0f, 1.0f, 1.0f), 0, 0, 255);
628 // yellow
629 testColour (Colour::fromHSV (60 / 360.0f, 1.0f, 1.0f, 1.0f), 255, 255, 0);
630 // cyan
631 testColour (Colour::fromHSV (180 / 360.0f, 1.0f, 1.0f, 1.0f), 0, 255, 255);
632 // magenta
633 testColour (Colour::fromHSV (300 / 360.0f, 1.0f, 1.0f, 1.0f), 255, 0, 255);
634 // silver
635 testColour (Colour::fromHSV (0.0f, 0.0f, 0.75f, 1.0f), 191, 191, 191);
636 // grey
637 testColour (Colour::fromHSV (0.0f, 0.0f, 0.5f, 1.0f), 128, 128, 128);
638 // maroon
639 testColour (Colour::fromHSV (0.0f, 1.0f, 0.5f, 1.0f), 128, 0, 0);
640 // olive
641 testColour (Colour::fromHSV (60 / 360.0f, 1.0f, 0.5f, 1.0f), 128, 128, 0);
642 // green
643 testColour (Colour::fromHSV (120 / 360.0f, 1.0f, 0.5f, 1.0f), 0, 128, 0);
644 // purple
645 testColour (Colour::fromHSV (300 / 360.0f, 1.0f, 0.5f, 1.0f), 128, 0, 128);
646 // teal
647 testColour (Colour::fromHSV (180 / 360.0f, 1.0f, 0.5f, 1.0f), 0, 128, 128);
648 // navy
649 testColour (Colour::fromHSV (240 / 360.0f, 1.0f, 0.5f, 1.0f), 0, 0, 128);
650 }
651
652 beginTest ("HSL");
653 {
654 // black
655 testColour (Colour::fromHSL (0.0f, 0.0f, 0.0f, 1.0f), 0, 0, 0);
656 // white
657 testColour (Colour::fromHSL (0.0f, 0.0f, 1.0f, 1.0f), 255, 255, 255);
658 // red
659 testColour (Colour::fromHSL (0.0f, 1.0f, 0.5f, 1.0f), 255, 0, 0);
660 testColour (Colour::fromHSL (1.0f, 1.0f, 0.5f, 1.0f), 255, 0, 0);
661 // lime
662 testColour (Colour::fromHSL (120 / 360.0f, 1.0f, 0.5f, 1.0f), 0, 255, 0);
663 // blue
664 testColour (Colour::fromHSL (240 / 360.0f, 1.0f, 0.5f, 1.0f), 0, 0, 255);
665 // yellow
666 testColour (Colour::fromHSL (60 / 360.0f, 1.0f, 0.5f, 1.0f), 255, 255, 0);
667 // cyan
668 testColour (Colour::fromHSL (180 / 360.0f, 1.0f, 0.5f, 1.0f), 0, 255, 255);
669 // magenta
670 testColour (Colour::fromHSL (300 / 360.0f, 1.0f, 0.5f, 1.0f), 255, 0, 255);
671 // silver
672 testColour (Colour::fromHSL (0.0f, 0.0f, 0.75f, 1.0f), 191, 191, 191);
673 // grey
674 testColour (Colour::fromHSL (0.0f, 0.0f, 0.5f, 1.0f), 128, 128, 128);
675 // maroon
676 testColour (Colour::fromHSL (0.0f, 1.0f, 0.25f, 1.0f), 128, 0, 0);
677 // olive
678 testColour (Colour::fromHSL (60 / 360.0f, 1.0f, 0.25f, 1.0f), 128, 128, 0);
679 // green
680 testColour (Colour::fromHSL (120 / 360.0f, 1.0f, 0.25f, 1.0f), 0, 128, 0);
681 // purple
682 testColour (Colour::fromHSL (300 / 360.0f, 1.0f, 0.25f, 1.0f), 128, 0, 128);
683 // teal
684 testColour (Colour::fromHSL (180 / 360.0f, 1.0f, 0.25f, 1.0f), 0, 128, 128);
685 // navy
686 testColour (Colour::fromHSL (240 / 360.0f, 1.0f, 0.25f, 1.0f), 0, 0, 128);
687 }
688
689 beginTest ("Modifiers");
690 {
691 Colour red (255, 0, 0);
692 testColour (red, 255, 0, 0);
693
694 testColour (red.withHue (120.0f / 360.0f), 0, 255, 0);
695 testColour (red.withSaturation (0.5f), 255, 128, 128);
696 testColour (red.withSaturationHSL (0.5f), 191, 64, 64);
697 testColour (red.withBrightness (0.5f), 128, 0, 0);
698 testColour (red.withLightness (1.0f), 255, 255, 255);
699 testColour (red.withRotatedHue (120.0f / 360.0f), 0, 255, 0);
700 testColour (red.withRotatedHue (480.0f / 360.0f), 0, 255, 0);
701 testColour (red.withRotatedHue (-240.0f / 360.0f), 0, 255, 0);
702 testColour (red.withRotatedHue (-600.0f / 360.0f), 0, 255, 0);
703 testColour (red.withMultipliedSaturation (0.0f), 255, 255, 255);
704 testColour (red.withMultipliedSaturationHSL (0.0f), 128, 128, 128);
705 testColour (red.withMultipliedBrightness (0.5f), 128, 0, 0);
706 testColour (red.withMultipliedLightness (2.0f), 255, 255, 255);
707 testColour (red.withMultipliedLightness (1.0f), 255, 0, 0);
708 testColour (red.withLightness (red.getLightness()), 255, 0, 0);
709 }
710 }
711};
712
714
715#endif
716
717} // namespace juce
Represents a colour, also including a transparency value.
Definition juce_Colour.h:38
uint8 getBlue() const noexcept
Returns the blue component of this colour.
Colour withSaturation(float newSaturation) const noexcept
Returns a copy of this colour with a different saturation.
Colour withAlpha(uint8 newAlpha) const noexcept
Returns a colour that's the same colour as this one, but with a new alpha value.
String toString() const
Returns a stringified version of this colour.
static Colour fromHSL(float hue, float saturation, float lightness, float alpha) noexcept
Creates a colour using floating point hue, saturation, lightness and alpha values.
Colour withLightness(float newLightness) const noexcept
Returns a copy of this colour with a different lightness.
Colour withMultipliedBrightness(float amount) const noexcept
Returns a copy of this colour with its brightness multiplied by the given value.
float getFloatGreen() const noexcept
Returns the green component of this colour as a floating point value.
Colour brighter(float amountBrighter=0.4f) const noexcept
Returns a brighter version of this colour.
float getSaturation() const noexcept
Returns the colour's saturation component.
Colour withMultipliedSaturation(float multiplier) const noexcept
Returns a copy of this colour with its saturation multiplied by the given value.
static Colour fromRGBA(uint8 red, uint8 green, uint8 blue, uint8 alpha) noexcept
Creates a colour using 8-bit red, green, blue and alpha values.
Colour withHue(float newHue) const noexcept
Returns a copy of this colour with a different hue.
Colour darker(float amountDarker=0.4f) const noexcept
Returns a darker version of this colour.
float getFloatBlue() const noexcept
Returns the blue component of this colour as a floating point value.
float getSaturationHSL() const noexcept
Returns the colour's saturation component as represented in the HSL colour space.
float getBrightness() const noexcept
Returns the colour's brightness component.
static Colour greyLevel(float brightness) noexcept
Returns an opaque shade of grey.
static Colour fromString(StringRef encodedColourString)
Reads the colour from a string that was created with toString().
float getLightness() const noexcept
Returns the colour's lightness component.
void getHSL(float &hue, float &saturation, float &lightness) const noexcept
Returns the colour's hue, saturation and lightness components all at once.
PixelARGB getPixelARGB() const noexcept
Returns a premultiplied ARGB pixel object that represents this colour.
Colour withRotatedHue(float amountToRotate) const noexcept
Returns a copy of this colour with its hue rotated.
Colour interpolatedWith(Colour other, float proportionOfOther) const noexcept
Returns a colour that lies somewhere between this one and another.
Colour withMultipliedAlpha(float alphaMultiplier) const noexcept
Returns a colour that's the same colour as this one, but with a modified alpha value.
String toDisplayString(bool includeAlphaValue) const
Returns the colour as a hex string in the form RRGGBB or AARRGGBB.
Colour withSaturationHSL(float newSaturation) const noexcept
Returns a copy of this colour with a different saturation in the HSL colour space.
uint8 getGreen() const noexcept
Returns the green component of this colour.
bool isOpaque() const noexcept
Returns true if this colour is completely opaque.
Colour withMultipliedSaturationHSL(float multiplier) const noexcept
Returns a copy of this colour with its saturation multiplied by the given value.
uint32 getARGB() const noexcept
Returns a 32-bit integer that represents this colour.
static Colour fromRGB(uint8 red, uint8 green, uint8 blue) noexcept
Creates an opaque colour using 8-bit red, green and blue values.
bool operator==(const Colour &other) const noexcept
Compares two colours.
Colour withBrightness(float newBrightness) const noexcept
Returns a copy of this colour with a different brightness.
void getHSB(float &hue, float &saturation, float &brightness) const noexcept
Returns the colour's hue, saturation and brightness components all at once.
PixelARGB getNonPremultipliedPixelARGB() const noexcept
Returns an ARGB pixel object that represents this colour.
uint8 getRed() const noexcept
Returns the red component of this colour.
float getFloatRed() const noexcept
Returns the red component of this colour as a floating point value.
uint8 getAlpha() const noexcept
Returns the colour's alpha (opacity).
static Colour fromHSV(float hue, float saturation, float brightness, float alpha) noexcept
Creates a colour using floating point hue, saturation, brightness and alpha values.
float getFloatAlpha() const noexcept
Returns the colour's alpha (opacity) as a floating point value.
static Colour fromFloatRGBA(float red, float green, float blue, float alpha) noexcept
Creates a colour using floating point red, green, blue and alpha values.
Colour contrasting(float amount=1.0f) const noexcept
Returns a colour that will be clearly visible against this colour.
bool isTransparent() const noexcept
Returns true if this colour is completely transparent.
bool operator!=(const Colour &other) const noexcept
Compares two colours.
Colour()=default
Creates a transparent black colour.
Colour withMultipliedLightness(float amount) const noexcept
Returns a copy of this colour with its lightness multiplied by the given value.
float getHue() const noexcept
Returns the colour's hue component.
float getPerceivedBrightness() const noexcept
Returns a skewed brightness value, adjusted to better reflect the way the human eye responds to diffe...
Colour overlaidWith(Colour foregroundColour) const noexcept
Returns a colour that is the result of alpha-compositing a new colour over this one.
Represents a 32-bit INTERNAL pixel with premultiplied alpha, and can perform compositing operations w...
forcedinline uint32 getInARGBMaskOrder() const noexcept
Returns a uint32 which will be in argb order as if constructed with the following mask operation ((al...
forcedinline void premultiply() noexcept
Premultiplies the pixel's RGB values by its alpha.
Represents an 8-bit single-channel pixel, and can perform compositing operations on it.
Represents a 24-bit RGB pixel, and can perform compositing operations on it.
A simple class for holding temporary references to a string literal or String.
The JUCE String class!
Definition juce_String.h:53
static String toHexString(IntegerType number)
Returns a string representing this numeric value in hexadecimal.
This is a base class for classes that perform a unit test.
T floor(T... args)
#define jassert(expression)
Platform-independent assertion macro.
typedef int
typedef float
JUCE Namespace.
constexpr bool approximatelyEqual(Type a, Type b, Tolerance< Type > tolerance=Tolerance< Type >{} .withAbsolute(std::numeric_limits< Type >::min()) .withRelative(std::numeric_limits< Type >::epsilon()))
Returns true if the two floating-point numbers are approximately equal.
constexpr Type jmin(Type a, Type b)
Returns the smaller of two values.
constexpr bool exactlyEqual(Type a, Type b)
Equivalent to operator==, but suppresses float-equality warnings.
constexpr Type jmax(Type a, Type b)
Returns the larger of two values.
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Constrains a value to keep it within a given range.
constexpr NumericType square(NumericType n) noexcept
Returns the square of its argument.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
unsigned int uint32
A platform-independent 32-bit unsigned integer type.
unsigned char uint8
A platform-independent 8-bit unsigned integer type.
int roundToInt(const FloatType value) noexcept
Fast floating-point-to-integer conversion.
T sqrt(T... args)
Parses a character string, to read a hexadecimal value.
y1