iPlug2 - C++ Audio Plug-in Framework
IColorPickerControl.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5 
6  See LICENSE.txt for more info.
7 
8  ==============================================================================
9 */
10 
11 #pragma once
12 
18 #include "IControl.h"
19 #ifdef IGRAPHICS_NANOVG
20 #include "nanovg.h"
21 #endif
22 
23 BEGIN_IPLUG_NAMESPACE
24 BEGIN_IGRAPHICS_NAMESPACE
25 
29 {
30 public:
31  IColorPickerControl(const IRECT& bounds)
32  : IControl(bounds)
33  {
34  }
35 
36  void Draw(IGraphics& g) override
37  {
38 #ifdef IGRAPHICS_NANOVG
39  NVGcontext* vg = (NVGcontext* ) g.GetDrawContext();
40  float x = mRECT.L;
41  float y = mRECT.T;
42  float w = mRECT.W();
43  float h = mRECT.H();
44 
45  int i;
46  float r0, r1, ax,ay, bx,by, cx,cy, aeps, r;
47  float hue = 0.;
48  NVGpaint paint;
49 
50  nvgSave(vg);
51 
52  /* nvgBeginPath(vg);
53  nvgRect(vg, x,y,w,h);
54  nvgFillColor(vg, nvgRGBA(255,0,0,128));
55  nvgFill(vg);*/
56 
57  cx = x + w*0.5f;
58  cy = y + h*0.5f;
59  r1 = (w < h ? w : h) * 0.5f - 5.0f;
60  r0 = r1 - 20.0f;
61  aeps = 0.5f / r1; // half a pixel arc length in radians (2pi cancels out).
62 
63  for (i = 0; i < 6; i++) {
64  float a0 = (float)i / 6.0f * NVG_PI * 2.0f - aeps;
65  float a1 = (float)(i+1.0f) / 6.0f * NVG_PI * 2.0f + aeps;
66  nvgBeginPath(vg);
67  nvgArc(vg, cx,cy, r0, a0, a1, NVG_CW);
68  nvgArc(vg, cx,cy, r1, a1, a0, NVG_CCW);
69  nvgClosePath(vg);
70  ax = cx + cosf(a0) * (r0+r1)*0.5f;
71  ay = cy + sinf(a0) * (r0+r1)*0.5f;
72  bx = cx + cosf(a1) * (r0+r1)*0.5f;
73  by = cy + sinf(a1) * (r0+r1)*0.5f;
74  paint = nvgLinearGradient(vg, ax,ay, bx,by, nvgHSLA(a0/(NVG_PI*2),1.0f,0.55f,255), nvgHSLA(a1/(NVG_PI*2),1.0f,0.55f,255));
75  nvgFillPaint(vg, paint);
76  nvgFill(vg);
77  }
78 
79  nvgBeginPath(vg);
80  nvgCircle(vg, cx,cy, r0-0.5f);
81  nvgCircle(vg, cx,cy, r1+0.5f);
82  nvgStrokeColor(vg, nvgRGBA(0,0,0,64));
83  nvgStrokeWidth(vg, 1.0f);
84  nvgStroke(vg);
85 
86  // Selector
87  nvgSave(vg);
88  nvgTranslate(vg, cx,cy);
89  nvgRotate(vg, hue*NVG_PI*2);
90 
91  // Marker on
92  nvgStrokeWidth(vg, 2.0f);
93  nvgBeginPath(vg);
94  nvgRect(vg, r0-1,-3,r1-r0+2,6);
95  nvgStrokeColor(vg, nvgRGBA(255,255,255,192));
96  nvgStroke(vg);
97 
98  paint = nvgBoxGradient(vg, r0-3,-5,r1-r0+6,10, 2,4, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0));
99  nvgBeginPath(vg);
100  nvgRect(vg, r0-2-10,-4-10,r1-r0+4+20,8+20);
101  nvgRect(vg, r0-2,-4,r1-r0+4,8);
102  nvgPathWinding(vg, NVG_HOLE);
103  nvgFillPaint(vg, paint);
104  nvgFill(vg);
105 
106  // Center triangle
107  r = r0 - 6;
108  ax = cosf(120.0f/180.0f*NVG_PI) * r;
109  ay = sinf(120.0f/180.0f*NVG_PI) * r;
110  bx = cosf(-120.0f/180.0f*NVG_PI) * r;
111  by = sinf(-120.0f/180.0f*NVG_PI) * r;
112  nvgBeginPath(vg);
113  nvgMoveTo(vg, r,0);
114  nvgLineTo(vg, ax,ay);
115  nvgLineTo(vg, bx,by);
116  nvgClosePath(vg);
117  paint = nvgLinearGradient(vg, r,0, ax,ay, nvgHSLA(hue,1.0f,0.5f,255), nvgRGBA(255,255,255,255));
118  nvgFillPaint(vg, paint);
119  nvgFill(vg);
120  paint = nvgLinearGradient(vg, (r+ax)*0.5f,(0+ay)*0.5f, bx,by, nvgRGBA(0,0,0,0), nvgRGBA(0,0,0,255));
121  nvgFillPaint(vg, paint);
122  nvgFill(vg);
123  nvgStrokeColor(vg, nvgRGBA(0,0,0,64));
124  nvgStroke(vg);
125 
126  // Select circle on triangle
127  ax = cosf(120.0f/180.0f*NVG_PI) * r*0.3f;
128  ay = sinf(120.0f/180.0f*NVG_PI) * r*0.4f;
129  nvgStrokeWidth(vg, 2.0f);
130  nvgBeginPath(vg);
131  nvgCircle(vg, ax,ay,5);
132  nvgStrokeColor(vg, nvgRGBA(255,255,255,192));
133  nvgStroke(vg);
134 
135  paint = nvgRadialGradient(vg, ax,ay, 7,9, nvgRGBA(0,0,0,64), nvgRGBA(0,0,0,0));
136  nvgBeginPath(vg);
137  nvgRect(vg, ax-20,ay-20,40,40);
138  nvgCircle(vg, ax,ay,7);
139  nvgPathWinding(vg, NVG_HOLE);
140  nvgFillPaint(vg, paint);
141  nvgFill(vg);
142 
143  nvgRestore(vg);
144 
145  nvgRestore(vg);
146 #endif
147  }
148 
149  void OnMouseDown(float x, float y, const IMouseMod& mod) override
150  {
151  }
152 private:
153 };
154 
155 END_IGRAPHICS_NAMESPACE
156 END_IPLUG_NAMESPACE
The lowest level base class of an IGraphics control.
Definition: IControl.h:42
Used to manage a rectangular area, independent of draw class/platform.
Used to manage mouse modifiers i.e.
This file contains the base IControl implementation, along with some base classes for specific types ...
float H() const
float W() const
A control for choosing a color.
IControl(const IRECT &bounds, int paramIdx=kNoParameter, IActionFunction actionFunc=nullptr)
Constructor.
Definition: IControl.cpp:81
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
void Draw(IGraphics &g) override
Draw the control to the graphics context.
virtual void * GetDrawContext()=0
Gets a void pointer to underlying drawing context, for the IGraphics backend See draw class implement...
float L
Left side of the rectangle (X)
void OnMouseDown(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouse down event on this control.
float T
Top of the rectangle (Y)