-
Notifications
You must be signed in to change notification settings - Fork 7
/
widget.js
325 lines (255 loc) · 6.58 KB
/
widget.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/**
* Exposes a view displaying the HSV color space that can have any size and works
* with any default system unit. By tapping or dragging the user can #change the
* selected color, which will be returned in HSV, RGB and HEX.
*
* Inspired by: [https://github.com/JigarM/TiColorPicker/]()
*
* # Built-in spectrums
*
* The default color spectrum is {@link Widgets.nlFokkezbColor.lib.ghsv ghsv},
* but you can also set this to {@link Widgets.nlFokkezbColor.lib.hsvg hsvg}
* or {@link Widgets.nlFokkezbColor.lib.hsv hsv}.
*
* # Custom spectrums
*
* You can also set the `spectrum` argument to a custom type. Just pass an
* object exposing the required `image` property (start your path by `/` to
* prevent the widget to prepend the widget asset path) and the `pc2hsv`
* and `hsv2pc` methods as with the builtins.
*
* @class Widgets.nlFokkezbColor.controllers.widget
* @requires Widgets.nlFokkezbColor.lib.convert
*/
/* exported onPostlayout, onColorChange */
/** @property {Object} convert The Widgets.nlFokkezbColor.lib.convert library. */
$.convert = require(WPATH('convert'));
/**
* @property {Object} color Current color
*
* @property {Object} color.hsv Color in HSV
* @property {Number} color.hsv.h Hue (0 - 359)
* @property {Number} color.hsv.s Saturation (0 - 100)
* @property {Number} color.hsv.v Value (0 - 100)
*
* @property {Object} color.rgb Color in RGB
* @property {Number} color.rgb.r Red (0 - 255)
* @property {Number} color.rgb.g Green (0 - 255)
* @property {Number} color.rgb.b Blue (0 - 255)
*
* @property {String} color.hex Color in HEX
*
* @property {String} color.bw Either `white` or `black` depending on contrast.
*/
Object.defineProperty($, 'color', {
get: getColor,
set: setColor
});
/**
* Set the current color.
*
* This can be formatted as #color or any of it's properties.
*
* @param {Object|String} clr Color to set
*/
$.setColor = setColor;
/**
* Get the current color.
*
* @return {Object} Current #color
*/
$.getColor = getColor;
/**
* Show th color picker
*/
$.show = function () {
$.image.show();
};
/**
* Hide the color picker
*/
$.hide = function () {
$.image.hide();
};
$.applyProperties = applyProperties;
/**
* Fired when the user changes the color.
*
* @event change
*
* @param {Object} e Event
* @param {Object} e.color Current #color
*/
// private vars
var spectrum, rect, unit, color;
/**
* Constructor for the widget.
*
* @method Controller
* @param {Object} args Arguments passed to the controller, which will be applied to the main view.
* @param {Object|String} [args.color] The color to set.
* @param {Object|String} [args.spectrum] One of the built-in color spectrum names or an object exposing one.
* @param {Array} [args.children] Child views to overlay.
*/
(function constuctor(args) {
if (args.children) {
_.each(args.children, function(child) {
$.image.add(child);
});
}
args.spectrum = args.spectrum || 'ghsv';
applyProperties(args);
})(arguments[0] || {});
/**
* Set properties for the widget.
*
* @param {Object} prop Properties to apply to the main view.
* @param {Object|String} [prop.color] The color to set.
* @param {Object|String} [prop.spectrum] One of the built-in color spectrum names or an object exposing one.
*/
function applyProperties(prop) {
if (prop.spectrum) {
spectrum = _.isObject(prop.spectrum) ? prop.spectrum : require(WPATH(prop.spectrum));
var image = spectrum.image;
if (image.substr(0, 1) !== '/') {
image = WPATH(image);
}
prop.backgroundImage = image;
}
var apply = _.omit(prop, 'id', '__parentSymbol', '__itemTemplate', '$model', 'children', 'color', 'spectrum');
if (_.size(apply) > 0) {
$.image.applyProperties(apply);
}
if (prop.color) {
setColor(prop.color);
} else if (prop.spectrum) {
setCircle();
}
}
function setColor(clr) {
parseColor(clr);
setCircle();
}
function getColor() {
return color;
}
function onPostlayout(e) { // jshint unused:false
$.image.removeEventListener('postlayout', onPostlayout);
rect = $.image.rect;
setCircle();
}
function onColorChange(e) {
var x = e.x,
y = e.y;
// Android doesn't return these in system unit, but always in px
if (OS_ANDROID) {
var def = getDefaultUnit();
if (def !== Ti.UI.UNIT_PX) {
x = Ti.UI.convertUnits(x.toString() + 'px', def);
y = Ti.UI.convertUnits(y.toString() + 'px', def);
}
}
x = Math.max(0, Math.min(rect.width, x));
y = Math.max(0, Math.min(rect.height, y));
// convert px to pc
var pc = {
x: (x / rect.width) * 100,
y: (y / rect.height) * 100
};
// convert pc to hsv
var hsv = spectrum.pc2hsv(pc);
// convert to other spectrums
var rgb = $.convert.hsv2rgb(hsv);
var hex = $.convert.rgb2hex(rgb);
var bw = $.convert.hsv2bw(hsv);
// save as current color
color = {
hsv: hsv,
rgb: rgb,
hex: hex,
bw: bw
};
// position circle
$.circle.applyProperties({
center: {
x: x,
y: y
},
borderColor: bw
});
$.circle.show();
// broadcast change
$.trigger('change', color);
}
function onTouchend(e){
$.trigger('touchend', e);
}
function parseColor(clr) {
var hsv, rgb, hex, bw;
if (_.isObject(clr)) {
if (clr.h) {
hsv = clr;
rgb = $.convert.hsv2rgb(hsv);
hex = $.convert.rgb2hex(rgb);
} else if (clr.r) {
rgb = clr;
hex = $.convert.rgb2hex(rgb);
hsv = $.convert.rgb2hsv(rgb);
} else {
color = clr;
return;
}
} else if (_.isString(clr)) {
hex = clr;
rgb = $.convert.hex2rgb(hex);
if (!rgb) {
return;
}
hsv = $.convert.rgb2hsv(rgb);
} else {
return;
}
bw = $.convert.hsv2bw(hsv);
color = {
hsv: hsv,
rgb: rgb,
hex: hex,
bw: bw
};
}
function setCircle() {
if (rect && color) {
// convert hsv to pc
var pc = spectrum.hsv2pc(color.hsv);
// convert pc to px
var px = {
x: rect.width * (pc.x / 100),
y: rect.height * (pc.y / 100)
};
$.circle.applyProperties({
center: px,
borderColor: color.bw
});
$.circle.show();
} else {
$.circle.hide();
}
}
function getDefaultUnit() {
if (!unit) {
var defaultUnit = Ti.App.Properties.getString('ti.ui.defaultunit');
var units = {
'dp': Ti.UI.UNIT_DIP,
'dip': Ti.UI.UNIT_DIP,
'in': Ti.UI.UNIT_IN,
'cm': Ti.UI.UNIT_CM,
'mm': Ti.UI.UNIT_MM,
'px': Ti.UI.UNIT_PX
};
if (!units[defaultUnit]) {
throw 'Unknown ti.ui.defaultunit: ' + defaultUnit;
}
unit = units[defaultUnit];
}
return unit;
}