aboutsummaryrefslogtreecommitdiff
path: root/js/controllers/color-controller.js
blob: c02561bf3b8689db7375008ec7f77bec77cb1700 (plain)
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
/* <copyright>
Copyright (c) 2012, Motorola Mobility LLC.
All Rights Reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

* Neither the name of Motorola Mobility LLC nor the names of its
  contributors may be used to endorse or promote products derived from this
  software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
</copyright> */

////////////////////////////////////////////////////////////////////////
//
var Montage =           	require("montage/core/core").Montage,
    Component =         	require("montage/ui/component").Component,
    ColorModel =        	require("js/models/color-model").ColorModel,
    ColorPanelBase =    	require("js/panels/Color/colorpanelbase.reel").ColorPanelBase,
    ElementsMediator =  	require("js/mediators/element-mediator").ElementMediator,
    ColorPopupManager =		require("js/panels/Color/colorpopup-manager").ColorPopupManager,
    ColorButtonManager =	require("js/panels/Color/colorbutton-manager").ColorButtonManager;
////////////////////////////////////////////////////////////////////////
//Exporting as ColorController
exports.ColorController = Montage.create(Component, {
    ////////////////////////////////////////////////////////////////////
    //
    hasTemplate: {
        value: false
    },
    ////////////////////////////////////////////////////////////////////
    //
    deserializedFromTemplate: {
    	value: function () {
    		//Setting up colorManager in other classes
    		this.colorPanelBase.colorManager = this.colorModel;
    		this.colorPopupManager.colorManager = this.colorModel;
    		this.colorButtonManager.colorManager = this.colorModel;
    		//Listening for color changes
    		this.colorModel.addEventListener('change', this, false);
    	}
    },
    ////////////////////////////////////////////////////////////////////
    //
    colorModel: {
        value: ColorModel
    },
    ////////////////////////////////////////////////////////////////////
    //
    colorPanelBase: {
        value: ColorPanelBase
    },
    ////////////////////////////////////////////////////////////////////
    //
    colorPopupManager: {
        value: ColorPopupManager
    },
    ////////////////////////////////////////////////////////////////////
    //
    colorButtonManager: {
        value: ColorPopupManager
    },
    ////////////////////////////////////////////////////////////////////
    //
    colorView: {
        value: null
    },
    ////////////////////////////////////////////////////////////////////
    //
    colorToolbar: {
        value: null
    },
    ////////////////////////////////////////////////////////////////////
    //
    _popupTab: {
    	value: 'wheel'
    },
    ////////////////////////////////////////////////////////////////////
    //
    popupTab: {
        get: function() {return this._popupTab;},
        set: function(value) {this._popupTab = value.toLowerCase();}
    },
    ////////////////////////////////////////////////////////////////////
    //
    addButton: {
        value: function (type, button) {
            if (this.colorView) {
                this.colorView.addButton(type, button);
                return true;
            } else if (this.ColorPanelBase) {
                this.ColorPanelBase.addButton(type, button);
                return true;
            } else {
                return false;
            }
        }
    },
    ////////////////////////////////////////////////////////////////////
    //
    removeButton: {
        value: function (type, button) {
            if (this.colorView) {
                this.colorView.removeButton(type, button);
                return true;
            } else if (this.ColorPanelBase) {
                this.ColorPanelBase.removeButton(type, button);
                return true;
            } else {
                return false;
            }
        }
    },
    ////////////////////////////////////////////////////////////////////
    //
    _fill: {
    	value: null
    },
    ////////////////////////////////////////////////////////////////////
    //
    fill: {
        get: function() {return this._fill;},
        set: function(value) {this._fill = value;}
    },
    ////////////////////////////////////////////////////////////////////
    //
    _stroke: {
    	value: null
    },
    ////////////////////////////////////////////////////////////////////
    //
    stroke: {
        get: function() {return this._stroke;},
        set: function(value) {this._stroke = value;}
    },
    ////////////////////////////////////////////////////////////////////
    //
    getBackground: {
        value: function (element) {
        	//TODO: Return object with all background properties
        	console.log(ElementsMediator.getProperty(element, 'background-color'));
        	console.log(ElementsMediator.getProperty(element, 'background-image'));
        }
    },
    ////////////////////////////////////////////////////////////////////
    //
    getBorder: {
        value: function (element) {

        }
    },
    ////////////////////////////////////////////////////////////////////
    //
    setColor: {
        value: function (mode, color, isFill, selection) {
            var elements;
            //The selection is optional, if none, it asks for the currently selected elements
            if (selection) {
                elements = selection;
            } else {
                elements = this.application.ninja.selectedElements;
            }
            if (elements && elements.length) {
                var colorInfo = { mode:mode,
                               color:color
                             };
                ElementsMediator.setColor(elements, colorInfo, isFill, "Change", "color-controller");
            }
        }
    },
    ////////////////////////////////////////////////////////////////////
    //
    handleChange: {
        value: function (e) {
        	//
            var color, input = e._event.input, panelMode, mode = e._event.mode;
            if (this.colorView) {
            	panelMode = this.colorView.panelMode;
            }
            //
            if (mode === 'nocolor') {
            	color = {value: null, css: 'none'};
            } else if (panelMode === 'rgb' && e._event.rgba && mode !== 'gradient') {
                color = e._event.rgba;
                color.webGlColor = e._event.webGlColor;
            } else if (panelMode === 'hsl' && e._event.hsla && mode !== 'gradient') {
                color = e._event.hsla;
                color.webGlColor = e._event.webGlColor;
            } else if (mode !== 'gradient'){
                color = {value: e._event.hex, css: '#'+e._event.hex};
            } else if (mode === 'gradient'){
            	color = e._event.value.value;
            }
            color.mode = panelMode;
            color.wasSetByCode = true;
            color.type = "change";
            ////////////////////////////////////////////////////////////
            ////////////////////////////////////////////////////////////
            //
            if (input === 'fill') {
            	//
            	this.fill = color;
                //
                if(e._event.wasSetByCode) return;
            	//
                this.setColor(mode, color, true);
            } else if (input === 'stroke') {
            	//
            	this.stroke = color;
                //
                if(e._event.wasSetByCode) return;

                this.setColor(mode, color, false);
            }
            ////////////////////////////////////////////////////////////
            ////////////////////////////////////////////////////////////
        }
    },
    ////////////////////////////////////////////////////////////////////
    //Converts CSS to a color object to be used by the color model
    getColorObjFromCss: {
        value: function (css) {
        	//
        	var color, arr, i, j, temp, c, gradient;
        	//console.log(css.indexOf('-webkit'), css);
        	if (css && css.indexOf('-webkit') >= 0) {
        		//
        		gradient = {mode: null, stops: null};
        		//Checking for gradient type
        		if (css.indexOf('-webkit-radial-gradient') >= 0) {
        			//Radial gradient
        			gradient.stops = [];
        			gradient.mode = 'radial';
        			gradient.gradientMode = 'radial';
        			gradient.css = css;
        			//
        			arr = css.split('%,');
        			//
        			for (j=1; arr[j]; j++) {
        				//TODO: Add HSL support
        				if (arr[j].indexOf('rgb') >= 0 && arr[j].indexOf('rgba') < 0) {
    		    			temp = arr[j].split('rgb');
    		    			temp = temp[1].replace(/\(/i, "");
    		    			temp = temp.split(')');
    		    			c = this.parseCssToColor('rgb('+temp[0]+')');
    		    			gradient.stops.push({css: c.css, value: c.value, mode: c.mode, position: parseInt(temp[1].replace(/\%/i, ""))});
	    	    		} else if (css.indexOf('rgba') >= 0) {

	    	    			temp = arr[j].split('rgba');
    		    			temp = temp[1].replace(/\(/i, "");
    		    			temp = temp.split(')');
    		    			c = this.parseCssToColor('rgba('+temp[0]+')');
    		    			gradient.stops.push({css: c.css, value: c.value, mode: c.mode, position: parseInt(temp[1].replace(/\%/i, ""))});
		        		}
        			}
        		} else if (css.indexOf('-webkit-gradient') >= 0) {
        			//Linear gradient
        			gradient.stops = [];
        			gradient.mode = 'linear';
        			gradient.gradientMode = 'linear';
        			gradient.css = css;
        			//
        			arr = css.split('from(');
        			//
        			if (arr.length > 1) {
        				arr = arr[1].split('),');
        			} else {
	        			arr = (css.split(css.split('color-stop(')[0])[1]).split('),');
        			}
        			//
        			for (i=0; arr[i]; i++) {
        				arr[i] = arr[i].replace(/ color-stop\(/i, "");
        				//
        				if (arr[i].indexOf('to(') >= 0) {
        					arr[i] = arr[i].replace(/ to\(/i, "");
        					arr[i] = arr[i].replace(/\)\)/i, "");
        				}
        				//
        				if (i === 0 && arr[i].indexOf('color-stop') === -1) {
        					arr[i] = {css: arr[i], percent: 0};
        				} else if (i === arr.length-1) {
        					temp = arr[i].split(', rgb');
        					if (temp.length > 1) {
	        					arr[i] = {css: 'rgb'+temp[1].replace(/\)\)/i, ""), percent: Math.round(parseFloat(temp[0])*100)};
        					} else {
        						arr[i] = {css: arr[i], percent: 100};
        					}
        				} else {
        					//
        					if (arr[i].indexOf('rgb') >= 0 && arr[i].indexOf('rgba') < 0) {
        						temp = arr[i].split(', rgb');
        						temp[0] = temp[0].replace(/color\-stop\(/gi, '');
	        					arr[i] = {css: 'rgb'+temp[1], percent: Math.round(parseFloat(temp[0])*100)};
        					} else if (arr[i].indexOf('rgba') >= 0) {
    	    					temp = arr[i].split(', rgba');
	        					arr[i] = {css: 'rgba'+temp[1], percent: Math.round(parseFloat(temp[0])*100)};
	        				}
        				}
        				//
        				c = this.parseCssToColor(arr[i].css);
        				gradient.stops.push({css: c.css, value: c.value, mode: c.mode, position: arr[i].percent});
        			}
        		}
        		//Creating gradient object
        		color = {mode: 'gradient', value: {stops: gradient.stops, mode: gradient.mode, gradientMode: gradient.gradientMode, css: css}};
        	} else if (css){
        		//Simple solid color
        		color = this.parseCssToColor(css);
        	}
            // TODO - Hack for inconsistent color object -- some workflows set color.color and some color.value
            if(color)
            {
                if(color.value && !color.color)
                {
                    color.color = color.value;
                }
                else if(color.color && !color.value)
                {
                    color.value = color.color;
                }
            }
            //Returning color object (or null if none)
        	return color;
        }
    },
    ////////////////////////////////////////////////////////////////////
    //Parses simple solid CSS string into color object
    parseCssToColor: {
        value: function (css) {
        	var color, r, p;
        	//Parsing string and converting into color object
        	if (css.indexOf('#') >= 0) {
        		color = {mode: 'hex', css: css, value: css.split('#')[1]};
        	} else if (css.indexOf('rgb') >= 0 && css.indexOf('rgba') < 0) {
        		r = /^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/;
               	p = css.match(r);
                color = {mode: 'rgb', css: css, value: {css: css, r: parseInt(p[1]), g: parseInt(p[2]), b: parseInt(p[3]), a: 1}};
        	} else if (css.indexOf('rgba') >= 0) {
        		r = /^rgba\((\d+),\s*(\d+),\s*(\d+),\s*(\d*\.?\d+)\)$/;
               	p = css.match(r);
               	color = {mode: 'rgb', css: css, value: {css: css, r: parseInt(p[1]), g: parseInt(p[2]), b: parseInt(p[3]), a: parseFloat(p[4])}};
       		} else if (css.indexOf('hsl') >= 0 && css.indexOf('hsla') < 0) {
       			r = /^hsl\((\d+),\s*(\d+),\s*(\d+)\)$/;
               	p = css.match(r);
               	color = {mode: 'hsl', css: css, value: {css: css, h: parseInt(p[1]), s: parseInt(p[2]), l: parseInt(p[3]), a: 1}};
        	} if (css.indexOf('hsla') >= 0) {
        		r = /^hsla\((\d+),\s*(\d+),\s*(\d+),\s*(\d*\.?\d+)\)$/;
               	p = css.match(r);
               	color = {mode: 'hsl', css: css, value: {css: css, h: parseInt(p[1]), s: parseInt(p[2]), l: parseInt(p[3]), a: parseFloat(p[4])}};
       		}
       		//Must be a valid CSS or null will be returned
       		return color;
        }
    }
    ////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////
});