LMMS
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
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 && ! approximatelyEqual (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
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{
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{
267 Colour hslColour;
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.getInARGBMaskOrder();
304}
305
306//==============================================================================
308{
309 return getAlpha() == 0;
310}
311
313{
314 return getAlpha() == 0xff;
315}
316
317Colour Colour::withAlpha (uint8 newAlpha) const noexcept
318{
319 PixelARGB newCol (argb);
320 newCol.setAlpha (newAlpha);
321 return Colour (newCol);
322}
323
324Colour Colour::withAlpha (float newAlpha) const noexcept
325{
326 jassert (newAlpha >= 0 && newAlpha <= 1.0f);
327
328 PixelARGB newCol (argb);
329 newCol.setAlpha (ColourHelpers::floatToUInt8 (newAlpha));
330 return Colour (newCol);
331}
332
333Colour Colour::withMultipliedAlpha (float alphaMultiplier) const noexcept
334{
335 jassert (alphaMultiplier >= 0);
336
337 PixelARGB newCol (argb);
338 newCol.setAlpha ((uint8) jmin (0xff, roundToInt (alphaMultiplier * newCol.getAlpha())));
339 return Colour (newCol);
340}
341
342//==============================================================================
344{
345 auto destAlpha = getAlpha();
346
347 if (destAlpha <= 0)
348 return src;
349
350 auto invA = 0xff - (int) src.getAlpha();
351 auto resA = 0xff - (((0xff - destAlpha) * invA) >> 8);
352
353 if (resA <= 0)
354 return *this;
355
356 auto da = (invA * destAlpha) / resA;
357
358 return Colour ((uint8) (src.getRed() + ((((int) getRed() - src.getRed()) * da) >> 8)),
359 (uint8) (src.getGreen() + ((((int) getGreen() - src.getGreen()) * da) >> 8)),
360 (uint8) (src.getBlue() + ((((int) getBlue() - src.getBlue()) * da) >> 8)),
361 (uint8) resA);
362}
363
364Colour Colour::interpolatedWith (Colour other, float proportionOfOther) const noexcept
365{
366 if (proportionOfOther <= 0)
367 return *this;
368
369 if (proportionOfOther >= 1.0f)
370 return other;
371
372 PixelARGB c1 (getPixelARGB());
373 PixelARGB c2 (other.getPixelARGB());
374 c1.tween (c2, (uint32) roundToInt (proportionOfOther * 255.0f));
375 c1.unpremultiply();
376
377 return Colour (c1);
378}
379
380//==============================================================================
381float Colour::getFloatRed() const noexcept { return getRed() / 255.0f; }
382float Colour::getFloatGreen() const noexcept { return getGreen() / 255.0f; }
383float Colour::getFloatBlue() const noexcept { return getBlue() / 255.0f; }
384float Colour::getFloatAlpha() const noexcept { return getAlpha() / 255.0f; }
385
386//==============================================================================
387void Colour::getHSB (float& h, float& s, float& v) const noexcept
388{
389 ColourHelpers::HSB hsb (*this);
390 h = hsb.hue;
391 s = hsb.saturation;
392 v = hsb.brightness;
393}
394
395void Colour::getHSL (float& h, float& s, float& l) const noexcept
396{
397 ColourHelpers::HSL hsl (*this);
398 h = hsl.hue;
399 s = hsl.saturation;
400 l = hsl.lightness;
401}
402
406
409
410Colour Colour::withHue (float h) const noexcept { ColourHelpers::HSB hsb (*this); hsb.hue = h; return hsb.toColour (*this); }
411Colour Colour::withSaturation (float s) const noexcept { ColourHelpers::HSB hsb (*this); hsb.saturation = s; return hsb.toColour (*this); }
412Colour Colour::withBrightness (float v) const noexcept { ColourHelpers::HSB hsb (*this); hsb.brightness = v; return hsb.toColour (*this); }
413
414Colour Colour::withSaturationHSL (float s) const noexcept { ColourHelpers::HSL hsl (*this); hsl.saturation = s; return hsl.toColour (*this); }
415Colour Colour::withLightness (float l) const noexcept { ColourHelpers::HSL hsl (*this); hsl.lightness = l; return hsl.toColour (*this); }
416
418{
419 return std::sqrt (0.241f * square (getFloatRed())
420 + 0.691f * square (getFloatGreen())
421 + 0.068f * square (getFloatBlue()));
422}
423
424//==============================================================================
425Colour Colour::withRotatedHue (float amountToRotate) const noexcept
426{
427 ColourHelpers::HSB hsb (*this);
428 hsb.hue += amountToRotate;
429 return hsb.toColour (*this);
430}
431
432Colour Colour::withMultipliedSaturation (float amount) const noexcept
433{
434 ColourHelpers::HSB hsb (*this);
435 hsb.saturation = jmin (1.0f, hsb.saturation * amount);
436 return hsb.toColour (*this);
437}
438
439Colour Colour::withMultipliedSaturationHSL (float amount) const noexcept
440{
441 ColourHelpers::HSL hsl (*this);
442 hsl.saturation = jmin (1.0f, hsl.saturation * amount);
443 return hsl.toColour (*this);
444}
445
446Colour Colour::withMultipliedBrightness (float amount) const noexcept
447{
448 ColourHelpers::HSB hsb (*this);
449 hsb.brightness = jmin (1.0f, hsb.brightness * amount);
450 return hsb.toColour (*this);
451}
452
453Colour Colour::withMultipliedLightness (float amount) const noexcept
454{
455 ColourHelpers::HSL hsl (*this);
456 hsl.lightness = jmin (1.0f, hsl.lightness * amount);
457 return hsl.toColour (*this);
458}
459
460//==============================================================================
461Colour Colour::brighter (float amount) const noexcept
462{
463 jassert (amount >= 0.0f);
464 amount = 1.0f / (1.0f + amount);
465
466 return Colour ((uint8) (255 - (amount * (255 - getRed()))),
467 (uint8) (255 - (amount * (255 - getGreen()))),
468 (uint8) (255 - (amount * (255 - getBlue()))),
469 getAlpha());
470}
471
472Colour Colour::darker (float amount) const noexcept
473{
474 jassert (amount >= 0.0f);
475 amount = 1.0f / (1.0f + amount);
476
477 return Colour ((uint8) (amount * getRed()),
478 (uint8) (amount * getGreen()),
479 (uint8) (amount * getBlue()),
480 getAlpha());
481}
482
483//==============================================================================
484Colour Colour::greyLevel (float brightness) noexcept
485{
486 auto level = ColourHelpers::floatToUInt8 (brightness);
487 return Colour (level, level, level);
488}
489
490//==============================================================================
491Colour Colour::contrasting (float amount) const noexcept
492{
493 return overlaidWith ((getPerceivedBrightness() >= 0.5f
495 : Colours::white).withAlpha (amount));
496}
497
498Colour Colour::contrasting (Colour target, float minContrast) const noexcept
499{
500 ColourHelpers::YIQ bg (*this);
501 ColourHelpers::YIQ fg (target);
502
503 if (std::abs (bg.y - fg.y) >= minContrast)
504 return target;
505
506 auto y1 = jmax (0.0f, bg.y - minContrast);
507 auto y2 = jmin (1.0f, bg.y + minContrast);
508 fg.y = (std::abs (y1 - bg.y) > std::abs (y2 - bg.y)) ? y1 : y2;
509
510 return fg.toColour();
511}
512
514 Colour colour2) noexcept
515{
516 auto b1 = colour1.getPerceivedBrightness();
517 auto b2 = colour2.getPerceivedBrightness();
518 float best = 0.0f, bestDist = 0.0f;
519
520 for (float i = 0.0f; i < 1.0f; i += 0.02f)
521 {
522 auto d1 = std::abs (i - b1);
523 auto d2 = std::abs (i - b2);
524 auto dist = jmin (d1, d2, 1.0f - d1, 1.0f - d2);
525
526 if (dist > bestDist)
527 {
528 best = i;
529 bestDist = dist;
530 }
531 }
532
533 return colour1.overlaidWith (colour2.withMultipliedAlpha (0.5f))
534 .withBrightness (best);
535}
536
537//==============================================================================
539{
540 return String::toHexString ((int) argb.getInARGBMaskOrder());
541}
542
544{
545 return Colour (CharacterFunctions::HexParser<uint32>::parse (encodedColourString.text));
546}
547
548String Colour::toDisplayString (const bool includeAlphaValue) const
549{
550 return String::toHexString ((int) (argb.getInARGBMaskOrder() & (includeAlphaValue ? 0xffffffff : 0xffffff)))
551 .paddedLeft ('0', includeAlphaValue ? 8 : 6)
552 .toUpperCase();
553}
554
555
556//==============================================================================
557//==============================================================================
558#if JUCE_UNIT_TESTS
559
560class ColourTests : public UnitTest
561{
562public:
563 ColourTests()
564 : UnitTest ("Colour", UnitTestCategories::graphics)
565 {}
566
567 void runTest() override
568 {
569 auto testColour = [this] (Colour colour,
570 uint8 expectedRed, uint8 expectedGreen, uint8 expectedBlue,
571 uint8 expectedAlpha = 255, float expectedFloatAlpha = 1.0f)
572 {
573 expectEquals (colour.getRed(), expectedRed);
574 expectEquals (colour.getGreen(), expectedGreen);
575 expectEquals (colour.getBlue(), expectedBlue);
576 expectEquals (colour.getAlpha(), expectedAlpha);
577 expectEquals (colour.getFloatAlpha(), expectedFloatAlpha);
578 };
579
580 beginTest ("Constructors");
581 {
582 Colour c1;
583 testColour (c1, (uint8) 0, (uint8) 0, (uint8) 0, (uint8) 0, 0.0f);
584
585 Colour c2 ((uint32) 0);
586 testColour (c2, (uint8) 0, (uint8) 0, (uint8) 0, (uint8) 0, 0.0f);
587
588 Colour c3 ((uint32) 0xffffffff);
589 testColour (c3, (uint8) 255, (uint8) 255, (uint8) 255, (uint8) 255, 1.0f);
590
591 Colour c4 (0, 0, 0);
592 testColour (c4, (uint8) 0, (uint8) 0, (uint8) 0, (uint8) 255, 1.0f);
593
594 Colour c5 (255, 255, 255);
595 testColour (c5, (uint8) 255, (uint8) 255, (uint8) 255, (uint8) 255, 1.0f);
596
597 Colour c6 ((uint8) 0, (uint8) 0, (uint8) 0, (uint8) 0);
598 testColour (c6, (uint8) 0, (uint8) 0, (uint8) 0, (uint8) 0, 0.0f);
599
600 Colour c7 ((uint8) 255, (uint8) 255, (uint8) 255, (uint8) 255);
601 testColour (c7, (uint8) 255, (uint8) 255, (uint8) 255, (uint8) 255, 1.0f);
602
603 Colour c8 ((uint8) 0, (uint8) 0, (uint8) 0, 0.0f);
604 testColour (c8, (uint8) 0, (uint8) 0, (uint8) 0, (uint8) 0, 0.0f);
605
606 Colour c9 ((uint8) 255, (uint8) 255, (uint8) 255, 1.0f);
607 testColour (c9, (uint8) 255, (uint8) 255, (uint8) 255, (uint8) 255, 1.0f);
608 }
609
610 beginTest ("HSV");
611 {
612 // black
613 testColour (Colour::fromHSV (0.0f, 0.0f, 0.0f, 1.0f), 0, 0, 0);
614 // white
615 testColour (Colour::fromHSV (0.0f, 0.0f, 1.0f, 1.0f), 255, 255, 255);
616 // red
617 testColour (Colour::fromHSV (0.0f, 1.0f, 1.0f, 1.0f), 255, 0, 0);
618 testColour (Colour::fromHSV (1.0f, 1.0f, 1.0f, 1.0f), 255, 0, 0);
619 // lime
620 testColour (Colour::fromHSV (120 / 360.0f, 1.0f, 1.0f, 1.0f), 0, 255, 0);
621 // blue
622 testColour (Colour::fromHSV (240 / 360.0f, 1.0f, 1.0f, 1.0f), 0, 0, 255);
623 // yellow
624 testColour (Colour::fromHSV (60 / 360.0f, 1.0f, 1.0f, 1.0f), 255, 255, 0);
625 // cyan
626 testColour (Colour::fromHSV (180 / 360.0f, 1.0f, 1.0f, 1.0f), 0, 255, 255);
627 // magenta
628 testColour (Colour::fromHSV (300 / 360.0f, 1.0f, 1.0f, 1.0f), 255, 0, 255);
629 // silver
630 testColour (Colour::fromHSV (0.0f, 0.0f, 0.75f, 1.0f), 191, 191, 191);
631 // grey
632 testColour (Colour::fromHSV (0.0f, 0.0f, 0.5f, 1.0f), 128, 128, 128);
633 // maroon
634 testColour (Colour::fromHSV (0.0f, 1.0f, 0.5f, 1.0f), 128, 0, 0);
635 // olive
636 testColour (Colour::fromHSV (60 / 360.0f, 1.0f, 0.5f, 1.0f), 128, 128, 0);
637 // green
638 testColour (Colour::fromHSV (120 / 360.0f, 1.0f, 0.5f, 1.0f), 0, 128, 0);
639 // purple
640 testColour (Colour::fromHSV (300 / 360.0f, 1.0f, 0.5f, 1.0f), 128, 0, 128);
641 // teal
642 testColour (Colour::fromHSV (180 / 360.0f, 1.0f, 0.5f, 1.0f), 0, 128, 128);
643 // navy
644 testColour (Colour::fromHSV (240 / 360.0f, 1.0f, 0.5f, 1.0f), 0, 0, 128);
645 }
646
647 beginTest ("HSL");
648 {
649 // black
650 testColour (Colour::fromHSL (0.0f, 0.0f, 0.0f, 1.0f), 0, 0, 0);
651 // white
652 testColour (Colour::fromHSL (0.0f, 0.0f, 1.0f, 1.0f), 255, 255, 255);
653 // red
654 testColour (Colour::fromHSL (0.0f, 1.0f, 0.5f, 1.0f), 255, 0, 0);
655 testColour (Colour::fromHSL (1.0f, 1.0f, 0.5f, 1.0f), 255, 0, 0);
656 // lime
657 testColour (Colour::fromHSL (120 / 360.0f, 1.0f, 0.5f, 1.0f), 0, 255, 0);
658 // blue
659 testColour (Colour::fromHSL (240 / 360.0f, 1.0f, 0.5f, 1.0f), 0, 0, 255);
660 // yellow
661 testColour (Colour::fromHSL (60 / 360.0f, 1.0f, 0.5f, 1.0f), 255, 255, 0);
662 // cyan
663 testColour (Colour::fromHSL (180 / 360.0f, 1.0f, 0.5f, 1.0f), 0, 255, 255);
664 // magenta
665 testColour (Colour::fromHSL (300 / 360.0f, 1.0f, 0.5f, 1.0f), 255, 0, 255);
666 // silver
667 testColour (Colour::fromHSL (0.0f, 0.0f, 0.75f, 1.0f), 191, 191, 191);
668 // grey
669 testColour (Colour::fromHSL (0.0f, 0.0f, 0.5f, 1.0f), 128, 128, 128);
670 // maroon
671 testColour (Colour::fromHSL (0.0f, 1.0f, 0.25f, 1.0f), 128, 0, 0);
672 // olive
673 testColour (Colour::fromHSL (60 / 360.0f, 1.0f, 0.25f, 1.0f), 128, 128, 0);
674 // green
675 testColour (Colour::fromHSL (120 / 360.0f, 1.0f, 0.25f, 1.0f), 0, 128, 0);
676 // purple
677 testColour (Colour::fromHSL (300 / 360.0f, 1.0f, 0.25f, 1.0f), 128, 0, 128);
678 // teal
679 testColour (Colour::fromHSL (180 / 360.0f, 1.0f, 0.25f, 1.0f), 0, 128, 128);
680 // navy
681 testColour (Colour::fromHSL (240 / 360.0f, 1.0f, 0.25f, 1.0f), 0, 0, 128);
682 }
683
684 beginTest ("Modifiers");
685 {
686 Colour red (255, 0, 0);
687 testColour (red, 255, 0, 0);
688
689 testColour (red.withHue (120.0f / 360.0f), 0, 255, 0);
690 testColour (red.withSaturation (0.5f), 255, 128, 128);
691 testColour (red.withSaturationHSL (0.5f), 191, 64, 64);
692 testColour (red.withBrightness (0.5f), 128, 0, 0);
693 testColour (red.withLightness (1.0f), 255, 255, 255);
694 testColour (red.withRotatedHue (120.0f / 360.0f), 0, 255, 0);
695 testColour (red.withRotatedHue (480.0f / 360.0f), 0, 255, 0);
696 testColour (red.withRotatedHue (-240.0f / 360.0f), 0, 255, 0);
697 testColour (red.withRotatedHue (-600.0f / 360.0f), 0, 255, 0);
698 testColour (red.withMultipliedSaturation (0.0f), 255, 255, 255);
699 testColour (red.withMultipliedSaturationHSL (0.0f), 128, 128, 128);
700 testColour (red.withMultipliedBrightness (0.5f), 128, 0, 0);
701 testColour (red.withMultipliedLightness (2.0f), 255, 255, 255);
702 testColour (red.withMultipliedLightness (1.0f), 255, 0, 0);
703 testColour (red.withLightness (red.getLightness()), 255, 0, 0);
704 }
705 }
706};
707
708static ColourTests colourTests;
709
710#endif
711
712} // namespace juce
Type jmin(const Type a, const Type b)
Definition MathsFunctions.h:60
Type jmax(const Type a, const Type b)
Definition MathsFunctions.h:48
#define noexcept
Definition DistrhoDefines.h:72
uint8_t uint8
Definition basics.h:86
uint32_t uint32
Definition basics.h:90
static String toHexString(int number)
Definition String.cpp:1830
Definition juce_Colour.h:38
uint8 getBlue() const noexcept
Definition juce_Colour.h:178
JUCE_NODISCARD Colour withSaturation(float newSaturation) const noexcept
Definition juce_Colour.cpp:411
Colour withAlpha(uint8 newAlpha) const noexcept
Definition juce_Colour.cpp:317
String toString() const
Definition juce_Colour.cpp:538
static Colour fromHSL(float hue, float saturation, float lightness, float alpha) noexcept
Definition juce_Colour.cpp:265
PixelARGB argb
Definition juce_Colour.h:413
JUCE_NODISCARD Colour withLightness(float newLightness) const noexcept
Definition juce_Colour.cpp:415
JUCE_NODISCARD Colour withMultipliedBrightness(float amount) const noexcept
Definition juce_Colour.cpp:446
float getFloatGreen() const noexcept
Definition juce_Colour.cpp:382
JUCE_NODISCARD Colour brighter(float amountBrighter=0.4f) const noexcept
Definition juce_Colour.cpp:461
float getSaturation() const noexcept
Definition juce_Colour.cpp:404
JUCE_NODISCARD Colour withMultipliedSaturation(float multiplier) const noexcept
Definition juce_Colour.cpp:432
static Colour fromRGBA(uint8 red, uint8 green, uint8 blue, uint8 alpha) noexcept
Definition juce_Colour.cpp:238
JUCE_NODISCARD Colour withHue(float newHue) const noexcept
Definition juce_Colour.cpp:410
JUCE_NODISCARD Colour darker(float amountDarker=0.4f) const noexcept
Definition juce_Colour.cpp:472
float getFloatBlue() const noexcept
Definition juce_Colour.cpp:383
float getSaturationHSL() const noexcept
Definition juce_Colour.cpp:407
float getBrightness() const noexcept
Definition juce_Colour.cpp:405
static JUCE_NODISCARD Colour greyLevel(float brightness) noexcept
Definition juce_Colour.cpp:484
static JUCE_NODISCARD Colour fromString(StringRef encodedColourString)
Definition juce_Colour.cpp:543
float getLightness() const noexcept
Definition juce_Colour.cpp:408
void getHSL(float &hue, float &saturation, float &lightness) const noexcept
Definition juce_Colour.cpp:395
JUCE_NODISCARD Colour withRotatedHue(float amountToRotate) const noexcept
Definition juce_Colour.cpp:425
Colour interpolatedWith(Colour other, float proportionOfOther) const noexcept
Definition juce_Colour.cpp:364
Colour withMultipliedAlpha(float alphaMultiplier) const noexcept
Definition juce_Colour.cpp:333
String toDisplayString(bool includeAlphaValue) const
Definition juce_Colour.cpp:548
const PixelARGB getPixelARGB() const noexcept
Definition juce_Colour.cpp:294
JUCE_NODISCARD Colour withSaturationHSL(float newSaturation) const noexcept
Definition juce_Colour.cpp:414
uint8 getGreen() const noexcept
Definition juce_Colour.h:173
bool isOpaque() const noexcept
Definition juce_Colour.cpp:312
JUCE_NODISCARD Colour withMultipliedSaturationHSL(float multiplier) const noexcept
Definition juce_Colour.cpp:439
uint32 getARGB() const noexcept
Definition juce_Colour.cpp:301
static Colour fromRGB(uint8 red, uint8 green, uint8 blue) noexcept
Definition juce_Colour.cpp:228
JUCE_NODISCARD Colour withBrightness(float newBrightness) const noexcept
Definition juce_Colour.cpp:412
void getHSB(float &hue, float &saturation, float &brightness) const noexcept
Definition juce_Colour.cpp:387
uint8 getRed() const noexcept
Definition juce_Colour.h:168
float getFloatRed() const noexcept
Definition juce_Colour.cpp:381
uint8 getAlpha() const noexcept
Definition juce_Colour.h:211
static Colour fromHSV(float hue, float saturation, float brightness, float alpha) noexcept
Definition juce_Colour.cpp:260
float getFloatAlpha() const noexcept
Definition juce_Colour.cpp:384
static Colour fromFloatRGBA(float red, float green, float blue, float alpha) noexcept
Definition juce_Colour.cpp:248
JUCE_NODISCARD Colour contrasting(float amount=1.0f) const noexcept
Definition juce_Colour.cpp:491
bool isTransparent() const noexcept
Definition juce_Colour.cpp:307
Colour()=default
JUCE_NODISCARD Colour withMultipliedLightness(float amount) const noexcept
Definition juce_Colour.cpp:453
float getHue() const noexcept
Definition juce_Colour.cpp:403
float getPerceivedBrightness() const noexcept
Definition juce_Colour.cpp:417
Colour overlaidWith(Colour foregroundColour) const noexcept
Definition juce_Colour.cpp:343
Definition juce_PixelFormats.h:59
forcedinline void tween(const Pixel &src, uint32 amount) noexcept
Definition juce_PixelFormats.h:184
forcedinline void setAlpha(uint8 newAlpha) noexcept
Definition juce_PixelFormats.h:201
forcedinline void unpremultiply() noexcept
Definition juce_PixelFormats.h:253
forcedinline uint8 getAlpha() const noexcept
Definition juce_PixelFormats.h:107
Definition juce_PixelFormats.h:600
Definition juce_PixelFormats.h:354
Definition juce_String.h:53
Definition juce_StringRef.h:62
String::CharPointerType text
Definition juce_StringRef.h:130
Definition juce_UnitTest.h:70
String toUpperCase() const
Definition String.cpp:1331
String paddedLeft(water_uchar padCharacter, int minimumLength) const
Definition String.cpp:1042
int * l
Definition inflate.c:1579
unsigned v[N_MAX]
Definition inflate.c:1584
int g
Definition inflate.c:1573
register unsigned i
Definition inflate.c:1575
unsigned s
Definition inflate.c:1555
unsigned x[BMAX+1]
Definition inflate.c:1586
unsigned f
Definition inflate.c:1572
static void c8(register WDL_FFT_COMPLEX *a)
Definition fft.c:314
static void c2(register WDL_FFT_COMPLEX *a)
Definition fft.c:270
static void c4(register WDL_FFT_COMPLEX *a)
Definition fft.c:283
#define jassert(expression)
Definition juce_Colour.cpp:30
static float getHue(Colour col)
Definition juce_Colour.cpp:36
static uint8 floatToUInt8(float n) noexcept
Definition juce_Colour.cpp:31
const Colour black
Definition juce_Colours.h:50
const Colour white
Definition juce_Colours.h:180
Definition juce_UnitTestCategories.h:27
Definition carla_juce.cpp:31
constexpr Type jmin(Type a, Type b)
Definition juce_MathsFunctions.h:106
bool approximatelyEqual(Type a, Type b) noexcept
Definition juce_MathsFunctions.h:324
unsigned int uint32
Definition juce_MathsFunctions.h:45
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Definition juce_MathsFunctions.h:262
constexpr NumericType square(NumericType n) noexcept
Definition juce_MathsFunctions.h:580
unsigned char uint8
Definition juce_MathsFunctions.h:37
int roundToInt(const FloatType value) noexcept
Definition juce_MathsFunctions.h:465
#define min(x, y)
Definition os.h:74
png_structrp int png_fixed_point red
Definition png.h:1083
static ResultType parse(CharPointerType t) noexcept
Definition juce_CharacterFunctions.h:497
Definition juce_Colour.cpp:133
HSB(Colour col) noexcept
Definition juce_Colour.cpp:134
static PixelARGB toRGB(float h, float s, float v, uint8 alpha) noexcept
Definition juce_Colour.cpp:159
float brightness
Definition juce_Colour.cpp:180
float saturation
Definition juce_Colour.cpp:180
float hue
Definition juce_Colour.cpp:180
Colour toColour(Colour original) const noexcept
Definition juce_Colour.cpp:154
Definition juce_Colour.cpp:70
float saturation
Definition juce_Colour.cpp:128
float hue
Definition juce_Colour.cpp:128
static PixelARGB toRGB(float h, float s, float l, uint8 alpha) noexcept
Definition juce_Colour.cpp:102
HSL(Colour col) noexcept
Definition juce_Colour.cpp:71
float lightness
Definition juce_Colour.cpp:128
Colour toColour(Colour original) const noexcept
Definition juce_Colour.cpp:97
Definition juce_Colour.cpp:185
float q
Definition juce_Colour.cpp:206
float y
Definition juce_Colour.cpp:206
Colour toColour() const noexcept
Definition juce_Colour.cpp:198
float alpha
Definition juce_Colour.cpp:206
YIQ(Colour c) noexcept
Definition juce_Colour.cpp:186
float i
Definition juce_Colour.cpp:206
int n
Definition crypt.c:458
uch * p
Definition crypt.c:594
return c
Definition crypt.c:175
int r
Definition crypt.c:458
uch h[RAND_HEAD_LEN]
Definition crypt.c:459
b
Definition crypt.c:628
typedef int(UZ_EXP MsgFn)()
#define const
Definition zconf.h:137