ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
HueColorPicker.c
Go to the documentation of this file.
1/*
2 * ObjectivelyMVC: Object oriented MVC framework for OpenGL, SDL2 and GNU 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#include <assert.h>
24
25#include "HueColorPicker.h"
26
27#define _Class _HueColorPicker
28
29#pragma mark - Actions & delegates
30
34static void didSetHue(Slider *slider, double value) {
35
36 HueColorPicker *this = (HueColorPicker *) slider->delegate.self;
37
38 this->hue = value;
39
40 $((View *) this, updateBindings);
41
42 if (this->delegate.didPickColor) {
43 this->delegate.didPickColor(this, this->hue, this->saturation, this->value);
44 }
45}
46
47#pragma mark - Object
48
52static void dealloc(Object *self) {
53
54 HueColorPicker *this = (HueColorPicker *) self;
55
56 release(this->colorView);
57 release(this->hueInput);
58 release(this->hueSlider);
59 release(this->stackView);
60
61 super(Object, self, dealloc);
62}
63
64#pragma mark - View
65
69static void awakeWithDictionary(View *self, const Dictionary *dictionary) {
70
71 super(View, self, awakeWithDictionary, dictionary);
72
73 HueColorPicker *this = (HueColorPicker *) self;
74
75 const Inlet inlets[] = MakeInlets(
76 MakeInlet("colorView", InletTypeView, &this->colorView, NULL),
77 MakeInlet("hue", InletTypeDouble, &this->hue, NULL),
78 MakeInlet("saturation", InletTypeDouble, &this->saturation, NULL),
79 MakeInlet("value", InletTypeDouble, &this->value, NULL),
80 MakeInlet("hueInput", InletTypeView, &this->hueInput, NULL),
81 MakeInlet("hueSlider", InletTypeView, &this->hueSlider, NULL),
82 MakeInlet("stackView", InletTypeView, &this->stackView, NULL)
83 );
84
85 $(self, bind, inlets, dictionary);
86}
87
91static View *init(View *self) {
92 return (View *) $((HueColorPicker *) self, initWithFrame, NULL);
93}
94
98static void updateBindings(View *self) {
99
100 super(View, self, updateBindings);
101
102 HueColorPicker *this = (HueColorPicker *) self;
103
104 $(this->hueSlider, setValue, this->hue);
105
106 this->colorView->backgroundColor = $(this, rgbColor);
107}
108
109#pragma mark - HueColorPicker
110
115static HueColorPicker *initWithFrame(HueColorPicker *self, const SDL_Rect *frame) {
116
117 self = (HueColorPicker *) super(Control, self, initWithFrame, frame);
118 if (self) {
119
120 self->stackView = $(alloc(StackView), initWithFrame, NULL);
121 assert(self->stackView);
122
123 $((View *) self, addSubview, (View *) self->stackView);
124
125 self->colorView = $(alloc(View), initWithFrame, &MakeRect(0, 0, 0, 24));
126 assert(self->colorView);
127
128 $(self->colorView, addClassName, "colorView");
129 $((View *) self->stackView, addSubview, self->colorView);
130
131 self->hueSlider = $(alloc(Slider), initWithFrame, NULL);
132 assert(self->hueSlider);
133
134 self->hueSlider->delegate.self = self;
135 self->hueSlider->delegate.didSetValue = didSetHue;
136 self->hueSlider->max = 360.0;
137 $(self->hueSlider, setLabelFormat, "%.0lf");
138
139 self->hueInput = $(alloc(Input), initWithFrame, NULL);
140 assert(self->hueInput);
141
142 $(self->hueInput, setControl, (Control *) self->hueSlider);
143 $(self->hueInput->label->text, setText, "H");
144
145 $((View *) self->stackView, addSubview, (View *) self->hueInput);
146
147 $(self, setColor, 0.0, 1.0, 1.0);
148 }
149
150 return self;
151}
152
157static SDL_Color rgbColor(const HueColorPicker *self) {
158 return MVC_HSVToRGB(self->hue, self->saturation, self->value);
159}
160
165static void setColor(HueColorPicker *self, double hue, double saturation, double value) {
166
167 self->hue = hue;
168 self->saturation = saturation;
169 self->value = value;
170
171 $((View *) self, updateBindings);
172}
173
178static void setRGBColor(HueColorPicker *self, const SDL_Color *color) {
179
180 MVC_RGBToHSV(color, &self->hue, &self->saturation, &self->value);
181
182 $((View *) self, updateBindings);
183}
184
185#pragma mark - Class lifecycle
186
190static void initialize(Class *clazz) {
191
192 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
193
194 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
195 ((ViewInterface *) clazz->interface)->init = init;
196 ((ViewInterface *) clazz->interface)->updateBindings = updateBindings;
197
198 ((HueColorPickerInterface *) clazz->interface)->initWithFrame = initWithFrame;
199 ((HueColorPickerInterface *) clazz->interface)->rgbColor = rgbColor;
200 ((HueColorPickerInterface *) clazz->interface)->setColor = setColor;
201 ((HueColorPickerInterface *) clazz->interface)->setRGBColor = setRGBColor;
202}
203
208Class *_HueColorPicker(void) {
209 static Class *clazz;
210 static Once once;
211
212 do_once(&once, {
213 clazz = _initialize(&(const ClassDef) {
214 .name = "HueColorPicker",
215 .superclass = _Control(),
216 .instanceSize = sizeof(HueColorPicker),
217 .interfaceOffset = offsetof(HueColorPicker, interface),
218 .interfaceSize = sizeof(HueColorPickerInterface),
220 });
221 });
222
223 return clazz;
224}
225
226#undef _Class
static void didSetHue(Slider *slider, double value)
SliderDelegate callback for hue modification.
static void dealloc(Object *self)
static void initialize(Class *clazz)
Hue color picker.
#define MakeRect(x, y, w, h)
Creates an SDL_Rect with the given origin and size.
Definition: Types.h:74
@ 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
Box * initWithFrame(Box *self, const SDL_Rect *frame)
Initializes this Box with the given frame.
Definition: Box.c:92
CollectionView * init(CollectionView *self, const SDL_Rect *frame)
Initializes this CollectionView with the specified frame and style.
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
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
Controls are Views which capture events and dispatch Actions.
Definition: Control.h:83
Class * _Control(void)
The Control archetype.
Definition: Control.c:379
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.
The HueColorPicker type.
StackView * stackView
The StackView.
Class * _HueColorPicker(void)
The HueColorPicker archetype.
double hue
The color componenets.
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
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
void setValue(const ProgressBar *self, double value)
Sets the value, which is clamped to min and max.
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
void setText(Text *self, const char *text)
Sets this Text's text.
Definition: Text.c:261
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition: View.h:133
void updateBindings(View *self)
Updates data bindings, prompting the appropriate layout changes.
_Bool bind(View *self, const Inlet *inlets, const Dictionary *dictionary)
Performs data binding for the Inlets described in dictionary.
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
void awakeWithDictionary(View *self, const Dictionary *dictionary)
Wakes this View with the specified Dictionary.
Definition: Box.c:50