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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
|
/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
</copyright> */
////////////////////////////////////////////////////////////////////////
//
var Montage = require("montage/core/core").Montage,
Component = require("montage/ui/component").Component;
////////////////////////////////////////////////////////////////////////
//
exports.ColorModel = Montage.create(Component, {
////////////////////////////////////////////////////////////////////
//
hasTemplate: {
enumerable: false,
value: false
},
////////////////////////////////////////////////////////////////////
//HSV Value of current color selected
_gradient: {
enumerable: false,
value: null
},
////////////////////////////////////////////////////////////////////
//HSV Value of current color selected
gradient: {
enumerable: true,
get: function() {
return this._gradient;
},
set: function(value) {
this._gradient = value;
//Updating color selected (converting to all modes)
this.updateColorSelected('gradient', value);
}
},
////////////////////////////////////////////////////////////////////
//HSV Value of current color selected
_hsv: {
enumerable: false,
value: null
},
////////////////////////////////////////////////////////////////////
//HSV Value of current color selected
hsv: {
enumerable: true,
get: function() {
return this._hsv;
},
set: function(value) {
this._hsv = value;
//Updating color selected (converting to all modes)
this.updateColorSelected('hsv', value);
}
},
////////////////////////////////////////////////////////////////////
//RGB Value of current color selected
_rgb: {
enumerable: false,
value: null
},
////////////////////////////////////////////////////////////////////
//RGB Value of current color selected
rgb: {
enumerable: true,
get: function() {
return this._rgb;
},
set: function(value) {
this._rgb = value;
//Updating color selected (converting to all modes)
this.updateColorSelected('rgb', value);
}
},
////////////////////////////////////////////////////////////////////
//HSL Value of current color selected
_hsl: {
enumerable: false,
value: null
},
////////////////////////////////////////////////////////////////////
//HSL Value of current color selected
hsl: {
enumerable: true,
get: function() {
return this._hsl;
},
set: function(value) {
this._hsl = value;
//Updating color selected (converting to all modes)
this.updateColorSelected('hsl', value);
}
},
////////////////////////////////////////////////////////////////////
//HEX Value of current color selected
_hex: {
numerable: false,
value: null
},
////////////////////////////////////////////////////////////////////
//HEX Value of current color selected
hex: {
enumerable: true,
get: function() {
return this._hex;
},
set: function(value) {
this._hex = value;
//Updating color selected (converting to all modes)
this.updateColorSelected('hex', value);
}
},
////////////////////////////////////////////////////////////////////
//ALPHA Value of current color selected
_alpha: {
enumerable: false,
value: {value: 1, type: 'change', wasSetByCode: true}
},
////////////////////////////////////////////////////////////////////
//ALPHA Value of current color selected
alpha: {
enumerable: true,
get: function() {
return this._alpha;
},
set: function(value) {
value.value = Math.ceil(value.value*100)/100;
this._alpha = value;
//
if (this.rgb || this.hsl) {
this._dispatchChangeEvent('alpha', value);
}
}
},
////////////////////////////////////////////////////////////////////
//Input (fill or stroke) Value of current color selected
_input: {
enumerable: false,
value: 'fill'
},
////////////////////////////////////////////////////////////////////
//Input Value of current color selected
input: {
enumerable: true,
get: function() {
return this._input;
},
set: function(value) {
this._input = value;
//Dispatching change event
this._dispatchChangeEvent('input', value);
}
},
////////////////////////////////////////////////////////////////////
//Color mode of current color selected
_mode: {
enumerable: false,
value: null
},
////////////////////////////////////////////////////////////////////
//Color mode of current color selected
mode: {
enumerable: true,
get: function() {
return this._mode;
},
set: function(value) {
this._mode = value;
//Dispatching change event
this._dispatchChangeEvent('mode', value);
}
},
////////////////////////////////////////////////////////////////////
//Stroke Color Value of current color selected
_stroke: {
enumerable: false,
value: null
},
////////////////////////////////////////////////////////////////////
//Stroke Color Value of current color selected
stroke: {
enumerable: true,
get: function() {
return this._stroke;
},
set: function(value) {
this._stroke = value;
}
},
////////////////////////////////////////////////////////////////////
//Fill Color Value of current color selected
_fill: {
enumerable: false,
value: null
},
////////////////////////////////////////////////////////////////////
//Fill Color Value of current color selected
fill: {
enumerable: true,
get: function() {
return this._fill;
},
set: function(value) {
this._fill = value;
}
},
////////////////////////////////////////////////////////////////////
//History Value array of current color selected
colorHistory: {
enumerable: false,
value: {stroke: [{m: 'rgb', c: {r: 0, g: 0, b: 0}, a: 1}, {m: 'rgb', c: {r: 0, g: 0, b: 0}, a: 1}], fill: [{m: 'rgb', c: {r: 0, g: 0, b: 0}, a: 1}, {m: 'rgb', c: {r: 0, g: 0, b: 0}, a: 1}]}
},
////////////////////////////////////////////////////////////////////
//History Value array of current color selected
_addColorHistory: {
enumerable: true,
value: function(input, mode, color, alpha) {
//TODO: Add limit
if (this.colorHistory[input.toLowerCase()].length > 1) {
if (this.colorHistory[input.toLowerCase()][this.colorHistory[input.toLowerCase()].length-1].c !== color || this.colorHistory[input.toLowerCase()][this.colorHistory[input.toLowerCase()].length-1].a !== alpha.value) {
this.colorHistory[input.toLowerCase()].push({m: mode, c: color, a: alpha.value});
}
} else {
this.colorHistory[input.toLowerCase()].push({m: mode, c: color, a: alpha.value});
}
}
},
////////////////////////////////////////////////////////////////////
//
applyNoColor: {
enumerable: true,
value: function (code) {
//
var nocolor = {};
nocolor.wasSetByCode = code;
nocolor.type = 'change';
this.updateColorSelected('nocolor', nocolor);
}
},
////////////////////////////////////////////////////////////////////
//Method to update color selected and convert to other modes
updateColorSelected: {
enumerable: true,
//value: function (input, mode, color, alpha) {
value: function (mode, color) {
////////////////////////////////////////////////////////////
//Checking for color mode to convert colors
switch (mode.toLocaleLowerCase()) {
////////////////////////////////////////////////////////
case 'gradient':
//Checking for match of previous gradient
if (color !== this.gradient) {
//Setting value and breaking out of function
this.gradient = color;
return;
}
//
this._hex = '------';
this._rgb = null;
this._hsv = null;
this._hsl = null;
break;
////////////////////////////////////////////////////////
case 'rgb':
//Checking for match of previous (RGB)
if (color.r !== this.rgb.r && color.g !== this.rgb.g && color.b !== this.rgb.b) {
//Setting value and breaking out of function
this.rgb = color;
return;
}
//Setting other color mode values
this._hsv = this.rgbToHsv(color.r, color.g, color.b);
this._hsl = this.rgbToHsl(color.r, color.g, color.b);
this._hex = this.rgbToHex(color.r, color.g, color.b);
this._gradient = null;
break;
////////////////////////////////////////////////////////
case 'hsv':
//Checking for match of previous (HSV)
if (color.h !== this.hsv.h && color.s !== this.hsv.s && color.v !== this.hsv.v) {
//Setting value and breaking out of function
this.hsv = color;
return;
}
//Setting other color mode values
this._rgb = this.hsvToRgb(color.h/(2*Math.PI), color.s, color.v);
this._hsl = this.rgbToHsl(this.rgb.r, this.rgb.g, this.rgb.b);
this._hex = this.rgbToHex(this.rgb.r, this.rgb.g, this.rgb.b);
this._gradient = null;
break;
////////////////////////////////////////////////////////
case 'hsl':
//Checking for match of previous (HSV)
if (color.h !== this.hsl.h && color.s !== this.hsl.s && color.l !== this.hsl.l) {
//Setting value and breaking out of function
this.hsl = color;
return;
}
//Setting other color mode values
this._rgb = this.hslToRgb(color.h/360, color.s/100, color.l/100);
//This is a hack to keep the values of color spectrum the same on limits (B/W)
var hsvTemp = this.rgbToHsv(this.rgb.r, this.rgb.g, this.rgb.b);
this._hsv = {h: (this._hsl.h/360)*(2*Math.PI), s: hsvTemp.s, v: hsvTemp.v};
this._hex = this.rgbToHex(this.rgb.r, this.rgb.g, this.rgb.b);
this._gradient = null;
break;
////////////////////////////////////////////////////////
case 'hex':
switch (color.length) {
case 1:
this.applyNoColor(false);
return;
break;
case 2:
this.applyNoColor(false);
return;
break;
case 3:
color = color[0]+color[0]+color[1]+color[1]+color[2]+color[2];
break;
case 4:
this.applyNoColor(false);
return;
break;
case 5:
this.applyNoColor(false);
return;
break;
case 6:
//Nothing
break;
default:
this.applyNoColor(false);
return;
break;
}
//Checking for match of previous (HEX)
if (color !== this.hex) {
//Setting value and breaking out of function
this.hex = color;
return;
}
//Setting other color mode values
this._rgb = this.hexToRgb(color);
this._hsv = this.rgbToHsv(this.rgb.r, this.rgb.g, this.rgb.b);
this._hsl = this.rgbToHsl(this.rgb.r, this.rgb.g, this.rgb.b);
this._gradient = null;
break;
////////////////////////////////////////////////////////
case 'nocolor':
//
this._hex = '------';
this._rgb = null;
this._hsv = null;
this._hsl = null;
this._gradient = null;
break;
////////////////////////////////////////////////////////
default:
console.log('ERROR: An error occurred in ColorManager. '+mode+' was not identified in updateColorSelected');
break;
////////////////////////////////////////////////////////
}
//Dispatching change event
this._dispatchChangeEvent(mode, color);
}
},
////////////////////////////////////////////////////////////////////
//Dispatching "Change" event
_dispatchChangeEvent: {
//Modes: rgb, hsl, hsv, hex, alpha, input, mode
value: function(mode, value) {
//Creating custom event to dispatch and include values
var colorEvent = document.createEvent("CustomEvent");
//
if (value && value.type) {
if (value.type === "change") {
//
colorEvent.initEvent("change", true, true);
//Storing the selected color in the history array
if ((mode === 'rgb' || mode === 'hsv' || mode === 'hsl' || mode === 'hex' || mode === 'nocolor' || mode === 'gradient') && this.input !== 'chip') {
this._addColorHistory(this.input, mode, value, this.alpha);
} else if (mode === 'alpha' && this.input !== 'chip') {
var c = this.colorHistory[this.input][this.colorHistory[this.input].length-1];
this._addColorHistory(this.input, c.m, c.c, this.alpha);
}
} else if (value.type === "changing") {
//
colorEvent.initEvent("changing", true, true);
} else {
//
colorEvent.initEvent("error", true, true);
}
} else {
//
colorEvent.initEvent("error", true, true);
}
//Checking for event to be color, if so, adding color data
if (mode === 'rgb' || mode === 'hsv' || mode === 'hsl' || mode === 'hex' || mode === 'alpha' || mode === 'nocolor' || mode === 'gradient') {
//Returning color history of input
if (this.input === 'stroke') {
colorEvent.history = this.colorHistory.stroke;
} else if (this.input === 'fill') {
colorEvent.history = this.colorHistory.fill;
} else {
colorEvent.history = null;
}
}
//Also returning other color mode values
if (this.input)
colorEvent.input = this.input;
if (this.alpha)
colorEvent.alpha = this.alpha.value;
if (this.hsl)
colorEvent.hsla = {h: this.hsl.h, s: this.hsl.s, l: this.hsl.l, a: this.alpha.value, css: 'hsla('+this.hsl.h+', '+this.hsl.s+'%, '+this.hsl.l+'%, '+this.alpha.value+')'};
if (this.rgb) {
colorEvent.rgba = {r: this.rgb.r, g: this.rgb.g, b: this.rgb.b, a: this.alpha.value, css: 'rgba('+ this.rgb.r +', '+this.rgb.g+', '+this.rgb.b+', '+this.alpha.value+')'};
colorEvent.webGlColor = [this.rgb.r/255, this.rgb.g/255, this.rgb.b/255, this.alpha.value];
}
if (this.hex)
colorEvent.hex = this.hex;
//Standard values that apply to any event
colorEvent.value = value;
colorEvent.mode = mode;
if (value && value.wasSetByCode) {
colorEvent.wasSetByCode = value.wasSetByCode;
}
this.dispatchEvent(colorEvent);
}
},
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// COLOR CONVERTION METHODS (public)
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
//Convert RGB to HEX (RGB = 0 to 255)
rgbToHex: {
enumerable: true,
value: function (r, g, b) {
//Coverting color channel to Hex
function getHex (c) {
var filter = "0123456789ABCDEF";
c = parseInt (c, 10);
if (isNaN(c)) return "00";
c = Math.max(0,Math.min(c,255));
return filter.charAt((c-c%16)/16) + filter.charAt(c%16);
}
//Returning HEX string
return (getHex(r)+getHex(g)+getHex(b));
}
},
////////////////////////////////////////////////////////////////////
//Convert HEX to RGB (no # symbol)
hexToRgb: {
enumerable: true,
value: function (value) {
//Spliting string converting to values
var r = parseInt(value.substring(0,2), 16), g = parseInt(value.substring(2,4), 16), b = parseInt(value.substring(4,6), 16);
//Checking for valid values
if (isNaN(r)) return null;
if (isNaN(g)) return null;
if (isNaN(b)) return null;
return {r: r, g: g, b: b};
}
},
////////////////////////////////////////////////////////////////////
//Convert HSV to RGB (HSV = 0 to 1)
hsvToRgb: {
enumerable: true,
value: function (h, s, v) {
var r, g, b, i = Math.floor(h * 6), f = h * 6 - i, p = v * (1 - s), q = v * (1 - f * s), t = v * (1 - (1 - f) * s);
//
switch (i % 6){
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
//Returning RGB object
return {r: Math.round(r * 255), g: Math.round(g * 255), b: Math.round(b * 255)};
}
},
////////////////////////////////////////////////////////////////////
//Convert RGB to HSV (RGB = 0 to 255)
rgbToHsv: {
enumerable: true,
value: function (r, g, b) {
//RGB covertion to percentage
r = r/255, g = g/255, b = b/255;
var max = Math.max(r, g, b), min = Math.min(r, g, b), h, s, v, d;
h = s = v = max;
d = max - min;
s = max == 0 ? 0 : d / max;
//TODO: Check for Hue not to change if S or V = 0
if (max == min) {
h = 0;
} else {
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
//Returing HSV object
return {h: h*Math.PI*2, s: s, v: v};
}
},
////////////////////////////////////////////////////////////////////
//Convert RGB TO HSL (RGB = 0 to 255)
rgbToHsl: {
enumerable: true,
value: function (r, g, b) {
//RGB covertion to percentage
r /= 255, g /= 255, b /= 255;
var max = Math.max(r, g, b), min = Math.min(r, g, b), h, s, l, d;
h = s = l = (max + min) / 2;
//TODO: Check for Hue not to change if S or L = 0
if (max == min) {
h = s = 0;
} else {
d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
//Returing HSL object
return {h: h*360, s: s*100, l: l*100};
}
},
////////////////////////////////////////////////////////////////////
//Convert HSL to RGB (HSL = 0 to 1)
hslToRgb: {
enumerable: true,
value: function (h, s, l) {
var r, g, b;
if (s == 0) {
r = g = b = l;
} else {
function getRgb (p, q, t) {
if(t < 0) t += 1;
if(t > 1) t -= 1;
if(t < 1/6) return p + (q - p) * 6 * t;
if(t < 1/2) return q;
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
}
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
r = getRgb(p, q, h + 1/3);
g = getRgb(p, q, h);
b = getRgb(p, q, h - 1/3);
}
//Returning RGB object
return {r: Math.round(r * 255), g: Math.round(g * 255), b: Math.round(b * 255)};
}
},
////////////////////////////////////////////////////////////////////
//Returns WebGL color array in [r, g, b, a] format where the values are [0,1] given a color object
colorToWebGl: {
enumerable: true,
value: function (color) {
var temp;
if (color) {
if(color.l !== undefined) {
temp = this.hslToRgb(color.h/360, color.s/100, color.l/100);
} else if (color.r !== undefined) {
temp = color;
} else if (color.gradientMode) {
// TODO - Need to handle gradients at some point
return null;
}
temp.a = color.a;
}
//WebGL uses array
if (temp) {
return [temp.r/255, temp.g/255, temp.b/255, temp.a];
} else {
return null;
}
}
},
////////////////////////////////////////////////////////////////////
//Returns a color object given a WebGL color array/object with gradient stops
webGlToColor: {
enumerable: true,
value: function (c) {
if(c) {
if(c.gradientMode) {
// Gradient
var i = 0,
len,
css,
stops = c.color,
gradient;
// Create the CSS string
if (c.gradientMode === 'radial') {
css = '-webkit-radial-gradient(center, ellipse cover';
} else {
css = '-webkit-gradient(linear, left top, right top';
}
//Sorting array (must be sorted for radial gradients, at least in Chrome
stops.sort(function(a,b){return a.position - b.position});
//Looping through stops in gradient to create CSS
len = stops.length;
for (i=0; i < len; i++) {
//Adding to CSS String
if (c.gradientMode === 'radial' && stops[i].value) {
css += ', '+stops[i].value.css+' '+stops[i].position+'% ';
} else if (stops[i].value){
css += ', color-stop('+stops[i].position+'%,'+stops[i].value.css+')';
}
}
//Closing the CSS strings
css += ')';
gradient = {stops: c.color, mode: c.gradientMode, gradientMode: c.gradientMode, css: css};
return {mode: 'gradient', value: gradient, color: gradient};
} else if(c.length === 4) {
// CSS
return this.application.ninja.colorController.getColorObjFromCss('rgba(' + c[0]*255 + ', '
+ c[1]*255 + ', '
+ c[2]*255 + ', '
+ c[3] +')');
}
}
return null;
}
}
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
});
|