ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
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.
 
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 261 of file HSVColorPicker.c.

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

◆ awakeWithDictionary()

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

Definition at line 84 of file HSVColorPicker.c.

84 {
85
86 super(View, self, awakeWithDictionary, dictionary);
87
88 HSVColorPicker *this = (HSVColorPicker *) self;
89
90 const Inlet inlets[] = MakeInlets(
91 MakeInlet("colorView", InletTypeView, &this->colorView, NULL),
92 MakeInlet("hue", InletTypeDouble, &this->hue, NULL),
93 MakeInlet("saturation", InletTypeDouble, &this->saturation, NULL),
94 MakeInlet("value", InletTypeDouble, &this->value, NULL),
95 MakeInlet("hueSlider", InletTypeView, &this->hueSlider, NULL),
96 MakeInlet("hueInput", InletTypeView, &this->hueInput, NULL),
97 MakeInlet("saturationSlider", InletTypeView, &this->saturationSlider, NULL),
98 MakeInlet("saturationInput", InletTypeView, &this->saturationInput, NULL),
99 MakeInlet("valueSlider", InletTypeView, &this->valueSlider, NULL),
100 MakeInlet("valueInput", InletTypeView, &this->valueInput, NULL),
101 MakeInlet("stackView", InletTypeView, &this->stackView, NULL)
102 );
103
104 $(self, bind, inlets, dictionary);
105}
static void awakeWithDictionary(View *self, const Dictionary *dictionary)
@ 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:134

◆ 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 memset(&this->delegate, 0, sizeof(this->delegate));
66
67 release(this->colorView);
68 release(this->hueSlider);
69 release(this->hueInput);
70 release(this->saturationSlider);
71 release(this->saturationInput);
72 release(this->valueSlider);
73 release(this->valueInput);
74 release(this->stackView);
75
76 super(Object, self, dealloc);
77}
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}
static void updateBindings(View *self)
ident self
The delegate self-reference.
Definition Slider.h:47
SliderDelegate delegate
The delegate.
Definition Slider.h:83

◆ init()

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

Definition at line 110 of file HSVColorPicker.c.

110 {
111 return (View *) $((HSVColorPicker *) self, initWithFrame, NULL);
112}
static HSVColorPicker * initWithFrame(HSVColorPicker *self, const SDL_Rect *frame)

◆ initialize()

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

Definition at line 243 of file HSVColorPicker.c.

243 {
244
245 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
246
247 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
248 ((ViewInterface *) clazz->interface)->init = init;
249 ((ViewInterface *) clazz->interface)->updateBindings = updateBindings;
250
251 ((HSVColorPickerInterface *) clazz->interface)->initWithFrame = initWithFrame;
252 ((HSVColorPickerInterface *) clazz->interface)->rgbColor = rgbColor;
253 ((HSVColorPickerInterface *) clazz->interface)->setColor = setColor;
254 ((HSVColorPickerInterface *) clazz->interface)->setRGBColor = setRGBColor;
255}
static View * init(View *self)
static void setRGBColor(HSVColorPicker *self, const SDL_Color *color)
static void setColor(HSVColorPicker *self, double hue, double saturation, double value)
static SDL_Color rgbColor(const HSVColorPicker *self)
SDL_Color rgbColor(const HSVColorPicker *self)
void updateBindings(View *self)
Updates data bindings, prompting the appropriate layout changes.

◆ initWithFrame()

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

Definition at line 136 of file HSVColorPicker.c.

136 {
137
138 self = (HSVColorPicker *) super(Control, self, initWithFrame, frame);
139 if (self) {
140
141 self->stackView = $(alloc(StackView), initWithFrame, NULL);
142 assert(self->stackView);
143
144 $((View *) self, addSubview, (View *) self->stackView);
145
146 self->colorView = $(alloc(View), initWithFrame, &MakeRect(0, 0, 0, 24));
147 assert(self->colorView);
148
149 $(self->colorView, addClassName, "colorView");
150 $((View *) self->stackView, addSubview, self->colorView);
151
152 self->hueSlider = $(alloc(Slider), initWithFrame, NULL);
153 assert(self->hueSlider);
154
155 self->hueSlider->delegate.self = self;
156 self->hueSlider->delegate.didSetValue = didSetComponent;
157 self->hueSlider->max = 360.0;
158 $(self->hueSlider, setLabelFormat, "%.0lf");
159
160 self->hueInput = $(alloc(Input), initWithFrame, NULL);
161 assert(self->hueInput);
162
163 $(self->hueInput, setControl, (Control *) self->hueSlider);
164 $(self->hueInput->label->text, setText, "H");
165
166 $((View *) self->stackView, addSubview, (View *) self->hueInput);
167
168 self->saturationSlider = $(alloc(Slider), initWithFrame, NULL);
169 assert(self->saturationSlider);
170
171 self->saturationSlider->delegate.self = self;
172 self->saturationSlider->delegate.didSetValue = didSetComponent;
173 self->saturationSlider->max = 1.0;
174 $(self->saturationSlider, setLabelFormat, "%.3f");
175
176 self->saturationInput = $(alloc(Input), initWithFrame, NULL);
177 assert(self->saturationInput);
178
179 $(self->saturationInput, setControl, (Control *) self->saturationSlider);
180 $(self->saturationInput->label->text, setText, "S");
181
182 $((View *) self->stackView, addSubview, (View *) self->saturationInput);
183
184 self->valueSlider = $(alloc(Slider), initWithFrame, NULL);
185 assert(self->valueSlider);
186
187 self->valueSlider->delegate.self = self;
188 self->valueSlider->delegate.didSetValue = didSetComponent;
189 self->valueSlider->max = 1.0;
190 $(self->valueSlider, setLabelFormat, "%.3f");
191
192 self->valueInput = $(alloc(Input), initWithFrame, NULL);
193 assert(self->valueInput);
194
195 $(self->valueInput, setControl, (Control *) self->valueSlider);
196 $(self->valueInput->label->text, setText, "V");
197
198 $((View *) self->stackView, addSubview, (View *) self->valueInput);
199
200 $(self, setColor, 0.0, 1.0, 1.0);
201 }
202
203 return self;
204}
static void didSetComponent(Slider *slider, double value)
SliderDelegate callback for color component modification.
static void setControl(Input *self, Control *control)
Definition Input.c:113
static void addSubview(View *self, View *subview)
Definition PageView.c:49
static void setLabelFormat(ProgressBar *self, const char *labelFormat)
static void setText(Text *self, const char *text)
Definition Text.c:532
static void addClassName(View *self, const char *className)
Definition View.c:159
Controls are Views which capture and respond to events.
Definition Control.h:83
StackView * stackView
The StackView.
An Input stacks a Label with a Control.
Definition Input.h:57
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

◆ rgbColor()

static SDL_Color rgbColor ( const HSVColorPicker self)
static

Definition at line 210 of file HSVColorPicker.c.

210 {
211 return MVC_HSVToRGB(self->hue, self->saturation, self->value);
212}
SDL_Color MVC_HSVToRGB(double hue, double saturation, double value)
Definition Colors.c:685
double hue
The color components.

◆ setColor()

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

Definition at line 218 of file HSVColorPicker.c.

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

◆ setRGBColor()

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

Definition at line 231 of file HSVColorPicker.c.

231 {
232
233 MVC_RGBToHSV(color, &self->hue, &self->saturation, &self->value);
234
235 $((View *) self, updateBindings);
236}
void MVC_RGBToHSV(const SDL_Color *color, double *hue, double *saturation, double *value)
Definition Colors.c:733

◆ updateBindings()

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

Definition at line 117 of file HSVColorPicker.c.

117 {
118
119 super(View, self, updateBindings);
120
121 HSVColorPicker *this = (HSVColorPicker *) self;
122
123 $(this->hueSlider, setValue, this->hue);
124 $(this->saturationSlider, setValue, this->saturation);
125 $(this->valueSlider, setValue, this->value);
126
127 this->colorView->backgroundColor = $(this, rgbColor);
128}
static void setValue(ProgressBar *self, double value)