Loading...
Searching...
No Matches
GradientColorPicker.h
Go to the documentation of this file.
1// Copyright (c) 2025 Dave R. Smith.
2// Malena Framework — Licensed under PolyForm Noncommercial 1.0.0; commercial use requires a paid license. See LICENSE.
3
4#ifndef MALENA_GRADIENTCOLORPICKER_H
5#define MALENA_GRADIENTCOLORPICKER_H
6
7#pragma once
8
12#include <functional>
13
14namespace ml
15{
31 class MALENA_API GradientColorPicker : public Graphic<sf::RectangleShape>
32 {
33 public:
35
37 void setColor(const sf::Color& c);
38 [[nodiscard]] sf::Color color() const;
39
41 void onColorChanged(std::function<void(const sf::Color&)> cb);
43 void onCommit(std::function<void()> cb);
44
45 void setSize(const sf::Vector2f& size);
46 [[nodiscard]] sf::Vector2f getSize() const;
47
48 void setPosition(const sf::Vector2f& pos) override;
49 [[nodiscard]] sf::Vector2f getPosition() const override;
50 [[nodiscard]] sf::FloatRect getGlobalBounds() const override;
51
52 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
53
54 // HSV<->RGB helpers (h in [0,360), s/v in [0,1]).
55 static sf::Color hsvToRgb(float h, float s, float v);
56 static void rgbToHsv(const sf::Color& c, float& h, float& s, float& v);
57
58 private:
59 [[nodiscard]] sf::FloatRect svRect() const;
60 [[nodiscard]] sf::FloatRect hueRect() const;
61 void emitChange();
62
63 sf::Vector2f _pos { 0.f, 0.f };
64 sf::Vector2f _size { 240.f, 200.f };
65 float _hueBarW = 22.f;
66 float _gap = 10.f;
67
68 float _h = 0.f, _s = 1.f, _v = 1.f;
69
70 int _drag = 0; // 0 none, 1 SV square, 2 hue bar
71 bool _prevMouseDown = false;
72 bool _prevEnter = false;
73
74 std::function<void(const sf::Color&)> _onColorChanged;
75 std::function<void()> _onCommit;
76 };
77
78} // namespace ml
79
80#endif // MALENA_GRADIENTCOLORPICKER_H
void setPosition(const sf::Vector2f &pos) override
Set the world-space position immediately (no animation).
sf::FloatRect getGlobalBounds() const override
Return the axis-aligned bounding box in world space.
void setSize(const sf::Vector2f &size)
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
void onCommit(std::function< void()> cb)
sf::Color color() const
static void rgbToHsv(const sf::Color &c, float &h, float &s, float &v)
void setColor(const sf::Color &c)
static sf::Color hsvToRgb(float h, float s, float v)
sf::Vector2f getPosition() const override
Return the current world-space position.
void onColorChanged(std::function< void(const sf::Color &)> cb)
sf::Vector2f getSize() const
#define MALENA_API
Definition Component.h:22
Rect< float > FloatRect
Vector2< float > Vector2f