Skip to content

Commit 68f7ed4

Browse files
Colour: Prevent an integer alpha value being processed as a float
1 parent 4212720 commit 68f7ed4

File tree

2 files changed

+83
-16
lines changed

2 files changed

+83
-16
lines changed

modules/juce_graphics/colour/juce_Colour.cpp

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,20 @@ Colour Colour::fromHSV (float hue, float saturation, float brightness, float alp
271271
return Colour (hue, saturation, brightness, alpha);
272272
}
273273

274+
Colour Colour::fromHSV (float hue, float saturation, float brightness, uint8 alpha) noexcept
275+
{
276+
return Colour (hue, saturation, brightness, alpha);
277+
}
278+
274279
Colour Colour::fromHSL (float hue, float saturation, float lightness, float alpha) noexcept
280+
{
281+
return fromHSL (hue, saturation, lightness, ColourHelpers::floatToUInt8 (alpha));
282+
}
283+
284+
Colour Colour::fromHSL (float hue, float saturation, float lightness, uint8 alpha) noexcept
275285
{
276286
Colour hslColour;
277-
hslColour.argb = ColourHelpers::HSL::toRGB (hue, saturation, lightness, ColourHelpers::floatToUInt8 (alpha));
287+
hslColour.argb = ColourHelpers::HSL::toRGB (hue, saturation, lightness, alpha);
278288

279289
return hslColour;
280290
}
@@ -582,117 +592,150 @@ class ColourTests final : public UnitTest
582592
{
583593
auto testColour = [this] (Colour colour,
584594
uint8 expectedRed, uint8 expectedGreen, uint8 expectedBlue,
585-
uint8 expectedAlpha = 255, float expectedFloatAlpha = 1.0f)
595+
uint8 expectedAlpha = 255)
586596
{
587-
expectEquals (colour.getRed(), expectedRed);
588-
expectEquals (colour.getGreen(), expectedGreen);
589-
expectEquals (colour.getBlue(), expectedBlue);
590-
expectEquals (colour.getAlpha(), expectedAlpha);
591-
expectEquals (colour.getFloatAlpha(), expectedFloatAlpha);
597+
expectEquals (colour.getRed(), expectedRed);
598+
expectEquals (colour.getGreen(), expectedGreen);
599+
expectEquals (colour.getBlue(), expectedBlue);
600+
expectEquals (colour.getAlpha(), expectedAlpha);
592601
};
593602

594603
beginTest ("Constructors");
595604
{
596605
Colour c1;
597-
testColour (c1, (uint8) 0, (uint8) 0, (uint8) 0, (uint8) 0, 0.0f);
606+
testColour (c1, (uint8) 0, (uint8) 0, (uint8) 0, (uint8) 0);
598607

599608
Colour c2 ((uint32) 0);
600-
testColour (c2, (uint8) 0, (uint8) 0, (uint8) 0, (uint8) 0, 0.0f);
609+
testColour (c2, (uint8) 0, (uint8) 0, (uint8) 0, (uint8) 0);
601610

602611
Colour c3 ((uint32) 0xffffffff);
603-
testColour (c3, (uint8) 255, (uint8) 255, (uint8) 255, (uint8) 255, 1.0f);
612+
testColour (c3, (uint8) 255, (uint8) 255, (uint8) 255, (uint8) 255);
604613

605614
Colour c4 (0, 0, 0);
606-
testColour (c4, (uint8) 0, (uint8) 0, (uint8) 0, (uint8) 255, 1.0f);
615+
testColour (c4, (uint8) 0, (uint8) 0, (uint8) 0, (uint8) 255);
607616

608617
Colour c5 (255, 255, 255);
609-
testColour (c5, (uint8) 255, (uint8) 255, (uint8) 255, (uint8) 255, 1.0f);
618+
testColour (c5, (uint8) 255, (uint8) 255, (uint8) 255, (uint8) 255);
610619

611620
Colour c6 ((uint8) 0, (uint8) 0, (uint8) 0, (uint8) 0);
612-
testColour (c6, (uint8) 0, (uint8) 0, (uint8) 0, (uint8) 0, 0.0f);
621+
testColour (c6, (uint8) 0, (uint8) 0, (uint8) 0, (uint8) 0);
613622

614623
Colour c7 ((uint8) 255, (uint8) 255, (uint8) 255, (uint8) 255);
615-
testColour (c7, (uint8) 255, (uint8) 255, (uint8) 255, (uint8) 255, 1.0f);
624+
testColour (c7, (uint8) 255, (uint8) 255, (uint8) 255, (uint8) 255);
616625

617626
Colour c8 ((uint8) 0, (uint8) 0, (uint8) 0, 0.0f);
618-
testColour (c8, (uint8) 0, (uint8) 0, (uint8) 0, (uint8) 0, 0.0f);
627+
testColour (c8, (uint8) 0, (uint8) 0, (uint8) 0, (uint8) 0);
619628

620629
Colour c9 ((uint8) 255, (uint8) 255, (uint8) 255, 1.0f);
621-
testColour (c9, (uint8) 255, (uint8) 255, (uint8) 255, (uint8) 255, 1.0f);
630+
testColour (c9, (uint8) 255, (uint8) 255, (uint8) 255, (uint8) 255);
622631
}
623632

624633
beginTest ("HSV");
625634
{
626635
// black
627636
testColour (Colour::fromHSV (0.0f, 0.0f, 0.0f, 1.0f), 0, 0, 0);
637+
testColour (Colour::fromHSV (0.0f, 0.0f, 0.0f, 0.5f), 0, 0, 0, 128);
628638
// white
629639
testColour (Colour::fromHSV (0.0f, 0.0f, 1.0f, 1.0f), 255, 255, 255);
640+
testColour (Colour::fromHSV (0.0f, 0.0f, 1.0f, 0.5f), 255, 255, 255, 128);
630641
// red
631642
testColour (Colour::fromHSV (0.0f, 1.0f, 1.0f, 1.0f), 255, 0, 0);
643+
testColour (Colour::fromHSV (0.0f, 1.0f, 1.0f, 0.5f), 255, 0, 0, 128);
632644
testColour (Colour::fromHSV (1.0f, 1.0f, 1.0f, 1.0f), 255, 0, 0);
645+
testColour (Colour::fromHSV (1.0f, 1.0f, 1.0f, 0.5f), 255, 0, 0, 128);
633646
// lime
634647
testColour (Colour::fromHSV (120 / 360.0f, 1.0f, 1.0f, 1.0f), 0, 255, 0);
648+
testColour (Colour::fromHSV (120 / 360.0f, 1.0f, 1.0f, 0.5f), 0, 255, 0, 128);
635649
// blue
636650
testColour (Colour::fromHSV (240 / 360.0f, 1.0f, 1.0f, 1.0f), 0, 0, 255);
651+
testColour (Colour::fromHSV (240 / 360.0f, 1.0f, 1.0f, 0.5f), 0, 0, 255, 128);
637652
// yellow
638653
testColour (Colour::fromHSV (60 / 360.0f, 1.0f, 1.0f, 1.0f), 255, 255, 0);
654+
testColour (Colour::fromHSV (60 / 360.0f, 1.0f, 1.0f, 0.5f), 255, 255, 0, 128);
639655
// cyan
640656
testColour (Colour::fromHSV (180 / 360.0f, 1.0f, 1.0f, 1.0f), 0, 255, 255);
657+
testColour (Colour::fromHSV (180 / 360.0f, 1.0f, 1.0f, 0.5f), 0, 255, 255, 128);
641658
// magenta
642659
testColour (Colour::fromHSV (300 / 360.0f, 1.0f, 1.0f, 1.0f), 255, 0, 255);
660+
testColour (Colour::fromHSV (300 / 360.0f, 1.0f, 1.0f, 0.5f), 255, 0, 255, 128);
643661
// silver
644662
testColour (Colour::fromHSV (0.0f, 0.0f, 0.75f, 1.0f), 191, 191, 191);
663+
testColour (Colour::fromHSV (0.0f, 0.0f, 0.75f, 0.5f), 191, 191, 191, 128);
645664
// grey
646665
testColour (Colour::fromHSV (0.0f, 0.0f, 0.5f, 1.0f), 128, 128, 128);
666+
testColour (Colour::fromHSV (0.0f, 0.0f, 0.5f, 0.5f), 128, 128, 128, 128);
647667
// maroon
648668
testColour (Colour::fromHSV (0.0f, 1.0f, 0.5f, 1.0f), 128, 0, 0);
669+
testColour (Colour::fromHSV (0.0f, 1.0f, 0.5f, 0.5f), 128, 0, 0, 128);
649670
// olive
650671
testColour (Colour::fromHSV (60 / 360.0f, 1.0f, 0.5f, 1.0f), 128, 128, 0);
672+
testColour (Colour::fromHSV (60 / 360.0f, 1.0f, 0.5f, 0.5f), 128, 128, 0, 128);
651673
// green
652674
testColour (Colour::fromHSV (120 / 360.0f, 1.0f, 0.5f, 1.0f), 0, 128, 0);
675+
testColour (Colour::fromHSV (120 / 360.0f, 1.0f, 0.5f, 0.5f), 0, 128, 0, 128);
653676
// purple
654677
testColour (Colour::fromHSV (300 / 360.0f, 1.0f, 0.5f, 1.0f), 128, 0, 128);
678+
testColour (Colour::fromHSV (300 / 360.0f, 1.0f, 0.5f, 0.5f), 128, 0, 128, 128);
655679
// teal
656680
testColour (Colour::fromHSV (180 / 360.0f, 1.0f, 0.5f, 1.0f), 0, 128, 128);
681+
testColour (Colour::fromHSV (180 / 360.0f, 1.0f, 0.5f, 0.5f), 0, 128, 128, 128);
657682
// navy
658683
testColour (Colour::fromHSV (240 / 360.0f, 1.0f, 0.5f, 1.0f), 0, 0, 128);
684+
testColour (Colour::fromHSV (240 / 360.0f, 1.0f, 0.5f, 0.5f), 0, 0, 128, 128);
659685
}
660686

661687
beginTest ("HSL");
662688
{
663689
// black
664690
testColour (Colour::fromHSL (0.0f, 0.0f, 0.0f, 1.0f), 0, 0, 0);
691+
testColour (Colour::fromHSL (0.0f, 0.0f, 0.0f, 0.5f), 0, 0, 0, 128);
665692
// white
666693
testColour (Colour::fromHSL (0.0f, 0.0f, 1.0f, 1.0f), 255, 255, 255);
694+
testColour (Colour::fromHSL (0.0f, 0.0f, 1.0f, 0.5f), 255, 255, 255, 128);
667695
// red
668696
testColour (Colour::fromHSL (0.0f, 1.0f, 0.5f, 1.0f), 255, 0, 0);
697+
testColour (Colour::fromHSL (0.0f, 1.0f, 0.5f, 0.5f), 255, 0, 0, 128);
669698
testColour (Colour::fromHSL (1.0f, 1.0f, 0.5f, 1.0f), 255, 0, 0);
699+
testColour (Colour::fromHSL (1.0f, 1.0f, 0.5f, 0.5f), 255, 0, 0, 128);
670700
// lime
671701
testColour (Colour::fromHSL (120 / 360.0f, 1.0f, 0.5f, 1.0f), 0, 255, 0);
702+
testColour (Colour::fromHSL (120 / 360.0f, 1.0f, 0.5f, 0.5f), 0, 255, 0, 128);
672703
// blue
673704
testColour (Colour::fromHSL (240 / 360.0f, 1.0f, 0.5f, 1.0f), 0, 0, 255);
705+
testColour (Colour::fromHSL (240 / 360.0f, 1.0f, 0.5f, 0.5f), 0, 0, 255, 128);
674706
// yellow
675707
testColour (Colour::fromHSL (60 / 360.0f, 1.0f, 0.5f, 1.0f), 255, 255, 0);
708+
testColour (Colour::fromHSL (60 / 360.0f, 1.0f, 0.5f, 0.5f), 255, 255, 0, 128);
676709
// cyan
677710
testColour (Colour::fromHSL (180 / 360.0f, 1.0f, 0.5f, 1.0f), 0, 255, 255);
711+
testColour (Colour::fromHSL (180 / 360.0f, 1.0f, 0.5f, 0.5f), 0, 255, 255, 128);
678712
// magenta
679713
testColour (Colour::fromHSL (300 / 360.0f, 1.0f, 0.5f, 1.0f), 255, 0, 255);
714+
testColour (Colour::fromHSL (300 / 360.0f, 1.0f, 0.5f, 0.5f), 255, 0, 255, 128);
680715
// silver
681716
testColour (Colour::fromHSL (0.0f, 0.0f, 0.75f, 1.0f), 191, 191, 191);
717+
testColour (Colour::fromHSL (0.0f, 0.0f, 0.75f, 0.5f), 191, 191, 191, 128);
682718
// grey
683719
testColour (Colour::fromHSL (0.0f, 0.0f, 0.5f, 1.0f), 128, 128, 128);
720+
testColour (Colour::fromHSL (0.0f, 0.0f, 0.5f, 0.5f), 128, 128, 128, 128);
684721
// maroon
685722
testColour (Colour::fromHSL (0.0f, 1.0f, 0.25f, 1.0f), 128, 0, 0);
723+
testColour (Colour::fromHSL (0.0f, 1.0f, 0.25f, 0.5f), 128, 0, 0, 128);
686724
// olive
687725
testColour (Colour::fromHSL (60 / 360.0f, 1.0f, 0.25f, 1.0f), 128, 128, 0);
726+
testColour (Colour::fromHSL (60 / 360.0f, 1.0f, 0.25f, 0.5f), 128, 128, 0, 128);
688727
// green
689728
testColour (Colour::fromHSL (120 / 360.0f, 1.0f, 0.25f, 1.0f), 0, 128, 0);
729+
testColour (Colour::fromHSL (120 / 360.0f, 1.0f, 0.25f, 0.5f), 0, 128, 0, 128);
690730
// purple
691731
testColour (Colour::fromHSL (300 / 360.0f, 1.0f, 0.25f, 1.0f), 128, 0, 128);
732+
testColour (Colour::fromHSL (300 / 360.0f, 1.0f, 0.25f, 0.5f), 128, 0, 128, 128);
692733
// teal
693734
testColour (Colour::fromHSL (180 / 360.0f, 1.0f, 0.25f, 1.0f), 0, 128, 128);
735+
testColour (Colour::fromHSL (180 / 360.0f, 1.0f, 0.25f, 0.5f), 0, 128, 128, 128);
694736
// navy
695737
testColour (Colour::fromHSL (240 / 360.0f, 1.0f, 0.25f, 1.0f), 0, 0, 128);
738+
testColour (Colour::fromHSL (240 / 360.0f, 1.0f, 0.25f, 0.5f), 0, 0, 128, 128);
696739
}
697740

698741
beginTest ("Modifiers");
@@ -715,6 +758,8 @@ class ColourTests final : public UnitTest
715758
testColour (red.withMultipliedLightness (2.0f), 255, 255, 255);
716759
testColour (red.withMultipliedLightness (1.0f), 255, 0, 0);
717760
testColour (red.withLightness (red.getLightness()), 255, 0, 0);
761+
testColour (red.withAlpha ((uint8) 128), 255, 0, 0, 128);
762+
testColour (red.withAlpha (0.5f), 255, 0, 0, 128);
718763
}
719764
}
720765
};

modules/juce_graphics/colour/juce_Colour.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,17 @@ class JUCE_API Colour final
136136
float brightness,
137137
float alpha) noexcept;
138138

139+
/** Creates a colour using floating point hue, saturation, brightness values, and an 8-bit alpha.
140+
141+
All values must be between 0.0 and 1.0.
142+
An alpha of 0x00 is completely transparent, alpha of 0xff is opaque.
143+
Numbers outside the valid range will be clipped.
144+
*/
145+
static Colour fromHSV (float hue,
146+
float saturation,
147+
float brightness,
148+
uint8 alpha) noexcept;
149+
139150
/** Creates a colour using floating point hue, saturation, lightness and alpha values.
140151
141152
All values must be between 0.0 and 1.0.
@@ -146,6 +157,17 @@ class JUCE_API Colour final
146157
float lightness,
147158
float alpha) noexcept;
148159

160+
/** Creates a colour using floating point hue, saturation, lightness values, and an 8-bit alpha.
161+
162+
All values must be between 0.0 and 1.0.
163+
An alpha of 0x00 is completely transparent, alpha of 0xff is opaque.
164+
Numbers outside the valid range will be clipped.
165+
*/
166+
static Colour fromHSL (float hue,
167+
float saturation,
168+
float lightness,
169+
uint8 alpha) noexcept;
170+
149171
/** Creates a colour using a PixelARGB object. This function assumes that the argb pixel is
150172
not premultiplied.
151173
*/

0 commit comments

Comments
 (0)