68 for (
int y = 0; y < height; ++y)
70 auto val = 1.0f - (
float) y / (
float) height;
72 for (
int x = 0; x < width; ++x)
74 auto sat = (
float) x / (
float) width;
101 void updateIfNeeded()
144 ColourSpaceMarker marker;
152 .withCentre (area.getRelativePoint (s, 1.0f - v)));
172 cg.point1.setXY (0.0f, (
float) edge);
175 for (
float i = 0.0f; i <= 1.0f; i += 0.02f)
176 cg.addColour (i,
Colour (i, 1.0f, 1.0f, 1.0f));
188 .withCentre (area.getRelativePoint (0.5f, h)));
198 owner.setHue ((
float) (e.
y - edge) / (
float) (
getHeight() - edge * 2));
201 void updateIfNeeded()
218 void paint (Graphics& g)
override
224 p.addTriangle (1.0f, 1.0f,
225 cw * 0.3f,
ch * 0.5f,
228 p.addTriangle (
cw - 1.0f, 1.0f,
229 cw * 0.7f,
ch * 0.5f,
230 cw - 1.0f,
ch - 1.0f);
232 g.setColour (Colours::white.withAlpha (0.75f));
235 g.setColour (Colours::black.withAlpha (0.75f));
236 g.strokePath (p, PathStrokeType (1.2f));
240 HueSelectorMarker marker;
250 : owner (
cs), index (itemIndex)
266 m.
addItem (1,
TRANS (
"Use this swatch as the current colour"));
268 m.
addItem (2,
TRANS (
"Set this swatch to the current colour"));
282 if (result == 1) comp->setColourFromSwatch();
283 if (result == 2) comp->setSwatchFromColour();
287 void setColourFromSwatch()
292 void setSwatchFromColour()
311 colourLabel.
setFont (labelFont);
321 ed->setInputRestrictions ((owner.flags &
showAlphaChannel) ? 8 : 6,
"1234567890ABCDEFabcdef");
326 updateColourIfNecessary (colourLabel.
getText());
333 void updateIfNeeded()
340 auto textColour = (Colours::white.overlaidWith (currentColour).contrasting());
355 Colour (0xffdddddd).overlaidWith (currentColour),
356 Colour (0xffffffff).overlaidWith (currentColour));
385 : colour (Colours::white),
414 for (
auto& slider : sliders)
415 slider->onValueChange = [
this] { changeColour(); };
418 if ((flags & showColourspace) != 0)
423 addAndMakeVisible (colourSpace.get());
424 addAndMakeVisible (hueSelector.get());
430ColourSelector::~ColourSelector()
432 dispatchPendingMessages();
433 swatchComponents.clear();
437Colour ColourSelector::getCurrentColour()
const
439 return ((flags & showAlphaChannel) != 0) ? colour : colour.
withAlpha ((
uint8) 0xff);
446 colour = ((flags & showAlphaChannel) != 0) ? c : c.
withAlpha ((
uint8) 0xff);
449 update (notification);
453void ColourSelector::setHue (
float newH)
455 newH =
jlimit (0.0f, 1.0f, newH);
457 if (! approximatelyEqual (h, newH))
460 colour =
Colour (h, s, v, colour.getFloatAlpha());
461 update (sendNotification);
465void ColourSelector::setSV (
float newS,
float newV)
467 newS =
jlimit (0.0f, 1.0f, newS);
468 newV =
jlimit (0.0f, 1.0f, newV);
470 if (! approximatelyEqual (s, newS) || ! approximatelyEqual (v, newV))
474 colour = Colour (h, s, v, colour.getFloatAlpha());
475 update (sendNotification);
480void ColourSelector::updateHSV()
482 colour.getHSB (h, s, v);
485void ColourSelector::update (NotificationType notification)
487 if (sliders[0] !=
nullptr)
489 sliders[0]->setValue ((
int) colour.getRed(), notification);
490 sliders[1]->setValue ((
int) colour.getGreen(), notification);
491 sliders[2]->setValue ((
int) colour.getBlue(), notification);
492 sliders[3]->setValue ((
int) colour.getAlpha(), notification);
495 if (colourSpace !=
nullptr)
497 colourSpace->updateIfNeeded();
498 hueSelector->updateIfNeeded();
501 if (previewComponent !=
nullptr)
502 previewComponent->updateIfNeeded();
504 if (notification != dontSendNotification)
507 if (notification == sendNotificationSync)
508 dispatchPendingMessages();
512void ColourSelector::paint (Graphics& g)
514 g.fillAll (findColour (backgroundColourId));
516 if ((flags & showSliders) != 0)
518 g.setColour (findColour (labelTextColourId));
521 for (
auto& slider : sliders)
523 if (slider->isVisible())
524 g.drawText (slider->getName() +
":",
526 slider->getX() - 8, slider->getHeight(),
527 Justification::centredRight,
false);
532void ColourSelector::resized()
534 const int swatchesPerRow = 8;
535 const int swatchHeight = 22;
537 const int numSliders = ((flags & showAlphaChannel) != 0) ? 4 : 3;
538 const int numSwatches = getNumSwatches();
540 const int swatchSpace = numSwatches > 0 ? edgeGap + swatchHeight * ((numSwatches + 7) / swatchesPerRow) : 0;
541 const int sliderSpace = ((flags & showSliders) != 0) ?
jmin (22 * numSliders + edgeGap, proportionOfHeight (0.3f)) : 0;
542 const int topSpace = ((flags & showColourAtTop) != 0) ?
jmin (30 + edgeGap * 2, proportionOfHeight (0.2f)) : edgeGap;
544 if (previewComponent !=
nullptr)
545 previewComponent->setBounds (edgeGap, edgeGap, getWidth() - edgeGap * 2, topSpace - edgeGap * 2);
549 if ((flags & showColourspace) != 0)
551 const int hueWidth =
jmin (50, proportionOfWidth (0.15f));
553 colourSpace->setBounds (edgeGap, y,
554 getWidth() - hueWidth - edgeGap - 4,
555 getHeight() - topSpace - sliderSpace - swatchSpace - edgeGap);
557 hueSelector->setBounds (colourSpace->getRight() + 4, y,
558 getWidth() - edgeGap - (colourSpace->getRight() + 4),
559 colourSpace->getHeight());
561 y = getHeight() - sliderSpace - swatchSpace - edgeGap;
564 if ((flags & showSliders) != 0)
566 auto sliderHeight =
jmax (4, sliderSpace / numSliders);
568 for (
int i = 0; i < numSliders; ++i)
570 sliders[i]->setBounds (proportionOfWidth (0.2f), y,
571 proportionOfWidth (0.72f), sliderHeight - 2);
579 const int startX = 8;
582 const int swatchWidth = (getWidth() - startX * 2) / swatchesPerRow;
585 if (swatchComponents.size() != numSwatches)
587 swatchComponents.clear();
589 for (
int i = 0; i < numSwatches; ++i)
591 auto* sc =
new SwatchComponent (*
this, i);
592 swatchComponents.add (sc);
593 addAndMakeVisible (sc);
599 for (
int i = 0; i < swatchComponents.size(); ++i)
601 auto* sc = swatchComponents.getUnchecked (i);
603 sc->setBounds (x + xGap / 2,
606 swatchHeight - yGap);
608 if (((i + 1) % swatchesPerRow) == 0)
621void ColourSelector::changeColour()
623 if (sliders[0] !=
nullptr)
624 setCurrentColour (Colour ((uint8) sliders[0]->getValue(),
625 (uint8) sliders[1]->getValue(),
626 (uint8) sliders[2]->getValue(),
627 (uint8) sliders[3]->getValue()));
631int ColourSelector::getNumSwatches()
const
636Colour ColourSelector::getSwatchColour (
int)
const
639 return Colours::black;
642void ColourSelector::setSwatchColour (
int,
const Colour&)
Describes the layout and colours that should be used to paint a colour gradient.
bool isRadial
If true, the gradient should be filled circularly, centred around point1, with point2 defining a poin...
void paint(Graphics &g) override
Components can override this method to draw their content.
void resized() override
Called when this component's size has been changed.
void resized() override
Called when this component's size has been changed.
void mouseDown(const MouseEvent &e) override
Called when a mouse button is pressed.
void paint(Graphics &g) override
Components can override this method to draw their content.
void mouseDrag(const MouseEvent &e) override
Called when the mouse is moved while a button is held down.
void mouseDown(const MouseEvent &e) override
Called when a mouse button is pressed.
void mouseDrag(const MouseEvent &e) override
Called when the mouse is moved while a button is held down.
void resized() override
Called when this component's size has been changed.
void paint(Graphics &g) override
Components can override this method to draw their content.
void paint(Graphics &g) override
Components can override this method to draw their content.
void mouseDown(const MouseEvent &) override
Called when a mouse button is pressed.
A component that lets the user choose a colour.
ColourSelector(int flags=(showAlphaChannel|showColourAtTop|showSliders|showColourspace), int edgeGap=4, int gapAroundColourSpaceComponent=7)
Creates a ColourSelector object.
virtual void setSwatchColour(int index, const Colour &newColour)
Called by the selector when the user puts a new colour into one of the swatches.
virtual Colour getSwatchColour(int index) const
Called by the selector to find out the colour of one of the swatches.
Colour getCurrentColour() const
Returns the colour that the user has currently selected.
void setCurrentColour(Colour newColour, NotificationType notificationType=sendNotification)
Changes the colour that is currently being shown.
@ showColourAtTop
if set, a swatch of the colour is shown at the top of the component.
@ editableColour
if set, the colour shows at the top of the component is editable.
@ showSliders
if set, RGB sliders are shown at the bottom of the component.
@ showColourspace
if set, a big HSV selector is shown.
@ showAlphaChannel
if set, the colour's alpha channel can be changed as well as its RGB.
Represents a colour, also including a transparency value.
Colour withAlpha(uint8 newAlpha) const noexcept
Returns a colour that's the same colour as this one, but with a new alpha value.
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().
String toDisplayString(bool includeAlphaValue) const
Returns the colour as a hex string in the form RRGGBB or AARRGGBB.
The base class for all JUCE user-interface objects.
void setInterceptsMouseClicks(bool allowClicksOnThisComponent, bool allowClicksOnChildComponents) noexcept
Changes the default return value for the hitTest() method.
int getHeight() const noexcept
Returns the component's height in pixels.
void addAndMakeVisible(Component *child, int zOrder=-1)
Adds a child component to this one, and also makes the child visible if it isn't already.
void setMouseCursor(const MouseCursor &cursorType)
Changes the mouse cursor shape to use when the mouse is over this component.
void repaint()
Marks the whole component as needing to be redrawn.
void setColour(int colourID, Colour newColour)
Registers a colour to be used for a particular purpose.
int getWidth() const noexcept
Returns the component's width in pixels.
Rectangle< int > getLocalBounds() const noexcept
Returns the component's bounds, relative to its own origin.
void centreWithSize(int width, int height)
Changes the component's size and centres it within its parent.
void addChildComponent(Component *child, int zOrder=-1)
Adds a child component to this one.
Represents a particular font, including its size, style, etc.
float getHeight() const noexcept
Returns the total height of this font, in pixels.
int getStringWidth(const String &text) const
Returns the total width of a string as it would be drawn using this font.
A graphics context, used for drawing a component or image.
void setOpacity(float newOpacity)
Changes the opacity to use with the current colour.
void setGradientFill(const ColourGradient &gradient)
Sets the context to use a gradient for its fill pattern.
void drawImageTransformed(const Image &imageToDraw, const AffineTransform &transform, bool fillAlphaChannelWithCurrentBrush=false) const
Draws an image, having applied an affine transform to it.
void fillRect(Rectangle< int > rectangle) const
Fills a rectangle with the current colour or brush.
void drawEllipse(float x, float y, float width, float height, float lineThickness) const
Draws an elliptical stroke using the current colour or brush.
void fillCheckerBoard(Rectangle< float > area, float checkWidth, float checkHeight, Colour colour1, Colour colour2) const
Fills a rectangle with a checkerboard pattern, alternating between two colours.
void setColour(Colour newColour)
Changes the current drawing colour.
Retrieves a section of an image as raw pixel data, so it can be read or written to.
void setPixelColour(int x, int y, Colour colour) const noexcept
Sets the colour of a given pixel.
Holds a fixed-size bitmap.
Rectangle< int > getBounds() const noexcept
Returns a rectangle with the same size as this image.
bool isNull() const noexcept
Returns true if this image is not valid.
@ RGB
< each pixel is a 3-byte packed RGB colour value.
@ centred
Indicates that the item should be centred vertically and horizontally.
void setEditable(bool editOnSingleClick, bool editOnDoubleClick=false, bool lossOfFocusDiscardsChanges=false)
Makes the label turn into a TextEditor when clicked.
@ textWhenEditingColourId
The colour for the text when the label is being edited.
@ textColourId
The colour for the text.
void setFont(const Font &newFont)
Changes the font to use to draw the text.
std::function< void()> onEditorShow
You can assign a lambda to this callback object to have it called when the label's editor is shown.
std::function< void()> onEditorHide
You can assign a lambda to this callback object to have it called when the label's editor is hidden.
String getText(bool returnActiveEditorContents=false) const
Returns the label's current text.
void setJustificationType(Justification justification)
Sets the style of justification to be used for positioning the text.
TextEditor * getCurrentTextEditor() const noexcept
Returns the currently-visible text editor, or nullptr if none is open.
void setText(const String &newText, NotificationType notification)
Changes the label text.
static ModalComponentManager::Callback * forComponent(void(*functionToCall)(int, ComponentType *), ComponentType *component)
This is a utility function to create a ModalComponentManager::Callback that will call a static functi...
@ CrosshairCursor
A pair of crosshairs.
Contains position and status information about a mouse event.
const int x
The x-position of the mouse when the event occurred.
const int y
The y-position of the mouse when the event occurred.
Defines the method used to position some kind of rectangular object within a rectangular viewport.
AffineTransform getTransformToFit(const Rectangle< float > &source, const Rectangle< float > &destination) const noexcept
Returns the transform that should be applied to these source coordinates to fit them into the destina...
@ stretchToFit
If this flag is set, then the source rectangle will be resized to completely fill the destination rec...
Manages a rectangle and allows geometric operations to be performed on it.
Rectangle< float > toFloat() const noexcept
Casts this rectangle to a Rectangle<float>.
Rectangle reduced(ValueType deltaX, ValueType deltaY) const noexcept
Returns a rectangle that is smaller than this one by a given amount.
A slider control for changing a value.
Slider()
Creates a slider.
void setRange(double newMinimum, double newMaximum, double newInterval=0)
Sets the limits that the slider's value can take.
String toUpperCase() const
Returns an upper-case version of this string.
String paddedLeft(juce_wchar padCharacter, int minimumLength) const
Returns a copy of this string with the specified character repeatedly added to its beginning until th...
static String toHexString(IntegerType number)
Returns a string representing this numeric value in hexadecimal.
int getHexValue32() const noexcept
Parses the string as a hexadecimal number.
#define TRANS(stringLiteral)
Uses the LocalisedStrings class to translate the given string literal.
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 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.
NotificationType
These enums are used in various classes to indicate whether a notification event should be sent out.
@ dontSendNotification
No notification message should be sent.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
unsigned char uint8
A platform-independent 8-bit unsigned integer type.
double getValueFromText(const String &text) override
Subclasses can override this to convert a text string to a value.
String getTextFromValue(double value) override
Turns the slider's current value into a text string.