1 /*
2     nanogui/colorwheel.h -- fancy analog widget to select a color value
3 
4     This widget was contributed by Dmitriy Morozov.
5 
6     NanoGUI was developed by Wenzel Jakob <wenzel.jakob@epfl.ch>.
7     The widget drawing code is based on the NanoVG demo application
8     by Mikko Mononen.
9 
10     All rights reserved. Use of this source code is governed by a
11     BSD-style license that can be found in the LICENSE.txt file.
12 */
13 /** \file */
14 
15 #pragma once
16 
17 #include <nanogui/widget.h>
18 
NAMESPACE_BEGIN(nanogui)19 NAMESPACE_BEGIN(nanogui)
20 
21 /**
22  * \class ColorWheel colorwheel.h nanogui/colorwheel.h
23  *
24  * \brief Fancy analog widget to select a color value.
25  */
26 class NANOGUI_EXPORT ColorWheel : public Widget {
27 public:
28     ColorWheel(Widget *parent, const Color& color = Color(1.0f, 0.0f, 0.0f, 1.0f));
29 
30     /// Set the change callback
31     std::function<void(const Color &)> callback() const                  { return mCallback;     }
32     void setCallback(const std::function<void(const Color &)> &callback) { mCallback = callback; }
33 
34     /// Get the current color
35     Color color() const;
36     /// Set the current color
37     void setColor(const Color& color);
38 
39     virtual Vector2i preferredSize(NVGcontext *ctx) const override;
40     virtual void draw(NVGcontext *ctx) override;
41     virtual bool mouseButtonEvent(const Vector2i &p, int button, bool down, int modifiers) override;
42     virtual bool mouseDragEvent(const Vector2i &p, const Vector2i &rel, int button, int modifiers) override;
43 
44     virtual void save(Serializer &s) const override;
45     virtual bool load(Serializer &s) override;
46 private:
47     enum Region {
48         None = 0,
49         InnerTriangle = 1,
50         OuterCircle = 2,
51         Both = 3
52     };
53 
54     Color hue2rgb(float h) const;
55     Region adjustPosition(const Vector2i &p, Region consideredRegions = Both);
56 
57 protected:
58     float mHue;
59     float mWhite;
60     float mBlack;
61     Region mDragRegion;
62     std::function<void(const Color &)> mCallback;
63 };
64 
65 NAMESPACE_END(nanogui)
66