Debian Codegraph @

closure-library/closure/goog/ui/hsvapalette.js

raw
1 // Copyright 2008 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview An HSVA (hue/saturation/value/alpha) color palette/picker
17 * implementation.
18 * Without the styles from the demo css file, only a hex color label and input
19 * field show up.
20 *
21 * @author [email protected] (Chris Nokleberg)
22 * @see ../demos/hsvapalette.html
23 */
24
25 goog.provide('goog.ui.HsvaPalette');
26
27 goog.require('goog.array');
28 goog.require('goog.color.alpha');
29 goog.require('goog.dom.TagName');
30 goog.require('goog.events');
31 goog.require('goog.events.EventType');
32 goog.require('goog.style');
33 goog.require('goog.ui.Component');
34 goog.require('goog.ui.HsvPalette');
35
36
37
38 /**
39 * Creates an HSVA palette. Allows a user to select the hue, saturation,
40 * value/brightness and alpha/opacity.
41 * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
42 * @param {string=} opt_color Optional initial color, without alpha (default is
43 * red).
44 * @param {number=} opt_alpha Optional initial alpha (default is 1).
45 * @param {string=} opt_class Optional base for creating classnames (default is
46 * 'goog-hsva-palette').
47 * @extends {goog.ui.HsvPalette}
48 * @constructor
49 * @final
50 */
51 goog.ui.HsvaPalette = function(opt_domHelper, opt_color, opt_alpha, opt_class) {
52 goog.ui.HsvaPalette.base(
53 this, 'constructor', opt_domHelper, opt_color, opt_class);
54
55 /**
56 * Alpha transparency of the currently selected color, in [0, 1]. When
57 * undefined, the palette will behave as a non-transparent HSV palette,
58 * assuming full opacity.
59 * @type {number}
60 * @private
61 */
62 this.alpha_ = goog.isDef(opt_alpha) ? opt_alpha : 1;
63
64 /**
65 * @override
66 */
67 this.className = opt_class || goog.getCssName('goog-hsva-palette');
68 };
69 goog.inherits(goog.ui.HsvaPalette, goog.ui.HsvPalette);
70
71
72 /**
73 * DOM element representing the alpha background image.
74 * @type {HTMLElement}
75 * @private
76 */
77 goog.ui.HsvaPalette.prototype.aImageEl_;
78
79
80 /**
81 * DOM element representing the alpha handle.
82 * @type {HTMLElement}
83 * @private
84 */
85 goog.ui.HsvaPalette.prototype.aHandleEl_;
86
87
88 /**
89 * DOM element representing the swatch backdrop image.
90 * @type {Element}
91 * @private
92 */
93 goog.ui.HsvaPalette.prototype.swatchBackdropEl_;
94
95
96 /** @override */
97 goog.ui.HsvaPalette.prototype.getAlpha = function() {
98 return this.alpha_;
99 };
100
101
102 /**
103 * Sets which color is selected and update the UI. The passed color should be
104 * in #rrggbb format. The alpha value will be set to 1.
105 * @param {number} alpha The selected alpha value, in [0, 1].
106 */
107 goog.ui.HsvaPalette.prototype.setAlpha = function(alpha) {
108 this.setColorAlphaHelper_(this.color, alpha);
109 };
110
111
112 /**
113 * Sets which color is selected and update the UI. The passed color should be
114 * in #rrggbb format. The alpha value will be set to 1.
115 * @param {string} color The selected color.
116 * @override
117 */
118 goog.ui.HsvaPalette.prototype.setColor = function(color) {
119 this.setColorAlphaHelper_(color, 1);
120 };
121
122
123 /**
124 * Gets the color that is currently selected in this color picker, in #rrggbbaa
125 * format.
126 * @return {string} The string of the selected color with alpha.
127 */
128 goog.ui.HsvaPalette.prototype.getColorRgbaHex = function() {
129 var alphaHex = Math.floor(this.alpha_ * 255).toString(16);
130 return this.color + (alphaHex.length == 1 ? '0' + alphaHex : alphaHex);
131 };
132
133
134 /**
135 * Sets which color is selected and update the UI. The passed color should be
136 * in #rrggbbaa format. The alpha value will be set to 1.
137 * @param {string} color The selected color with alpha.
138 */
139 goog.ui.HsvaPalette.prototype.setColorRgbaHex = function(color) {
140 var parsed = goog.ui.HsvaPalette.parseColorRgbaHex_(color);
141 this.setColorAlphaHelper_(parsed[0], parsed[1]);
142 };
143
144
145 /**
146 * Sets which color and alpha value are selected and update the UI. The passed
147 * color should be in #rrggbb format.
148 * @param {string} color The selected color in #rrggbb format.
149 * @param {number} alpha The selected alpha value, in [0, 1].
150 * @private
151 */
152 goog.ui.HsvaPalette.prototype.setColorAlphaHelper_ = function(color, alpha) {
153 var colorChange = this.color != color;
154 var alphaChange = this.alpha_ != alpha;
155 this.alpha_ = alpha;
156 this.color = color;
157 if (colorChange) {
158 // This is to prevent multiple event dispatches.
159 this.setColorInternal(color);
160 }
161 if (colorChange || alphaChange) {
162 this.updateUi();
163 this.dispatchEvent(goog.ui.Component.EventType.ACTION);
164 }
165 };
166
167
168 /** @override */
169 goog.ui.HsvaPalette.prototype.createDom = function() {
170 goog.ui.HsvaPalette.base(this, 'createDom');
171
172 var dom = this.getDomHelper();
173 this.aImageEl_ = /** @type {!HTMLElement} */ (
174 dom.createDom(
175 goog.dom.TagName.DIV, goog.getCssName(this.className, 'a-image')));
176 this.aHandleEl_ = /** @type {!HTMLElement} */ (
177 dom.createDom(
178 goog.dom.TagName.DIV, goog.getCssName(this.className, 'a-handle')));
179 this.swatchBackdropEl_ = dom.createDom(
180 goog.dom.TagName.DIV, goog.getCssName(this.className, 'swatch-backdrop'));
181 var element = this.getElement();
182 dom.appendChild(element, this.aImageEl_);
183 dom.appendChild(element, this.aHandleEl_);
184 dom.appendChild(element, this.swatchBackdropEl_);
185 };
186
187
188 /** @override */
189 goog.ui.HsvaPalette.prototype.disposeInternal = function() {
190 goog.ui.HsvaPalette.base(this, 'disposeInternal');
191
192 delete this.aImageEl_;
193 delete this.aHandleEl_;
194 delete this.swatchBackdropEl_;
195 };
196
197
198 /** @override */
199 goog.ui.HsvaPalette.prototype.updateUi = function() {
200 goog.ui.HsvaPalette.base(this, 'updateUi');
201 if (this.isInDocument()) {
202 var a = this.alpha_ * 255;
203 var top = this.aImageEl_.offsetTop -
204 Math.floor(this.aHandleEl_.offsetHeight / 2) +
205 this.aImageEl_.offsetHeight * ((255 - a) / 255);
206 this.aHandleEl_.style.top = top + 'px';
207 this.aImageEl_.style.backgroundColor = this.color;
208 goog.style.setOpacity(this.swatchElement, a / 255);
209 }
210 };
211
212
213 /** @override */
214 goog.ui.HsvaPalette.prototype.updateInput = function() {
215 if (!goog.array.equals(
216 [this.color, this.alpha_],
217 goog.ui.HsvaPalette.parseUserInput_(this.inputElement.value))) {
218 this.inputElement.value = this.getColorRgbaHex();
219 }
220 };
221
222
223 /** @override */
224 goog.ui.HsvaPalette.prototype.handleMouseDown = function(e) {
225 goog.ui.HsvaPalette.base(this, 'handleMouseDown', e);
226 if (e.target == this.aImageEl_ || e.target == this.aHandleEl_) {
227 // Setup value change listeners
228 var b = goog.style.getBounds(this.valueBackgroundImageElement);
229 this.handleMouseMoveA_(b, e);
230 this.mouseMoveListener = goog.events.listen(
231 this.getDomHelper().getDocument(), goog.events.EventType.MOUSEMOVE,
232 goog.bind(this.handleMouseMoveA_, this, b));
233 this.mouseUpListener = goog.events.listen(
234 this.getDomHelper().getDocument(), goog.events.EventType.MOUSEUP,
235 this.handleMouseUp, false, this);
236 }
237 };
238
239
240 /**
241 * Handles mousemove events on the document once a drag operation on the alpha
242 * slider has started.
243 * @param {goog.math.Rect} b Boundaries of the value slider object at the start
244 * of the drag operation.
245 * @param {goog.events.Event} e Event object.
246 * @private
247 */
248 goog.ui.HsvaPalette.prototype.handleMouseMoveA_ = function(b, e) {
249 e.preventDefault();
250 var vportPos = this.getDomHelper().getDocumentScroll();
251 var newA =
252 (b.top + b.height -
253 Math.min(Math.max(vportPos.y + e.clientY, b.top), b.top + b.height)) /
254 b.height;
255 this.setAlpha(newA);
256 };
257
258
259 /** @override */
260 goog.ui.HsvaPalette.prototype.handleInput = function(e) {
261 var parsed = goog.ui.HsvaPalette.parseUserInput_(this.inputElement.value);
262 if (parsed) {
263 this.setColorAlphaHelper_(parsed[0], parsed[1]);
264 }
265 };
266
267
268 /**
269 * Parses an #rrggbb or #rrggbbaa color string.
270 * @param {string} value User-entered color value.
271 * @return {Array<?>} A two element array [color, alpha], where color is
272 * #rrggbb and alpha is in [0, 1]. Null if the argument was invalid.
273 * @private
274 */
275 goog.ui.HsvaPalette.parseUserInput_ = function(value) {
276 if (/^#?[0-9a-f]{8}$/i.test(value)) {
277 return goog.ui.HsvaPalette.parseColorRgbaHex_(value);
278 } else if (/^#?[0-9a-f]{6}$/i.test(value)) {
279 return [value, 1];
280 }
281 return null;
282 };
283
284
285 /**
286 * Parses a #rrggbbaa color string.
287 * @param {string} color The color and alpha in #rrggbbaa format.
288 * @return {!Array<?>} A two element array [color, alpha], where color is
289 * #rrggbb and alpha is in [0, 1].
290 * @private
291 */
292 goog.ui.HsvaPalette.parseColorRgbaHex_ = function(color) {
293 var hex = goog.color.alpha.parse(color).hex;
294 return [
295 goog.color.alpha.extractHexColor(hex),
296 parseInt(goog.color.alpha.extractAlpha(hex), 16) / 255
297 ];
298 };