]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | /**\r |
2 | * View Model that holds the "selectedColor" of the color picker container.\r | |
3 | */\r | |
4 | Ext.define('Ext.ux.colorpick.SelectorModel', {\r | |
5 | extend : 'Ext.app.ViewModel',\r | |
6 | alias : 'viewmodel.colorpick-selectormodel',\r | |
7 | \r | |
8 | requires: [\r | |
9 | 'Ext.ux.colorpick.ColorUtils'\r | |
10 | ],\r | |
11 | \r | |
12 | data: {\r | |
13 | selectedColor: {\r | |
14 | r : 255, // red\r | |
15 | g : 255, // green\r | |
16 | b : 255, // blue\r | |
17 | h : 0, // hue,\r | |
18 | s : 1, // saturation\r | |
19 | v : 1, // value\r | |
20 | a : 1 // alpha (opacity)\r | |
21 | },\r | |
22 | previousColor: {\r | |
23 | r : 0, // red\r | |
24 | g : 0, // green\r | |
25 | b : 0, // blue\r | |
26 | h : 0, // hue,\r | |
27 | s : 1, // saturation\r | |
28 | v : 1, // value\r | |
29 | a : 1 // alpha (opacity)\r | |
30 | }\r | |
31 | },\r | |
32 | \r | |
33 | formulas: {\r | |
34 | // Hexadecimal representation of the color\r | |
35 | hex: {\r | |
36 | get: function (get) {\r | |
37 | var r = get('selectedColor.r').toString(16),\r | |
38 | g = get('selectedColor.g').toString(16),\r | |
39 | b = get('selectedColor.b').toString(16),\r | |
40 | result;\r | |
41 | \r | |
42 | result = Ext.ux.colorpick.ColorUtils.rgb2hex(r, g, b);\r | |
43 | return '#' + result;\r | |
44 | },\r | |
45 | \r | |
46 | set: function (hex) {\r | |
47 | var rgb = Ext.ux.colorpick.ColorUtils.hex2rgb(hex);\r | |
48 | this.changeRGB(rgb);\r | |
49 | }\r | |
50 | },\r | |
51 | \r | |
52 | // "R" in "RGB"\r | |
53 | red: {\r | |
54 | get: function(get) {\r | |
55 | return get('selectedColor.r');\r | |
56 | },\r | |
57 | \r | |
58 | set: function(r) {\r | |
59 | this.changeRGB({ r: r });\r | |
60 | }\r | |
61 | },\r | |
62 | \r | |
63 | // "G" in "RGB"\r | |
64 | green: {\r | |
65 | get: function(get) {\r | |
66 | return get('selectedColor.g');\r | |
67 | },\r | |
68 | \r | |
69 | set: function(g) {\r | |
70 | this.changeRGB({ g: g });\r | |
71 | }\r | |
72 | },\r | |
73 | \r | |
74 | // "B" in "RGB"\r | |
75 | blue: {\r | |
76 | get: function(get) {\r | |
77 | return get('selectedColor.b');\r | |
78 | },\r | |
79 | \r | |
80 | set: function(b) {\r | |
81 | this.changeRGB({ b: b });\r | |
82 | }\r | |
83 | },\r | |
84 | \r | |
85 | // "H" in HSV\r | |
86 | hue: {\r | |
87 | get: function(get) {\r | |
88 | return get('selectedColor.h') * 360;\r | |
89 | },\r | |
90 | \r | |
91 | set: function(hue) {\r | |
92 | this.changeHSV({ h: hue / 360 });\r | |
93 | }\r | |
94 | },\r | |
95 | \r | |
96 | // "S" in HSV\r | |
97 | saturation: {\r | |
98 | get : function(get) {\r | |
99 | return get('selectedColor.s') * 100;\r | |
100 | },\r | |
101 | \r | |
102 | set: function(saturation) {\r | |
103 | this.changeHSV({ s: saturation / 100 });\r | |
104 | }\r | |
105 | },\r | |
106 | \r | |
107 | // "V" in HSV\r | |
108 | value: {\r | |
109 | get: function(get) {\r | |
110 | var v = get('selectedColor.v');\r | |
111 | return v * 100;\r | |
112 | },\r | |
113 | \r | |
114 | set: function(value) {\r | |
115 | this.changeHSV({ v: value / 100 });\r | |
116 | }\r | |
117 | },\r | |
118 | \r | |
119 | alpha: {\r | |
120 | get: function(data) {\r | |
121 | var a = data('selectedColor.a');\r | |
122 | return a * 100;\r | |
123 | },\r | |
124 | \r | |
125 | set: function(alpha) {\r | |
126 | this.set('selectedColor', Ext.applyIf({\r | |
127 | a: alpha / 100\r | |
128 | }, this.data.selectedColor));\r | |
129 | }\r | |
130 | }\r | |
131 | }, // formulas\r | |
132 | \r | |
133 | changeHSV: function (hsv) {\r | |
134 | Ext.applyIf(hsv, this.data.selectedColor);\r | |
135 | \r | |
136 | var rgb = Ext.ux.colorpick.ColorUtils.hsv2rgb(hsv.h, hsv.s, hsv.v);\r | |
137 | \r | |
138 | hsv.r = rgb.r;\r | |
139 | hsv.g = rgb.g;\r | |
140 | hsv.b = rgb.b;\r | |
141 | \r | |
142 | this.set('selectedColor', hsv);\r | |
143 | },\r | |
144 | \r | |
145 | changeRGB: function (rgb) {\r | |
146 | Ext.applyIf(rgb, this.data.selectedColor);\r | |
147 | \r | |
148 | var hsv = Ext.ux.colorpick.ColorUtils.rgb2hsv(rgb.r, rgb.g, rgb.b);\r | |
149 | \r | |
150 | rgb.h = hsv.h;\r | |
151 | rgb.s = hsv.s;\r | |
152 | rgb.v = hsv.v;\r | |
153 | \r | |
154 | this.set('selectedColor', rgb);\r | |
155 | }\r | |
156 | });\r |