ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
HueColorPicker.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#include <assert.h>
24
25#include "HueColorPicker.h"
26
27#define _Class _HueColorPicker
28
29#pragma mark - 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, NULL);
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 memset(&this->delegate, 0, sizeof(this->delegate));
57
58 release(this->colorView);
59 release(this->hueInput);
60 release(this->hueSlider);
61 release(this->stackView);
62
63 super(Object, self, dealloc);
64}
65
66#pragma mark - View
67
71static void awakeWithDictionary(View *self, const Dictionary *dictionary) {
72
73 super(View, self, awakeWithDictionary, dictionary);
74
75 HueColorPicker *this = (HueColorPicker *) self;
76
77 const Inlet inlets[] = MakeInlets(
78 MakeInlet("colorView", InletTypeView, &this->colorView, NULL),
79 MakeInlet("hue", InletTypeDouble, &this->hue, NULL),
80 MakeInlet("saturation", InletTypeDouble, &this->saturation, NULL),
81 MakeInlet("value", InletTypeDouble, &this->value, NULL),
82 MakeInlet("hueInput", InletTypeView, &this->hueInput, NULL),
83 MakeInlet("hueSlider", InletTypeView, &this->hueSlider, NULL),
84 MakeInlet("stackView", InletTypeView, &this->stackView, NULL)
85 );
86
87 $(self, bind, inlets, dictionary);
88}
89
93static View *init(View *self) {
94 return (View *) $((HueColorPicker *) self, initWithFrame, NULL);
95}
96
100static void updateBindings(View *self, ident data) {
101
102 super(View, self, updateBindings, data);
103
104 HueColorPicker *this = (HueColorPicker *) self;
105
106 $(this->hueSlider, setValue, this->hue);
107
108 const SDL_Color color = $(this, rgbColor);
109 $(this->colorView->style, addColorAttribute, "background-color", &color);
110 $(this->colorView, invalidateStyle);
111}
112
113#pragma mark - HueColorPicker
114
119static HueColorPicker *initWithFrame(HueColorPicker *self, const SDL_Rect *frame) {
120
121 self = (HueColorPicker *) super(Control, self, initWithFrame, frame);
122 if (self) {
123
124 self->stackView = $(alloc(StackView), initWithFrame, NULL);
125 assert(self->stackView);
126
127 $((View *) self, addSubview, (View *) self->stackView);
128
129 self->colorView = $(alloc(View), initWithFrame, &MakeRect(0, 0, 0, 24));
130 assert(self->colorView);
131
132 $(self->colorView, addClassName, "colorView");
133 $((View *) self->stackView, addSubview, self->colorView);
134
135 self->hueSlider = $(alloc(Slider), initWithFrame, NULL);
136 assert(self->hueSlider);
137
138 self->hueSlider->delegate.self = self;
139 self->hueSlider->delegate.didSetValue = didSetHue;
140 self->hueSlider->max = 360.0;
141 $(self->hueSlider, setLabelFormat, "%.0lf");
142
143 self->hueInput = $(alloc(Input), initWithFrame, NULL);
144 assert(self->hueInput);
145
146 $(self->hueInput, setControl, (Control *) self->hueSlider);
147 $(self->hueInput->label->text, setText, "H");
148
149 $((View *) self->stackView, addSubview, (View *) self->hueInput);
150
151 $(self, setColor, 0.0, 1.0, 1.0);
152 }
153
154 return self;
155}
156
161static SDL_Color rgbColor(const HueColorPicker *self) {
162 return MVC_HSVToRGB(self->hue, self->saturation, self->value);
163}
164
169static void setColor(HueColorPicker *self, double hue, double saturation, double value) {
170
171 self->hue = hue;
172 self->saturation = saturation;
173 self->value = value;
174
175 $((View *) self, updateBindings, NULL);
176}
177
182static void setRGBColor(HueColorPicker *self, const SDL_Color *color) {
183
184 MVC_RGBToHSV(color, &self->hue, &self->saturation, &self->value);
185
186 $((View *) self, updateBindings, NULL);
187}
188
189#pragma mark - Class lifecycle
190
194static void initialize(Class *clazz) {
195
196 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
197
198 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
199 ((ViewInterface *) clazz->interface)->init = init;
200 ((ViewInterface *) clazz->interface)->updateBindings = updateBindings;
201
202 ((HueColorPickerInterface *) clazz->interface)->initWithFrame = initWithFrame;
203 ((HueColorPickerInterface *) clazz->interface)->rgbColor = rgbColor;
204 ((HueColorPickerInterface *) clazz->interface)->setColor = setColor;
205 ((HueColorPickerInterface *) clazz->interface)->setRGBColor = setRGBColor;
206}
207
212Class *_HueColorPicker(void) {
213 static Class *clazz;
214 static Once once;
215
216 do_once(&once, {
217 clazz = _initialize(&(const ClassDef) {
218 .name = "HueColorPicker",
219 .superclass = _Control(),
220 .instanceSize = sizeof(HueColorPicker),
221 .interfaceSize = sizeof(HueColorPickerInterface),
222 .initialize = initialize,
223 });
224 });
225
226 return clazz;
227}
228
229#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:414
static SDL_Color rgbColor(const HueColorPicker *self)
static void setColor(HueColorPicker *self, double hue, double saturation, double value)
static View * init(View *self)
static void awakeWithDictionary(View *self, const Dictionary *dictionary)
static HueColorPicker * initWithFrame(HueColorPicker *self, const SDL_Rect *frame)
Class * _HueColorPicker(void)
static void updateBindings(View *self, ident data)
static void didSetHue(Slider *slider, double value)
SliderDelegate callback for hue modification.
static void dealloc(Object *self)
static void initialize(Class *clazz)
static void setRGBColor(HueColorPicker *self, const SDL_Color *color)
Hue 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 addColorAttribute(Style *self, const char *attr, const SDL_Color *color)
Definition Style.c:253
static void setText(Text *self, const char *text)
Definition Text.c:899
@ InletTypeDouble
Definition View+JSON.h:67
@ InletTypeView
Definition View+JSON.h:133
#define MakeInlets(...)
Creates a null-termianted array of Inlets.
Definition View+JSON.h:215
#define MakeInlet(name, type, dest, data)
Creates an Inlet with the specified parameters.
Definition View+JSON.h:210
static void addClassName(View *self, const char *className)
Definition View.c:183
static void invalidateStyle(View *self)
Definition View.c:1092
Controls are Views which capture and respond to events.
Definition Control.h:83
The HueColorPicker type.
SDL_Color rgbColor(const HueColorPicker *self)
StackView * stackView
The StackView.
double hue
The color componenets.
Inlets enable inbound data binding of View attributes through JSON.
Definition View+JSON.h:149
An Input stacks a Label with a Control.
Definition Input.h:57
ident self
The delegate self-reference.
Definition Slider.h:50
A Control allowing users to drag a handle to select a numeric value.
Definition Slider.h:65
SliderDelegate delegate
The delegate.
Definition Slider.h:86
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:225
void updateBindings(View *self, ident data)
Updates data bindings, prompting the appropriate layout changes.