| 1 | /* |
| 2 | NanoGUI was developed by Wenzel Jakob <wenzel.jakob@epfl.ch>. |
| 3 | The widget drawing code is based on the NanoVG demo application |
| 4 | by Mikko Mononen. |
| 5 | |
| 6 | All rights reserved. Use of this source code is governed by a |
| 7 | BSD-style license that can be found in the LICENSE.txt file. |
| 8 | */ |
| 9 | /** |
| 10 | * \file nanogui/colorwheel.h |
| 11 | * |
| 12 | * \brief Fancy analog widget to select a color value. This widget was |
| 13 | * contributed by Dmitriy Morozov. |
| 14 | */ |
| 15 | |
| 16 | #pragma once |
| 17 | |
| 18 | #include <nanogui/widget.h> |
| 19 | |
| 20 | NAMESPACE_BEGIN(nanogui) |
| 21 | |
| 22 | /** |
| 23 | * \class ColorWheel colorwheel.h nanogui/colorwheel.h |
| 24 | * |
| 25 | * \brief Fancy analog widget to select a color value. This widget was |
| 26 | * contributed by Dmitriy Morozov. |
| 27 | */ |
| 28 | class NANOGUI_EXPORT ColorWheel : public Widget { |
| 29 | public: |
| 30 | /** |
| 31 | * Adds a ColorWheel to the specified parent. |
| 32 | * |
| 33 | * \param parent |
| 34 | * The Widget to add this ColorWheel to. |
| 35 | * |
| 36 | * \param color |
| 37 | * The initial color of the ColorWheel (default: Red). |
| 38 | */ |
| 39 | ColorWheel(Widget *parent, const Color& color = Color(1.0f, 0.0f, 0.0f, 1.0f)); |
| 40 | |
| 41 | /// The callback to execute when a user changes the ColorWheel value. |
| 42 | std::function<void(const Color &)> callback() const { return mCallback; } |
| 43 | |
| 44 | /// Sets the callback to execute when a user changes the ColorWheel value. |
| 45 | void setCallback(const std::function<void(const Color &)> &callback) { mCallback = callback; } |
| 46 | |
| 47 | /// The current Color this ColorWheel has selected. |
| 48 | Color color() const; |
| 49 | |
| 50 | /// Sets the current Color this ColorWheel has selected. |
| 51 | void setColor(const Color& color); |
| 52 | |
| 53 | /// The preferred size of this ColorWheel. |
| 54 | virtual Vector2i preferredSize(NVGcontext *ctx) const override; |
| 55 | |
| 56 | /// Draws the ColorWheel. |
| 57 | virtual void draw(NVGcontext *ctx) override; |
| 58 | |
| 59 | /// Handles mouse button click events for the ColorWheel. |
| 60 | virtual bool mouseButtonEvent(const Vector2i &p, int button, bool down, int modifiers) override; |
| 61 | |
| 62 | /// Handles mouse drag events for the ColorWheel. |
| 63 | virtual bool mouseDragEvent(const Vector2i &p, const Vector2i &rel, int button, int modifiers) override; |
| 64 | |
| 65 | /// Saves the current state of this ColorWheel to the specified Serializer. |
| 66 | virtual void save(Serializer &s) const override; |
| 67 | |
| 68 | /// Sets the state of this ColorWheel using the specified Serializer. |
| 69 | virtual bool load(Serializer &s) override; |
| 70 | |
| 71 | private: |
| 72 | // Used to describe where the mouse is interacting |
| 73 | enum Region { |
| 74 | None = 0, |
| 75 | InnerTriangle = 1, |
| 76 | OuterCircle = 2, |
| 77 | Both = 3 |
| 78 | }; |
| 79 | |
| 80 | // Converts a specified hue (with saturation = value = 1) to RGB space. |
| 81 | Color hue2rgb(float h) const; |
| 82 | |
| 83 | // Manipulates the positioning of the different regions of the ColorWheel. |
| 84 | Region adjustPosition(const Vector2i &p, Region consideredRegions = Both); |
| 85 | |
| 86 | protected: |
| 87 | /// The current Hue in the HSV color model. |
| 88 | float mHue; |
| 89 | |
| 90 | /** |
| 91 | * The implicit Value component of the HSV color model. See implementation |
| 92 | * \ref nanogui::ColorWheel::color for its usage. Valid values are in the |
| 93 | * range ``[0, 1]``. |
| 94 | */ |
| 95 | float mWhite; |
| 96 | |
| 97 | /** |
| 98 | * The implicit Saturation component of the HSV color model. See implementation |
| 99 | * \ref nanogui::ColorWheel::color for its usage. Valid values are in the |
| 100 | * range ``[0, 1]``. |
| 101 | */ |
| 102 | float mBlack; |
| 103 | |
| 104 | /// The current region the mouse is interacting with. |
| 105 | Region mDragRegion; |
| 106 | |
| 107 | /// The current callback to execute when the color value has changed. |
| 108 | std::function<void(const Color &)> mCallback; |
| 109 | |
| 110 | public: |
| 111 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
| 112 | }; |
| 113 | |
| 114 | NAMESPACE_END(nanogui) |
| 115 | |