ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
Macros | Functions
HSVColorPicker.c File Reference
#include <assert.h>
#include "HSVColorPicker.h"

Go to the source code of this file.

Macros

#define _Class   _HSVColorPicker
 

Functions

Class * _HSVColorPicker (void)
 
static void awakeWithDictionary (View *self, const Dictionary *dictionary)
 
static void dealloc (Object *self)
 
static void didSetComponent (Slider *slider, double value)
 SliderDelegate callback for color component modification. More...
 
static Viewinit (View *self)
 
static void initialize (Class *clazz)
 
static HSVColorPickerinitWithFrame (HSVColorPicker *self, const SDL_Rect *frame)
 
static SDL_Color rgbColor (const HSVColorPicker *self)
 
static void setColor (HSVColorPicker *self, double hue, double saturation, double value)
 
static void setRGBColor (HSVColorPicker *self, const SDL_Color *color)
 
static void updateBindings (View *self)
 

Macro Definition Documentation

◆ _Class

#define _Class   _HSVColorPicker

Definition at line 28 of file HSVColorPicker.c.

Function Documentation

◆ _HSVColorPicker()

Class * _HSVColorPicker ( void  )

Definition at line 259 of file HSVColorPicker.c.

259 {
260 static Class *clazz;
261 static Once once;
262
263 do_once(&once, {
264 clazz = _initialize(&(const ClassDef) {
265 .name = "HSVColorPicker",
266 .superclass = _Control(),
267 .instanceSize = sizeof(HSVColorPicker),
268 .interfaceOffset = offsetof(HSVColorPicker, interface),
269 .interfaceSize = sizeof(HSVColorPickerInterface),
271 });
272 });
273
274 return clazz;
275}
static void initialize(Class *clazz)
Class * _Control(void)
The Control archetype.
Definition: Control.c:379
The HSVColorPicker type.

◆ awakeWithDictionary()

static void awakeWithDictionary ( View self,
const Dictionary *  dictionary 
)
static
See also
View::awakeWithDictionary(View *, const Dictionary *)

Definition at line 82 of file HSVColorPicker.c.

82 {
83
84 super(View, self, awakeWithDictionary, dictionary);
85
86 HSVColorPicker *this = (HSVColorPicker *) self;
87
88 const Inlet inlets[] = MakeInlets(
89 MakeInlet("colorView", InletTypeView, &this->colorView, NULL),
90 MakeInlet("hue", InletTypeDouble, &this->hue, NULL),
91 MakeInlet("saturation", InletTypeDouble, &this->saturation, NULL),
92 MakeInlet("value", InletTypeDouble, &this->value, NULL),
93 MakeInlet("hueSlider", InletTypeView, &this->hueSlider, NULL),
94 MakeInlet("hueInput", InletTypeView, &this->hueInput, NULL),
95 MakeInlet("saturationSlider", InletTypeView, &this->saturationSlider, NULL),
96 MakeInlet("saturationInput", InletTypeView, &this->saturationInput, NULL),
97 MakeInlet("valueSlider", InletTypeView, &this->valueSlider, NULL),
98 MakeInlet("valueInput", InletTypeView, &this->valueInput, NULL),
99 MakeInlet("stackView", InletTypeView, &this->stackView, NULL)
100 );
101
102 $(self, bind, inlets, dictionary);
103}
@ InletTypeDouble
Definition: View+JSON.h:67
@ InletTypeView
Definition: View+JSON.h:139
#define MakeInlets(...)
Creates a null-termianted array of Inlets.
Definition: View+JSON.h:221
#define MakeInlet(name, type, dest, data)
Creates an Inlet with the specified parameters.
Definition: View+JSON.h:216
Inlets enable inbound data binding of View attributes through JSON.
Definition: View+JSON.h:155
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition: View.h:133
_Bool bind(View *self, const Inlet *inlets, const Dictionary *dictionary)
Performs data binding for the Inlets described in dictionary.
void awakeWithDictionary(View *self, const Dictionary *dictionary)
Wakes this View with the specified Dictionary.
Definition: Box.c:50

◆ dealloc()

static void dealloc ( Object *  self)
static
See also
Object::dealloc(Object *)

Definition at line 61 of file HSVColorPicker.c.

61 {
62
63 HSVColorPicker *this = (HSVColorPicker *) self;
64
65 release(this->colorView);
66 release(this->hueSlider);
67 release(this->hueInput);
68 release(this->saturationSlider);
69 release(this->saturationInput);
70 release(this->valueSlider);
71 release(this->valueInput);
72 release(this->stackView);
73
74 super(Object, self, dealloc);
75}
static void dealloc(Object *self)

◆ didSetComponent()

static void didSetComponent ( Slider slider,
double  value 
)
static

SliderDelegate callback for color component modification.

Definition at line 35 of file HSVColorPicker.c.

35 {
36
37 HSVColorPicker *this = (HSVColorPicker *) slider->delegate.self;
38
39 if (slider == this->hueSlider) {
40 this->hue = value;
41 } else if (slider == this->saturationSlider) {
42 this->saturation = value;
43 } else if (slider == this->valueSlider) {
44 this->value = value;
45 } else {
46 assert(false);
47 }
48
49 $((View *) this, updateBindings);
50
51 if (this->delegate.didPickColor) {
52 this->delegate.didPickColor(this, this->hue, this->saturation, this->value);
53 }
54}
ident self
The delegate self-reference.
Definition: Slider.h:47
SliderDelegate delegate
The delegate.
Definition: Slider.h:83
void updateBindings(View *self)
Updates data bindings, prompting the appropriate layout changes.

◆ init()

static View * init ( View self)
static
See also
View::init(View *)

Definition at line 108 of file HSVColorPicker.c.

108 {
109 return (View *) $((HSVColorPicker *) self, initWithFrame, NULL);
110}
Box * initWithFrame(Box *self, const SDL_Rect *frame)
Initializes this Box with the given frame.
Definition: Box.c:92

◆ initialize()

static void initialize ( Class *  clazz)
static
See also
Class::initialize(Class *)

Definition at line 241 of file HSVColorPicker.c.

241 {
242
243 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
244
245 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
246 ((ViewInterface *) clazz->interface)->init = init;
247 ((ViewInterface *) clazz->interface)->updateBindings = updateBindings;
248
249 ((HSVColorPickerInterface *) clazz->interface)->initWithFrame = initWithFrame;
250 ((HSVColorPickerInterface *) clazz->interface)->rgbColor = rgbColor;
251 ((HSVColorPickerInterface *) clazz->interface)->setColor = setColor;
252 ((HSVColorPickerInterface *) clazz->interface)->setRGBColor = setRGBColor;
253}
CollectionView * init(CollectionView *self, const SDL_Rect *frame)
Initializes this CollectionView with the specified frame and style.
void setColor(HSVColorPicker *self, double hue, double saturation, double value)
Sets the color of the HSVColorPicker.
SDL_Color rgbColor(const HSVColorPicker *self)
void setRGBColor(HSVColorPicker *self, const SDL_Color *color)
Sets the color of the HSVColorPicker.

◆ initWithFrame()

static HSVColorPicker * initWithFrame ( HSVColorPicker self,
const SDL_Rect *  frame 
)
static

Definition at line 134 of file HSVColorPicker.c.

134 {
135
136 self = (HSVColorPicker *) super(Control, self, initWithFrame, frame);
137 if (self) {
138
139 self->stackView = $(alloc(StackView), initWithFrame, NULL);
140 assert(self->stackView);
141
142 $((View *) self, addSubview, (View *) self->stackView);
143
144 self->colorView = $(alloc(View), initWithFrame, &MakeRect(0, 0, 0, 24));
145 assert(self->colorView);
146
147 $(self->colorView, addClassName, "colorView");
148 $((View *) self->stackView, addSubview, self->colorView);
149
150 self->hueSlider = $(alloc(Slider), initWithFrame, NULL);
151 assert(self->hueSlider);
152
153 self->hueSlider->delegate.self = self;
154 self->hueSlider->delegate.didSetValue = didSetComponent;
155 self->hueSlider->max = 360.0;
156 $(self->hueSlider, setLabelFormat, "%.0lf");
157
158 self->hueInput = $(alloc(Input), initWithFrame, NULL);
159 assert(self->hueInput);
160
161 $(self->hueInput, setControl, (Control *) self->hueSlider);
162 $(self->hueInput->label->text, setText, "H");
163
164 $((View *) self->stackView, addSubview, (View *) self->hueInput);
165
166 self->saturationSlider = $(alloc(Slider), initWithFrame, NULL);
167 assert(self->saturationSlider);
168
169 self->saturationSlider->delegate.self = self;
170 self->saturationSlider->delegate.didSetValue = didSetComponent;
171 self->saturationSlider->max = 1.0;
172 $(self->saturationSlider, setLabelFormat, "%.3f");
173
174 self->saturationInput = $(alloc(Input), initWithFrame, NULL);
175 assert(self->saturationInput);
176
177 $(self->saturationInput, setControl, (Control *) self->saturationSlider);
178 $(self->saturationInput->label->text, setText, "S");
179
180 $((View *) self->stackView, addSubview, (View *) self->saturationInput);
181
182 self->valueSlider = $(alloc(Slider), initWithFrame, NULL);
183 assert(self->valueSlider);
184
185 self->valueSlider->delegate.self = self;
186 self->valueSlider->delegate.didSetValue = didSetComponent;
187 self->valueSlider->max = 1.0;
188 $(self->valueSlider, setLabelFormat, "%.3f");
189
190 self->valueInput = $(alloc(Input), initWithFrame, NULL);
191 assert(self->valueInput);
192
193 $(self->valueInput, setControl, (Control *) self->valueSlider);
194 $(self->valueInput->label->text, setText, "V");
195
196 $((View *) self->stackView, addSubview, (View *) self->valueInput);
197
198 $(self, setColor, 0.0, 1.0, 1.0);
199 }
200
201 return self;
202}
static void didSetComponent(Slider *slider, double value)
SliderDelegate callback for color component modification.
#define MakeRect(x, y, w, h)
Creates an SDL_Rect with the given origin and size.
Definition: Types.h:74
Controls are Views which capture events and dispatch Actions.
Definition: Control.h:83
StackView * stackView
The StackView.
An Input stacks a Label with a Control.
Definition: Input.h:57
void setControl(Input *self, Control *control)
Sets this Input's Control.
Definition: Input.c:113
void setLabelFormat(ProgressBar *self, const char *labelFormat)
Changes this ProgressBar's label format and calls appropriate update functions.
Definition: ProgressBar.c:145
A Control allowing users to drag a handle to select a numeric value.
Definition: Slider.h:62
StackViews are containers that manage the arrangement of their subviews.
Definition: StackView.h:68
void setText(Text *self, const char *text)
Sets this Text's text.
Definition: Text.c:261
void addSubview(View *self, View *subview)
Adds a subview to this view, to be drawn above its siblings.
Definition: PageView.c:35
void addClassName(View *self, const char *className)
Adds the given class name to this View.
Definition: View.c:120

◆ rgbColor()

static SDL_Color rgbColor ( const HSVColorPicker self)
static

Definition at line 208 of file HSVColorPicker.c.

208 {
209 return MVC_HSVToRGB(self->hue, self->saturation, self->value);
210}
OBJECTIVELYMVC_EXPORT SDL_Color MVC_HSVToRGB(double hue, double saturation, double value)
Converts the given HSV components to an RGB color.
Definition: Colors.c:684
double hue
The color components.

◆ setColor()

static void setColor ( HSVColorPicker self,
double  hue,
double  saturation,
double  value 
)
static

Definition at line 216 of file HSVColorPicker.c.

216 {
217
218 self->hue = hue;
219 self->saturation = saturation;
220 self->value = value;
221
222 $((View *) self, updateBindings);
223}

◆ setRGBColor()

static void setRGBColor ( HSVColorPicker self,
const SDL_Color *  color 
)
static

Definition at line 229 of file HSVColorPicker.c.

229 {
230
231 MVC_RGBToHSV(color, &self->hue, &self->saturation, &self->value);
232
233 $((View *) self, updateBindings);
234}
OBJECTIVELYMVC_EXPORT void MVC_RGBToHSV(const SDL_Color *color, double *hue, double *saturation, double *value)
Converts the given RGB color to HSV components.
Definition: Colors.c:732

◆ updateBindings()

static void updateBindings ( View self)
static
See also
View::updateBindings(View *)

Definition at line 115 of file HSVColorPicker.c.

115 {
116
117 super(View, self, updateBindings);
118
119 HSVColorPicker *this = (HSVColorPicker *) self;
120
121 $(this->hueSlider, setValue, this->hue);
122 $(this->saturationSlider, setValue, this->saturation);
123 $(this->valueSlider, setValue, this->value);
124
125 this->colorView->backgroundColor = $(this, rgbColor);
126}
void setValue(const ProgressBar *self, double value)
Sets the value, which is clamped to min and max.