ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
HSVColorPicker.c
Go to the documentation of this file.
1/*
2 * ObjectivelyMVC: Object oriented MVC framework for SDL3 and C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include <assert.h>
25
26#include "HSVColorPicker.h"
27
28#define _Class _HSVColorPicker
29
30#pragma mark - Delegates
31
35static void didSetComponent(Slider *slider, double value) {
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}
55
56#pragma mark - Object
57
61static void dealloc(Object *self) {
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}
78
79#pragma mark - View
80
84static void awakeWithDictionary(View *self, const Dictionary *dictionary) {
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}
106
110static View *init(View *self) {
111 return (View *) $((HSVColorPicker *) self, initWithFrame, NULL);
112}
113
117static void updateBindings(View *self) {
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}
129
130#pragma mark - HSVColorPicker
131
136static HSVColorPicker *initWithFrame(HSVColorPicker *self, const SDL_Rect *frame) {
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}
205
210static SDL_Color rgbColor(const HSVColorPicker *self) {
211 return MVC_HSVToRGB(self->hue, self->saturation, self->value);
212}
213
218static void setColor(HSVColorPicker *self, double hue, double saturation, double value) {
219
220 self->hue = hue;
221 self->saturation = saturation;
222 self->value = value;
223
224 $((View *) self, updateBindings);
225}
226
231static void setRGBColor(HSVColorPicker *self, const SDL_Color *color) {
232
233 MVC_RGBToHSV(color, &self->hue, &self->saturation, &self->value);
234
235 $((View *) self, updateBindings);
236}
237
238#pragma mark - Class lifecycle
239
243static void initialize(Class *clazz) {
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}
256
261Class *_HSVColorPicker(void) {
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}
278
279#undef _Class
SDL_Color MVC_HSVToRGB(double hue, double saturation, double value)
Definition Colors.c:685
void MVC_RGBToHSV(const SDL_Color *color, double *hue, double *saturation, double *value)
Definition Colors.c:733
Class * _Control(void)
Definition Control.c:391
Class * _HSVColorPicker(void)
static View * init(View *self)
static void awakeWithDictionary(View *self, const Dictionary *dictionary)
static void setRGBColor(HSVColorPicker *self, const SDL_Color *color)
static HSVColorPicker * initWithFrame(HSVColorPicker *self, const SDL_Rect *frame)
static void setColor(HSVColorPicker *self, double hue, double saturation, double value)
static void dealloc(Object *self)
static SDL_Color rgbColor(const HSVColorPicker *self)
static void initialize(Class *clazz)
static void updateBindings(View *self)
static void didSetComponent(Slider *slider, double value)
SliderDelegate callback for color component modification.
HSV color picker.
static void setControl(Input *self, Control *control)
Definition Input.c:113
static void addSubview(View *self, View *subview)
Definition PageView.c:49
static void setValue(ProgressBar *self, double value)
static void setLabelFormat(ProgressBar *self, const char *labelFormat)
static void setText(Text *self, const char *text)
Definition Text.c:532
@ 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
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
The HSVColorPicker type.
SDL_Color rgbColor(const HSVColorPicker *self)
double hue
The color components.
StackView * stackView
The StackView.
Inlets enable inbound data binding of View attributes through JSON.
Definition View+JSON.h:155
An Input stacks a Label with a Control.
Definition Input.h:57
ident self
The delegate self-reference.
Definition Slider.h:47
A Control allowing users to drag a handle to select a numeric value.
Definition Slider.h:62
SliderDelegate delegate
The delegate.
Definition Slider.h:83
StackViews are containers that manage the arrangement of their subviews.
Definition StackView.h:68
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:134
void updateBindings(View *self)
Updates data bindings, prompting the appropriate layout changes.