source: other-projects/nz-flag-design/trunk/main-form/bgrins-spectrum/index.html@ 29741

Last change on this file since 29741 was 29741, checked in by davidb, 9 years ago

Support JS library for colour picker

  • Property svn:executable set to *
File size: 45.3 KB
Line 
1<!doctype html>
2<html>
3<head>
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <title>Spectrum - The No Hassle jQuery Colorpicker</title>
6
7 <meta name="description" content="Spectrum is a JavaScript colorpicker plugin using the jQuery framework. It is highly customizable, but can also be used as a simple input type=color polyfill">
8 <meta name="author" content="Brian Grinstead and Spectrum contributors">
9
10 <link rel="stylesheet" type="text/css" href="spectrum.css">
11 <link rel="stylesheet" type="text/css" href="docs/bootstrap.css">
12 <link rel="stylesheet" type="text/css" href="docs/docs.css">
13 <script type="text/javascript" src="docs/jquery-1.9.1.js"></script>
14 <script type="text/javascript" src="spectrum.js"></script>
15 <script type='text/javascript' src='docs/toc.js'></script>
16 <script type='text/javascript' src='docs/docs.js'></script>
17</head>
18<body>
19<div id='header'>
20 <h1><a href='http://bgrins.github.com/spectrum'>Spectrum</a></h1> <h2><em>The No Hassle jQuery Colorpicker</em></h2>
21 <div id='links'>
22 <a href='http://github.com/bgrins/spectrum/zipball/1.6.0' class="btn btn-primary">Download Zip</a>
23 View the <a href='http://github.com/bgrins/spectrum'>Source code</a>.
24 Spectrum is a project by <a href='http://twitter.com/bgrins'>@bgrins</a>.
25 </div>
26 <br style='clear:both;' />
27</div>
28
29<div id='toc'></div>
30<div id='toc-slider'></div>
31
32<div id='docs'>
33 <div id='docs-content'>
34
35 <div id='switch-current'>
36 <input type='text' name='color1' id='pick1' value='#ddddff' />
37 <div id='switch-current-hsv' class='switch-current-output'></div>
38 <div id='switch-current-rgb' class='switch-current-output'></div>
39 <div id='switch-current-hex' class='switch-current-output'></div>
40 </div>
41
42 <div style='text-align:center;'>
43 <input id="full" />
44 </div>
45
46<pre class='prettyprint hide' id='code-heading'>
47&lt;input type='color' value='#f594d0' /&gt;
48<input type='color' class='basic' value='#f594d0' />
49</pre>
50
51 <h2 id="why">Why A Colorpicker?</h2>
52 <p><em>I wasn't satisfied with the solutions available for colorpicking</em>.
53 Many of them included a ton of images, were hard to skin or customize, or were very large plugins.
54 Here are the goals I had when making a new one:
55 </p>
56
57 <h3 id="why-footprint" class='point'>Small Footprint</h3>
58 <div class='note'>see a working <a href='http://jsfiddle.net/bgrins/ctkY3/'>jsFiddle example</a></div>
59 <p>Just include the needed CSS and JavaScript files, and you are ready to go! </p>
60<pre class='prettyprint' id='code-subheading'>
61&lt;script src='<a href='http://bgrins.github.com/spectrum/spectrum.js' target="_blank">spectrum.js</a>'&gt;&lt;/script&gt;
62&lt;link rel='stylesheet' href='<a href='http://bgrins.github.com/spectrum/spectrum.css' target="_blank">spectrum.css</a>' /&gt;
63</pre>
64 <p><strong>We don't need no stinkin' images!</strong></p>
65 <p>Nobody wants to add a bunch of code into their project. Spectrum is contained in two files, and both are careful not to mess with your existing code.</p>
66
67 <h3 id="why-polyfill" class='point'>Polyfill</h3>
68 <p>I wanted an option for the most basic use case, a polyfill for the <a href='http://dev.w3.org/html5/markup/input.color.html'>input[type=color]</a> HTML5 control.
69 This mode needs to work without JavaScript enabled - and fallback to an input[type=text] like other HTML5 inputs.
70 </p>
71 <p>If you don't want this behavior to happen, but still want to use spectrum elsewhere on the page, you can set <code>$.fn.spectrum.load = false;</code> right after loading the script file.</p>
72
73
74 <h3 id="why-customizable" class='point'>Customizable</h3>
75 <p>Just because you don't <em>have</em> to change anything to get it to work, doesn't mean you <em>can't</em>!
76 It is easy to skin and customize the plugin with CSS, and there are a wide range of modes and options to explore.
77 </p>
78
79 <h3 id="why-mobile" class='point'>Mobile Support</h3>
80 <p>Along with desktop browser support, I wanted a mobile colorpicker that was touch friendly, worked in iOS and Android, and used standards
81 that maximize future mobile support.
82 </p>
83
84 <h3 id="why-devtools" class='point'>Devtools</h3>
85
86 <p>
87 Believe it or not, <strong>this colorpicker lives inside of Chrome, Firefox, and Safari devtools</strong> to make picking colors easier for web developers and designers.
88 </p>
89
90 <p>
91 When I started the project, I wrote about <a href="http://www.briangrinstead.com/blog/chrome-developer-tools-colorpicker-concept">developer tools concept colorpicker implementation</a>. After that, I was <a href="http://groups.google.com/group/google-chrome-developer-tools/browse_thread/thread/4dd1e853b8051727/4549a6f0788885d4">contacted on the devtools mailing list</a> and got some initial feedback about the possibility of integrating it with devtools. Then I pulled the jQuery dependency out of a branch and I submitted a patch to the WebKit project.
92 </p>
93
94 <p>
95 From there, I opened a <a href="https://bugs.webkit.org/show_bug.cgi?id=71262">bug</a> to start working on it Web Inspector. 50+ comments and 10 patches later, the case <a href="http://www.webkit.org/blog/1804/last-week-in-webkit-calculated-css-values-and-the-translate-attribute/">landed in WebKit</a>. Here is the <a href='https://bugzilla.mozilla.org/show_bug.cgi?id=911702'>Firefox bug</a> where it was added.
96 </p>
97
98
99 <h2 id="modes">Modes</h2>
100
101 <h3 id="modes-custom" class='point'>Custom</h3>
102 <p>If you want to get more into the functionality, just create a normal input and initialize it as a normal jQuery plugin.
103 <strong>You can set a lot of options</strong> when initializing the colorpicker.
104 See the 'Options' section below.
105 </p>
106
107<pre class='prettyprint'>
108&lt;input type='text' id="custom" /&gt;
109</pre>
110
111<pre class='prettyprint'>
112&lt;script&gt;
113$("#custom").spectrum({
114 color: "#f00"
115});
116&lt;/script&gt;
117</pre>
118
119 <div class='example'>
120 <input type='text' id='custom' />
121 </div>
122
123 <h3 id="modes-flat" class='point'>Flat</h3>
124 <p><strong>Flat</strong>
125 This means that it will always show up at full size,
126 and be positioned as an inline-block element.
127 Look to the left for a full sized flat picker.
128 </p>
129
130<pre class='prettyprint'>
131&lt;input type='text' id="flat" /&gt;
132&lt;br/&gt;
133&lt;input type='text' id="flat" /&gt;
134</pre>
135<pre class='prettyprint'>
136$("#flat").spectrum({
137 flat: true,
138 showInput: true
139});
140$("#flatClearable").spectrum({
141 flat: true,
142 showInput: true,
143 allowEmpty:true
144});
145</pre>
146
147 <div class='example'>
148 <input type='text' id='flat' value="limegreen" />
149 <br/>
150 <input type='text' id='flatClearable' value="limegreen" />
151 </div>
152
153 <h3 id="modes-input" class='point'>input[type=color]</h3>
154
155 <p>
156 If you just want to provide a polyfill for the native color input,
157 the easiest way is to create an input with the type of color.
158 Once a user's browser supports a native color control, it will opt to use their native control instead.
159 </p>
160 <p><strong>Unlike the other modes, your value must be a 6 character hex value starting with a '#'.</strong> Why? Because the spec <a href='http://dev.w3.org/html5/markup/input.color.html#input.color.attrs.value'>says so</a>, that's why.
161 </p>
162
163<pre class='prettyprint' id='code-subheading'>
164&lt;input type='color' name='color' /&gt;
165&lt;input type='color' name='color2' value='#3355cc' /&gt;
166</pre>
167 <div class='example'>
168<form method="get">
169 <input type='color' name='color' />
170 <input type='color' name='color2' value='#3355cc' />
171 <input type='color' name='color3' value='#000000' />
172 <input type="submit" />
173</form>
174 </div>
175 <p><strong>That's it!</strong>
176 The field will degrade to a text input if the user does not have JavaScript enabled,
177 so that they will still be able to manually enter a color. You don't need to add a single line of code.
178 </p>
179
180<!--
181<div class="alert">
182 Heads up! Make sure you do not have a <code>maxlength</code> property set on your input. It has been known to break IE10.
183</div>
184-->
185
186 <h2 id="options">Options</h2>
187<pre class='prettyprint'>
188$("#picker").spectrum({
189 color: <strong>tinycolor</strong>,
190 flat: bool,
191 showInput: bool,
192 showInitial: bool,
193 allowEmpty: bool,
194 showAlpha: bool,
195 disabled: bool,
196 localStorageKey: string,
197 showPalette: bool,
198 showPaletteOnly: bool,
199 togglePaletteOnly: bool,
200 showSelectionPalette: bool,
201 clickoutFiresChange: bool,
202 cancelText: string,
203 chooseText: string,
204 togglePaletteMoreText: string,
205 togglePaletteLessText: string,
206 containerClassName: string,
207 replacerClassName: string,
208 preferredFormat: string,
209 maxSelectionSize: int,
210 palette: [[string]],
211 selectionPalette: [string]
212});
213</pre>
214 <div class="alert alert-info">
215 Tip: options can be specified in an options object in the <code>spectrum</code> initializer, like <code>$(element).spectrum({showAlpha: true })</code> or on the element's markup, like <code>&lt;input data-show-alpha="true" /&gt;</code>.
216 </div>
217
218 <h3 id="options-color">Color</h3>
219 <div class='option-content'>
220 <div class='description'>
221 <p>
222 The initial color will be set with the <code>color</code> option.
223 If you don't pass in a color, Spectrum will use the <code>value</code> attribute on the input.
224 </p>
225 <p>
226 The color parsing is based on the <a href='https://github.com/bgrins/TinyColor'>TinyColor</a> plugin.
227 This should parse any color string you throw at it.</p>
228 </p>
229 </div>
230
231 <pre class='prettyprint'>
232&lt;input type='text' class='basic' value='red' /&gt;
233&lt;input type='text' class='basic' value='#0f0' /&gt;
234&lt;input type='text' class='basic' value='blue' /&gt;
235&lt;br /&gt;
236&lt;input type='text' class='override' /&gt;
237&lt;br /&gt;
238&lt;input type='text' class='startEmpty' value='' /&gt;
239 </pre>
240
241 <pre class='prettyprint'>
242&lt;script&gt;
243$(".basic").spectrum();
244$(".override").spectrum({
245 color: "yellow"
246});
247(".startEmpty").spectrum({
248 allowEmpty: true
249});
250&lt;/script&gt;
251 </pre>
252 <div class='example'>
253 <input type='text' class='basic' value='red' />
254 <input type='text' class='basic' value='green' />
255 <input type='text' class='basic' value='blue' />
256 <br />
257 <input type='text' class='override' />
258 <br/>
259 <input type='text' class='startEmpty' value='' />
260 </div>
261 </div>
262
263 <h3 id="options-showInput">Show Input</h3>
264 <div class='option-content'>
265 <div class='description'>
266 <p>You can add an input to allow free form typing. The color parsing is very permissive in the allowed strings. See <a href='https://github.com/bgrins/TinyColor'>TinyColor</a> for more details.
267 </div>
268 <pre class='prettyprint'>
269$("#showInput").spectrum({
270 showInput: true
271});
272$("#showInputWithClear").spectrum({
273 showInput: true,
274 allowEmpty:true
275});
276 </pre>
277 <div class='example'>
278 <input type='text' name='showInput' id='showInput' />
279 <br/>
280 <input type='text' name='showInputWithClear' id='showInputWithClear' value='' />
281 </div>
282 </div>
283
284 <h3 id="options-showAlpha">Show Alpha</h3>
285 <div class='option-content'>
286 <div class='description'>
287 <p>You can allow alpha transparency selection. Check out these examples: </p>
288 </div>
289 <pre class='prettyprint'>
290$("#showAlpha").spectrum({
291 showAlpha: true
292});
293 </pre>
294 <div class='example'>
295 <input type='text' name='showAlpha' id='showAlpha' />
296 </div>
297 <div class='example'>
298 <input type='text' name='showAlphaWithInput' id='showAlphaWithInput' />
299 </div>
300 </div>
301
302 <h3 id="options-disabled">Disabled</h3>
303 <div class='option-content'>
304 <div class='description'>
305 <p>Spectrum can be automatically disabled if you pass in the <code>disabled</code> flag. Additionally, if the input that you initialize spectrum on is disabled, this will be the default value. Note: you <strong>cannot</strong> enable spectrum if the input is disabled (see below).</p>
306 </div>
307 <pre class='prettyprint'>
308$("#disabled").spectrum({
309 disabled: true
310});
311$("input:disabled").spectrum({
312
313});
314 </pre>
315 <div class='example'>
316 <input type='text' name='disabled' id='disabled' value='lightblue' />
317 <input type='text' disabled value='lightblue' />
318 <button id='toggle-disabled' class='btn'>Toggle Disabled</button>
319 </div>
320 </div>
321
322 <h3 id="options-showPalette">Show Palette</h3>
323 <div class='option-content'>
324 <div class='description'>
325 <p>Spectrum can show a palette below the colorpicker to make it convenient for users to choose from frequently or recently used colors. When the colorpicker is closed, the current color will be added to the palette if it isn't there already. Check it out here: </p>
326 </div>
327 <pre class='prettyprint'>
328$("#showPalette").spectrum({
329 showPalette: true,
330 palette: [
331 ['black', 'white', 'blanchedalmond'],
332 ['rgb(255, 128, 0);', 'hsv 100 70 50', 'lightyellow']
333 ]
334});
335 </pre>
336 <div class='example'>
337 <input type='text' name='showPalette' id='showPalette' value='lightblue' />
338 </div>
339 </div>
340
341 <h3 id="options-showPaletteOnly">Show Palette Only</h3>
342 <div class='option-content'>
343 <div class='note'>see a working <a href='http://jsfiddle.net/bgrins/S45tW/'>jsFiddle example</a></div>
344 <div class='description'>
345 <p>If you'd like, spectrum can show the palettes you specify, and nothing else.</p>
346 </div>
347
348 <pre class='prettyprint'>
349$("#showPaletteOnly").spectrum({
350 showPaletteOnly: true,
351 showPalette:true,
352 color: 'blanchedalmond',
353 palette: [
354 ['black', 'white', 'blanchedalmond',
355 'rgb(255, 128, 0);', 'hsv 100 70 50'],
356 ['red', 'yellow', 'green', 'blue', 'violet']
357 ]
358});
359 </pre>
360 <div class='example'>
361 <span class='label label-result'>
362 Result
363 </span>
364 <input type='text' name='showPaletteOnly' id='showPaletteOnly' />
365 </div>
366 </div>
367
368 <h3 id="options-togglePaletteOnly">Toggle Palette Only</h3>
369 <div class='option-content'>
370 <div class='description'>
371 <p>Spectrum can show a button to toggle the colorpicker next to the palette. This way, the user can choose from a limited number of colors in the palette, but still be able to pick a color that's not in the palette.<br />
372 The default value for <code>togglePaletteOnly</code> is FALSE. Set it to TRUE to enable the Toggle button.<br /><br />
373 You can also change the text on the Toggle Button with the options <code>togglePaletteMoreText</code> (default is "more") and <code>togglePaletteLessText</code> (default is "less").</p>
374 </div>
375
376 <pre class='prettyprint'>
377$("#togglePaletteOnly").spectrum({
378 showPaletteOnly: true,
379 togglePaletteOnly: true,
380 togglePaletteMoreText: 'more',
381 togglePaletteLessText: 'less',
382 color: 'blanchedalmond',
383 palette: [
384 ["#000","#444","#666","#999","#ccc","#eee","#f3f3f3","#fff"],
385 ["#f00","#f90","#ff0","#0f0","#0ff","#00f","#90f","#f0f"],
386 ["#f4cccc","#fce5cd","#fff2cc","#d9ead3","#d0e0e3","#cfe2f3","#d9d2e9","#ead1dc"],
387 ["#ea9999","#f9cb9c","#ffe599","#b6d7a8","#a2c4c9","#9fc5e8","#b4a7d6","#d5a6bd"],
388 ["#e06666","#f6b26b","#ffd966","#93c47d","#76a5af","#6fa8dc","#8e7cc3","#c27ba0"],
389 ["#c00","#e69138","#f1c232","#6aa84f","#45818e","#3d85c6","#674ea7","#a64d79"],
390 ["#900","#b45f06","#bf9000","#38761d","#134f5c","#0b5394","#351c75","#741b47"],
391 ["#600","#783f04","#7f6000","#274e13","#0c343d","#073763","#20124d","#4c1130"]
392 ]
393});
394 </pre>
395 <div class='example'>
396 <span class='label label-result'>
397 Result
398 </span>
399 <input type='text' name='togglePaletteOnly' id='togglePaletteOnly' />
400 </div>
401 </div>
402
403 <h3 id="options-showSelectionPalette">Show Selection Palette</h3>
404 <div class='option-content'>
405 <div class='description'>
406 <p>Spectrum can keep track of what has been selected by the user with the <code>showSelectionPalette</code> option.</p>
407 <p>If the <code>localStorageKey</code> option is defined, the selection will be saved in the browser's localStorage object</p>
408 </div>
409
410 <pre class='prettyprint'>
411$("#showSelectionPalette").spectrum({
412 showPalette: true,
413 showSelectionPalette: true, // true by default
414 palette: [ ]
415});
416$("#showSelectionPaletteStorage").spectrum({
417 showPalette: true,
418 showSelectionPalette: true,
419 palette: [ ],
420 localStorageKey: "spectrum.homepage", // Any Spectrum with the same string will share selection
421});
422 </pre>
423
424 <div class='example'>
425 <span class='label label-info'>This colorpicker will store what you pick:</span><br /><br />
426 <input type='text' name='showSelectionPalette' id='showSelectionPalette' value='lightblue' /><br /><br />
427 <span class='label label-info'>Try switching between the two colorpickers or reloading your page, the chosen colors are always available:</span><br /><br />
428 <input type='text' name='showSelectionPaletteStorage' id='showSelectionPaletteStorage' value='lightblue' />
429 <input type='text' name='showSelectionPaletteStorage2' id='showSelectionPaletteStorage2' value='pink' />
430 </div>
431 </div>
432
433 <h3 id="options-selectionPalette">Selection Palette</h3>
434 <div class='option-content'>
435 <div class='description'>
436 <p>The default values inside of the selection palette. Make sure that <a href="#options-showSelectionPalette">showSelectionPalette</a> and <a href="#options-showPalette">showPalette</a> are both enabled.</p>
437 <p>If a <code>localStorageKey</code> is defined, then this value will be overwritten by it.</p>
438 </div>
439
440 <pre class='prettyprint'>
441$("#selectionPalette").spectrum({
442 showPalette: true,
443 palette: [ ],
444 showSelectionPalette: true, // true by default
445 selectionPalette: ["red", "green", "blue"]
446});
447 </pre>
448
449 <div class='example'>
450 <span class='label label-info'>This colorpicker has default values in the selection palette:</span><br /><br />
451 <input type='text' name='selectionPalette' id='selectionPalette' value='orange' />
452 </div>
453 </div>
454
455 <h3 id="options-maxSelectionSize">Max Selection Size</h3>
456 <div class='option-content'>
457 <div class='description'>
458 <p>This is how many elements are allowed in the <a href="#options-selectionPalette">selectionPallete</a> at once.</p>
459 <p>Elements will be removed from the palette in first in - first out order if this limit is reached.</p>
460 </div>
461
462 <pre class='prettyprint'>
463$("#maxSelectionSize").spectrum({
464 showPalette: true,
465 palette: [ ],
466 showSelectionPalette: true, // true by default
467 selectionPalette: ["red", "green", "blue"],
468 maxSelectionSize: 2
469});
470 </pre>
471
472 <div class='example'>
473 <span class='label label-info'>This colorpicker starts removing selection palette colors older than 2:</span><br /><br />
474 <input type='text' name='maxSelectionSize' id='maxSelectionSize' value='red' />
475 </div>
476 </div>
477
478 <h3 id="options-hideAfterPaletteSelect">Hide After Palette Select</h3>
479 <div class='option-content'>
480 <div class='description'>
481 <p>You can have the colorpicker automatically hide after a palette color is selected.</p>
482 </div>
483
484 <pre class='prettyprint'>
485$("#hideAfterPaletteSelect").spectrum({
486 showPaletteOnly: true,
487 showPalette:true,
488 hideAfterPaletteSelect:true,
489 color: 'blanchedalmond',
490 palette: [
491 ['black', 'white', 'blanchedalmond',
492 'rgb(255, 128, 0);', 'hsv 100 70 50'],
493 ['red', 'yellow', 'green', 'blue', 'violet']
494 ]
495});
496 </pre>
497 <div class='example'>
498 <span class='label label-result'>
499 Result
500 </span>
501 <input type='text' name='hideAfterPaletteSelect' id='hideAfterPaletteSelect' />
502 </div>
503 </div>
504
505 <h3 id="options-clickoutFiresChange">Clickout Fires Change</h3>
506 <div class='option-content'>
507 <div class='description'>
508 <p>
509 When clicking outside of the colorpicker, you can force it to fire a <code>change</code> event rather than having it revert the change.
510 </p>
511 </div>
512
513 <pre class='prettyprint'>
514$("#clickoutFiresChange").spectrum({
515 clickoutFiresChange: true
516});
517$("#clickoutDoesntChange").spectrum({
518
519});
520 </pre>
521
522 <div class='example'>
523 <input type='text' name='clickoutFiresChange' id='clickoutFiresChange' value='goldenrod' />
524 <input type='text' name='clickoutDoesntFireChange' id='clickoutDoesntFireChange' value='goldenrod' />
525 </div>
526 </div>
527
528 <h3 id="options-showInitial">Show Initial</h3>
529 <div class='option-content'>
530 <div class='description'>
531 <p>
532 Spectrum can show the color that was initially set when opening.
533 This provides an easy way to click back to what was set when opened.
534 </p>
535 </div>
536
537 <pre class='prettyprint'>
538$("#showInitial").spectrum({
539 showInitial: true
540});
541 </pre>
542
543 <div class='example'>
544 <input type='text' name='showInitial' id='showInitial' value='goldenrod' />
545 </div>
546 </div>
547
548 <h3 id="options-showInputAndInitial">Show Input and Initial</h3>
549 <div class='option-content'>
550 <div class='description'>
551 <p>If you specify both the <code>showInput</code> and <code>showInitial</code> options, the CSS keeps things in order by wrapping the buttons to the bottom row, and shrinking the input. <em>Note: this is all customizable via CSS.</em></p>
552 </div>
553 <pre class='prettyprint'>
554$("#showInputAndInitial").spectrum({
555 showInitial: true,
556 showInput: true
557});
558 </pre>
559
560 <div class='example'>
561 <input type='text' name='showInputAndInitial' id='showInputAndInitial' value='goldenrod' />
562 </div>
563 </div>
564 <h3>Show Input, Initial, and Clear</h3>
565 <div class='option-content'>
566 <div class='description'>
567 <p>If you specify both the <code>showInput</code>, <code>showInitial</code>, and <code>allowEmpty</code> options, the CSS keeps things in order by wrapping the buttons to the bottom row, and shrinking the input. <em>Note: this is all customizable via CSS.</em></p>
568 </div>
569 <pre class='prettyprint'>
570$("#showInputInitialClear").spectrum({
571 allowEmpty:true,
572 showInitial: true,
573 showInput: true
574});
575 </pre>
576
577 <div class='example'>
578 <input type='text' name='showInputInitialClear' id='showInputInitialClear' value='' />
579 </div>
580 </div>
581 <h3 id="options-buttonText">Button Text</h3>
582 <div class='option-content'>
583 <div class='description'>
584 <p>You can set the button's text using <code>cancelText</code> and <code>chooseText</code> properties.</p>
585 </div>
586
587 <pre class='prettyprint'>
588$("#buttonText").spectrum({
589 allowEmpty:true,
590 chooseText: "Alright",
591 cancelText: "No way"
592});
593 </pre>
594
595 <div class='example'>
596 <input type='text' name='buttonText' id='buttonText' value='orangered' />
597 </div>
598 </div>
599
600 <h3 id="options-showButtons">Show Buttons</h3>
601 <div class='option-content'>
602 <div class='description'>
603 <p>
604 You can show or hide the buttons using the <code>showButtons</code> property.
605 If there are no buttons, the behavior will be to fire the `change` event (and update the original input) when the picker is closed.
606 </p>
607 </div>
608
609 <pre class='prettyprint'>
610$("#hideButtons").spectrum({
611 showButtons: false
612});
613 </pre>
614
615 <div class='example'>
616 <input type='text' name='hideButtons' id='hideButtons' value='orangered' />
617 </div>
618 </div>
619
620 <h3 id="options-containerClassName">Container Class Name</h3>
621 <div class='option-content'>
622 <div class='description'>
623 <p>
624 You can add an additional class name to the just the container element using the <code>containerClassName</code> property.
625 </p>
626 </div>
627
628 <pre class='prettyprint'>
629$("#containerClassName").spectrum({
630 containerClassName: 'awesome'
631});
632 </pre>
633 <pre class='prettyprint'>
634.awesome {
635 background: purple;
636}
637 </pre>
638 <style type='text/css'>
639.awesome {
640 background: purple;
641}
642 </style>
643
644 <div class='example'>
645 <input type='text' name='containerClassName' id='containerClassName' value='orangered' />
646 </div>
647 </div>
648
649 <h3 id="options-replacerClassName">Replacer Class Name</h3>
650 <div class='option-content'>
651 <div class='description'>
652 <p>
653 You can add an additional class name to just the replacer element using the <code>replacerClassName</code> property.
654 </p>
655 </div>
656
657 <pre class='prettyprint'>
658$("#replacerClassName").spectrum({
659 replacerClassName: 'awesome'
660});
661 </pre>
662 <pre class='prettyprint'>
663.awesome {
664 background: purple;
665}
666 </pre>
667 <style type='text/css'>
668.awesome {
669 background: purple;
670}
671 </style>
672
673 <div class='example'>
674 <input type='text' name='replacerClassName' id='replacerClassName' value='orangered' />
675 </div>
676 </div>
677
678 <h3 id="options-preferredFormat">Preferred Format</h3>
679 <div class='option-content'>
680 <div class='description'>
681 <p>You can set the format that is displayed in the text box.</p>
682 <p>This will also change the format that is displayed in the titles from the palette swatches.</p>
683 </div>
684
685 <pre class='prettyprint'>
686$("#preferredHex").spectrum({
687 preferredFormat: "hex",
688 showInput: true,
689 showPalette: true,
690 palette: [["red", "rgba(0, 255, 0, .5)", "rgb(0, 0, 255)"]]
691});
692$("#preferredHex3").spectrum({
693 preferredFormat: "hex3",
694 showInput: true,
695 showPalette: true,
696 palette: [["red", "rgba(0, 255, 0, .5)", "rgb(0, 0, 255)"]]
697});
698$("#preferredHsl").spectrum({
699 preferredFormat: "hsl",
700 showInput: true,
701 showPalette: true,
702 palette: [["red", "rgba(0, 255, 0, .5)", "rgb(0, 0, 255)"]]
703});
704$("#preferredRgb").spectrum({
705 preferredFormat: "rgb",
706 showInput: true,
707 showPalette: true,
708 palette: [["red", "rgba(0, 255, 0, .5)", "rgb(0, 0, 255)"]]
709});
710$("#preferredName").spectrum({
711 preferredFormat: "name",
712 showInput: true,
713 showPalette: true,
714 palette: [["red", "rgba(0, 255, 0, .5)", "rgb(0, 0, 255)"]]
715});
716$("#preferredNone").spectrum({
717 showInput: true,
718 showPalette: true,
719 palette: [["red", "rgba(0, 255, 0, .5)", "rgb(0, 0, 255)"]]
720});
721 </pre>
722 <div class='example'>
723 <div class='alert alert-info'>Hex</div>
724 <input type='text' name='preferredHex' id='preferredHex' value='orangered' />
725 <div class='alert alert-info'>Hex (3 Characters If Possible)</div>
726 <input type='text' name='preferredHex3' id='preferredHex3' value='orangered' />
727 <div class='alert alert-info'>Hsl</div>
728 <input type='text' name='preferredHsl' id='preferredHsl' value='orangered' />
729 <div class='alert alert-info'>Rgb</div>
730 <input type='text' name='preferredRgb' id='preferredRgb' value='orangered' />
731 <div class='alert alert-info'>Name (Falls back to hex)</div>
732 <input type='text' name='preferredName' id='preferredName' value='orangered' />
733 <div class='alert alert-info'>None (Depends on input - try changing formats with the text box)</div>
734 <input type='text' name='preferredNone' id='preferredNone' value='orangered' />
735 </div>
736 </div>
737
738 <h3 id="options-appendTo">appendTo</h3>
739 <div class='option-content'>
740 <div class='description'>
741 <p>You can choose which element the colorpicker container is appended to (default is <code>"body"</code>). This can be any valid object taken into the jQuery <a href="https://api.jquery.com/appendTo/">appendTo</a> function.</p>
742 <p>Changing this can help resolve issues with opening the colorpicker in a modal dialog or fixed position container, for instance.</p>
743 </div>
744
745</div>
746 <h2 id="events">Events</h2>
747
748<pre class='prettyprint'>
749$("#picker").spectrum({
750 move: function(tinycolor) { },
751 show: function(tinycolor) { },
752 hide: function(tinycolor) { },
753 beforeShow: function(tinycolor) { },
754});
755</pre>
756
757 <h3 id="events-change">change</h3>
758 <div class='option-content'>
759 <div class='description'>
760 <p>Called as the original input changes. Only happens when the input is closed or the 'Choose' button is clicked.</p>
761 </div>
762
763 <pre class='prettyprint'>
764change: function(color) {
765 color.toHexString(); // #ff0000
766}
767 </pre>
768
769 <div class='example'>
770 <input type='text' value='blanchedalmond' name='changeOnMoveNo' id='changeOnMoveNo' />
771 <em id='changeOnMoveNoLabel' class='em-label'></em>
772 </div>
773 </div>
774
775 <h3 id="events-move">move</h3>
776
777 <div class='option-content'>
778 <div class='description'>
779 <p>Called as the user moves around within the colorpicker</p>
780 </div>
781
782 <pre class='prettyprint'>
783move: function(color) {
784 color.toHexString(); // #ff0000
785}
786 </pre>
787
788 <div class='example'>
789 <input type='text' value='blanchedalmond' name='changeOnMove' id='changeOnMove' />
790 <em id='changeOnMoveLabel' class='em-label'></em>
791 </div>
792 </div>
793
794 <h3 id="events-hide">hide</h3>
795
796 <div class='option-content'>
797 <div class='description'>
798
799 <p>
800 Called after the colorpicker is hidden.
801 This happens when clicking outside of the picker while it is open.
802 Note, when any colorpicker on the page is shown it will hide any that are already open.
803 This event is ignored on a flat colorpicker.
804 </p>
805 </div>
806
807 <pre class='prettyprint'>
808hide: function(color) {
809 color.toHexString(); // #ff0000
810}
811 </pre>
812
813 <div class='example'>
814 <input type='text' value='blanchedalmond' name='eventhide' id='eventhide' />
815 <em id='eventhideLabel' class='em-label'></em>
816 </div>
817 </div>
818
819 <h3 id="events-show">show</h3>
820
821 <div class='option-content'>
822 <div class='description'>
823 <p>
824 Called after the colorpicker is opened.
825 This is ignored on a flat colorpicker.
826 Note, when any colorpicker on the page is shown it will hide any that are already open.
827 </p>
828 </div>
829
830 <pre class='prettyprint'>
831show: function(color) {
832 color.toHexString(); // #ff0000
833}
834 </pre>
835
836 <div class='example'>
837 <input type='text' value='blanchedalmond' name='eventshow' id='eventshow' />
838 <em id='eventshowLabel' class='em-label'></em>
839 </div>
840 </div>
841
842 <h3 id="events-beforeShow">beforeShow</h3>
843
844 <div class='option-content'>
845 <div class='description'>
846 <p>
847 You can prevent the colorpicker from showing up if you return false in the beforeShow event.
848 This event is ignored on a flat colorpicker.
849 </p>
850 </div>
851
852 <pre class='prettyprint'>
853beforeShow: function(color) {
854 return false; // Will never show up
855}
856 </pre>
857
858 <div class='example'>
859 <input type='text' value='blanchedalmond' name='beforeShow' id='beforeShow' />
860 </div>
861 </div>
862
863 <h3 id="events-dragstart">dragstart</h3>
864
865 <div class='option-content'>
866 <div class='description'>
867 <p>
868 Called at the beginning of a drag event on either
869 hue slider, alpha slider, or main color picker areas
870 </p>
871 </div>
872
873 <pre class='prettyprint'>
874$(element).on("dragstart.spectrum"): function(e, color) {
875 color.toHexString(); // #ff0000
876}
877 </pre>
878
879 <div class='example'>
880 <input type='text' value='blanchedalmond' name='eventdragstart' id='eventdragstart' />
881 <em id='eventdragstartLabel' class='em-label'></em>
882 </div>
883 </div>
884
885 <h3 id="events-dragstop">dragstop</h3>
886
887 <div class='option-content'>
888 <div class='description'>
889 <p>
890 Called at the end of a drag event on either
891 hue slider, alpha slider, or main color picker areas
892 </p>
893 </div>
894
895 <pre class='prettyprint'>
896$(element).on("dragstop.spectrum"): function(e, color) {
897 color.toHexString(); // #ff0000
898}
899 </pre>
900
901 <div class='example'>
902 <input type='text' value='blanchedalmond' name='eventdragstop' id='eventdragstop' />
903 <em id='eventdragstopLabel' class='em-label'></em>
904 </div>
905 </div>
906
907
908 <h2 id="methods">Methods</h2>
909<pre class='prettyprint'>
910$("#picker").spectrum("show");
911$("#picker").spectrum("hide");
912$("#picker").spectrum("toggle");
913$("#picker").spectrum("get");
914$("#picker").spectrum("set", colorString);
915$("#picker").spectrum("container");
916$("#picker").spectrum("reflow");
917$("#picker").spectrum("destroy");
918$("#picker").spectrum("enable");
919$("#picker").spectrum("disable");
920$("#picker").spectrum("option", optionName);
921$("#picker").spectrum("option", optionName, newOptionValue);
922</pre>
923
924 <h3 id="methods-show">show</h3>
925
926 <div class='option-content'>
927 <div class='description'>
928 <p>
929 Shows the colorpicker.
930 </p>
931 </div>
932 </div>
933
934 <h3 id="methods-hide">hide</h3>
935
936 <div class='option-content'>
937 <div class='description'>
938 <p>
939 Hides the colorpicker.
940 </p>
941 </div>
942 </div>
943
944 <h3 id="methods-toggle">toggle</h3>
945
946 <div class='option-content'>
947 <div class='description'>
948 <p>
949 Toggles the colorpicker.
950 </p>
951 <p class="warning">
952 <b>Warning:</b> If you are calling toggle from a click handler, make sure you <code>return false</code> to prevent the colorpicker from immediately hiding after it is toggled.
953 </p>
954 </div>
955
956 <pre class='prettyprint'>
957$("#btn-toggle").click(function() {
958 $("#toggle").spectrum("toggle");
959 return false;
960});
961 </pre>
962
963 <div class='example'>
964 <input type='text' value='blanchedalmond' name='toggle' id='toggle' />
965 <button id='btn-toggle'>Toggle</button>
966 </div>
967 </div>
968
969 <h3 id="methods-get">get</h3>
970
971 <div class='option-content'>
972 <div class='description'>
973 <p>
974 Gets the current value from the colorpicker.
975 </p>
976 </div>
977 </div>
978
979 <h3 id="methods-set">set</h3>
980
981 <div class='option-content'>
982 <div class='description'>
983 <p>
984 Setting the colorpicker programmatically will update the original input.
985 </p>
986 <p>
987 Note: this will <strong>not</strong> fire the <code>change</code> event,
988 to prevent infinite loops from calling <code>set</code> from within <code>change</code>.
989 </p>
990 </div>
991
992 <pre class='prettyprint'>
993&lt;input type='text' value='blanchedalmond' name='triggerSet' id='triggerSet' /&gt;
994&lt;input type='text' placeholder='Enter A Color' id='enterAColor' /&gt;
995&lt;button id='btnEnterAColor'&gt;Trigger Set&lt;/button&gt;
996
997&lt;script&gt;
998$("#triggerSet").spectrum();
999
1000// Show the original input to demonstrate the value changing when calling `set`
1001$("#triggerSet").show();
1002
1003$("#btnEnterAColor").click(function() {
1004 $("#triggerSet").spectrum("set", $("#enterAColor").val());
1005});
1006&lt;/script&gt;
1007 </pre>
1008
1009 <div class='example'>
1010 <input type='text' value='blanchedalmond' name='triggerSet' id='triggerSet' /><br /><br />
1011 <input type='text' placeholder='Enter A Color' id='enterAColor' /><button id='btnEnterAColor'>Trigger Set</button>
1012 </div>
1013 </div>
1014
1015 <h3 id="methods-container">container</h3>
1016
1017 <div class='option-content'>
1018 <div class='description'>
1019 <p>
1020 Retrieves the container element of the colorpicker, in case you want to manaully position it or do other things.
1021 </p>
1022 </div>
1023 </div>
1024
1025 <h3 id="methods-reflow">reflow</h3>
1026
1027 <div class='option-content'>
1028 <div class='description'>
1029 <p>
1030 Resets the positioning of the container element. This could be used was hidden when initialized, or if the colorpicker is inside of a moving area.
1031 </p>
1032 </div>
1033 </div>
1034
1035 <h3 id="methods-destroy">destroy</h3>
1036
1037 <div class='option-content'>
1038 <div class='description'>
1039 <p>
1040 Removes the colorpicker functionality and restores the element to its original state.
1041 </p>
1042 </div>
1043 </div>
1044
1045 <h3 id="methods-enable">enable</h3>
1046
1047 <div class='option-content'>
1048 <div class='description'>
1049 <p>
1050 Allows selection of the colorpicker component. If it is already enabled, this method does nothing.
1051 </p>
1052 <p>
1053 Additionally, this will cause the original (now hidden) input to be set as disabled.
1054 </p>
1055 </div>
1056 </div>
1057
1058 <h3 id="methods-disable">disable</h3>
1059
1060 <div class='option-content'>
1061 <div class='description'>
1062 <p>
1063 Disables selection of the colorpicker component. Adds the <code>sp-disabled</code> class onto the replacer element. If it is already disabled, this method does nothing.
1064 </p>
1065 <p>
1066 Additionally, this will remove the <code>disabled</code> property on the original (now hidden).
1067 </p>
1068 </div>
1069 </div>
1070
1071 <h3 id="methods-option">option</h3>
1072
1073 <div class='option-content'>
1074 <div class='description'>
1075 <p>
1076 Calling <code>option</code> with an option name will return the current value of that option. So, for example:
1077 </p>
1078 <pre class='prettyprint'>
1079$("input").spectrum({
1080 showInput: true
1081});
1082
1083$("input").spectrum("option", "showInput"); // true
1084 </pre>
1085 <p>
1086 Calling <code>option</code> with an option name and an option value will set the option to the new value.
1087 </p>
1088 <pre class='prettyprint'>
1089$("input").spectrum({
1090 showInput: true
1091});
1092
1093$("input").spectrum("option", "showInput", false);
1094$("input").spectrum("option", "showInput"); // false
1095 </pre>
1096 </div>
1097 </div>
1098
1099
1100 <h2 id="skinning">Skinning</h2>
1101
1102 <p>Since it is all built with HTML/CSS, you can skin it easily. There are two parts to the <a href='https://github.com/bgrins/spectrum/blob/master/spectrum.css'>spectrum.css</a> file, the core rules (at the top of the file), and the themable rules (at the bottom). Feel free to tweak these rules to make it look how you want.</p>
1103
1104 <h3 id="skinning-nonInput" class='point'>Non-input elements</h3>
1105 <p>
1106 You can use any element you would like to trigger the colorpicker: <a href='#' id='openWithLink'>Click me to open a colorpicker</a>, though it is strongly recommended to stick with <code>&lt;input&gt;</code> tags.
1107 </p>
1108
1109 <h2 id="details">Nitty Gritty</h2>
1110
1111 <h3 id="details-browserSupport" class='point'>Browser Support</h3>
1112 <p>I wanted this to work in the latest and greatest browsers, but also target backwords compatibility and <strong>mobile support</strong>.
1113 Here are the currently supported browers:
1114 <ul>
1115 <li>IE <small>6+</small></li>
1116 <li>Chrome <small>4+</small></li>
1117 <li>Firefox <small>3.6+</small></li>
1118 <li>Safari <small>4+</small></li>
1119 <li>Opera <small>11.1+</small></li>
1120 <li>iOS</li>
1121 </ul>
1122
1123 <h3 id="details-ieImplementation" class='point'>IE Implementation</h3>
1124 <p>
1125 IE Support is provided using
1126 <a href='http://msdn.microsoft.com/en-us/library/ms532997(v=vs.85).aspx'>proprietary filters</a>.
1127 Other browsers use CSS gradients.
1128 </p>
1129
1130
1131 <h3 id="details-acceptedColorInputs" class='point'>Accepted Color Inputs</h3>
1132 <p>Spectrum will use the color passed in to initialize. If there is no color passed in,
1133 it will try to parse a color based on the <code>value</code> of the input. The color parsing is based on the <a href='https://github.com/bgrins/TinyColor'>TinyColor</a> plugin, and accepts many forms of input:</p>
1134<pre class='prettyprint'>
1135red
1136#fff
1137fff
1138#ffffff
1139ffffff
1140rgb(255, 0, 0)
1141rgb 255 0 0
1142hsl(0, 100, 50)
1143hsl(0, 100%, 50%)
1144hsl 0 100 50
1145hsl 0 100% 50%
1146hsv(0, 100%, 100%)
1147hsv(0, 100, 100)
1148hsv 0 100% 100%
1149hsv 0 100 100
1150</pre>
1151 <p>It also provides the following forms of output:</p>
1152<pre class='prettyprint'>
1153var t = $("#element").spectrum("get");
1154t.toHex() // "ff0000"
1155t.toHexString() // "#ff0000"
1156t.toRgb() // {"r":255,"g":0,"b":0}
1157t.toRgbString() // "rgb(255, 0, 0)"
1158t.toHsv() // {"h":0,"s":1,"v":1}
1159t.toHsvString() // "hsv(0, 100%, 100%)"
1160t.toHsl() // {"h":0,"s":1,"l":0.5}
1161t.toHslString() // "hsl(0, 100%, 50%)"
1162t.toName() // "red"
1163</pre>
1164
1165
1166 <div class='footer'>
1167 <h2>Share</h2>
1168 <p>
1169 If you've made it this far, please share one of these links to help others find this project!
1170 <br />
1171 <a href='http://bgrins.github.com/spectrum'>JavaScript Colorpicker</a> |
1172 <a href='http://bgrins.github.com/spectrum'>jQuery Colorpicker</a> |
1173 <a href='http://bgrins.github.com/spectrum'>Mobile Colorpicker</a> |
1174 <a href='http://bgrins.github.com/spectrum'>Spectrum colorpicker</a>
1175 </p>
1176 <p>
1177 Thanks to all the <a href='https://github.com/bgrins/spectrum/graphs/contributors'>spectrum contributors</a> for committing code, documentation, and <a href='https://github.com/bgrins/spectrum/tree/master/i18n'>translations</a>.
1178 </p>
1179 <p>
1180 If you want to let me (<a href='http://twitter.com/bgrins'>@bgrins</a>) know you are using it, send me a link where it can be seen or add it to the <a href='https://github.com/bgrins/spectrum/wiki/Sites-Using-Spectrum'>list of projects using Spectrum</a>!
1181 </p>
1182 </div>
1183
1184 </div>
1185
1186</div>
1187
1188<script type="text/javascript" src="docs/prettify.js"></script>
1189<script type="text/javascript">
1190
1191 var _gaq = _gaq || [];
1192 _gaq.push(['_setAccount', 'UA-8259845-4']);
1193 _gaq.push(['_trackPageview']);
1194
1195 (function() {
1196 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
1197 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
1198 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
1199 })();
1200
1201</script>
1202</body>
1203</html>
Note: See TracBrowser for help on using the repository browser.