index.html 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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. <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">
  7. <meta name="author" content="Brian Grinstead and Spectrum contributors">
  8. <link rel="stylesheet" type="text/css" href="spectrum.css">
  9. <link rel="stylesheet" type="text/css" href="docs/bootstrap.css">
  10. <link rel="stylesheet" type="text/css" href="docs/docs.css">
  11. <script type="text/javascript" src="docs/jquery-1.9.1.js"></script>
  12. <script type="text/javascript" src="spectrum.js"></script>
  13. <script type='text/javascript' src='docs/toc.js'></script>
  14. <script type='text/javascript' src='docs/docs.js'></script>
  15. </head>
  16. <body>
  17. <div id='header'>
  18. <h1><a href='http://bgrins.github.com/spectrum'>Spectrum</a></h1> <h2><em>The No Hassle jQuery Colorpicker</em></h2>
  19. <div id='links'>
  20. <a href='http://github.com/bgrins/spectrum/zipball/1.3.4' class="btn btn-primary">Download Zip</a>
  21. View the <a href='http://github.com/bgrins/spectrum'>Source code</a>.
  22. Spectrum is a project by <a href='http://twitter.com/bgrins'>@bgrins</a>.
  23. </div>
  24. <br style='clear:both;' />
  25. </div>
  26. <div id='toc'></div>
  27. <div id='toc-slider'></div>
  28. <div id='docs'>
  29. <div id='docs-content'>
  30. <div id='switch-current'>
  31. <input type='text' name='color1' id='pick1' value='#ddddff' />
  32. <div id='switch-current-hsv' class='switch-current-output'></div>
  33. <div id='switch-current-rgb' class='switch-current-output'></div>
  34. <div id='switch-current-hex' class='switch-current-output'></div>
  35. </div>
  36. <div style='text-align:center;'>
  37. <input id="full" />
  38. </div>
  39. <pre class='prettyprint hide' id='code-heading'>
  40. &lt;input type='color' value='#f594d0' /&gt;
  41. <input type='color' class='basic' value='#f594d0' />
  42. </pre>
  43. <h2 id="why">Why A Colorpicker?</h2>
  44. <p><em>I wasn't satisfied with the solutions available for colorpicking</em>.
  45. Many of them included a ton of images, were hard to skin or customize, or were very large plugins.
  46. Here are the goals I had when making a new one:
  47. </p>
  48. <h3 id="why-footprint" class='point'>Small Footprint</h3>
  49. <div class='note'>see a working <a href='http://jsfiddle.net/bgrins/ctkY3/'>jsFiddle example</a></div>
  50. <p>Just include the needed CSS and JavaScript files, and you are ready to go! </p>
  51. <pre class='prettyprint' id='code-subheading'>
  52. &lt;script src='<a href='http://bgrins.github.com/spectrum/spectrum.js' target="_blank">spectrum.js</a>'&gt;&lt;/script&gt;
  53. &lt;link rel='stylesheet' href='<a href='http://bgrins.github.com/spectrum/spectrum.css' target="_blank">spectrum.css</a>' /&gt;
  54. </pre>
  55. <p><strong>We don't need no stinkin' images!</strong></p>
  56. <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>
  57. <h3 id="why-polyfill" class='point'>Polyfill</h3>
  58. <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.
  59. This mode needs to work without JavaScript enabled - and fallback to an input[type=text] like other HTML5 inputs.
  60. </p>
  61. <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>
  62. <h3 id="why-customizable" class='point'>Customizable</h3>
  63. <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>!
  64. It is easy to skin and customize the plugin with CSS, and there are a wide range of modes and options to explore.
  65. </p>
  66. <h3 id="why-mobile" class='point'>Mobile Support</h3>
  67. <p>Along with desktop browser support, I wanted a mobile colorpicker that was touch friendly, worked in iOS and Android, and used standards
  68. that maximize future mobile support.
  69. </p>
  70. <h3 id="why-devtools" class='point'>Devtools</h3>
  71. <p>
  72. Believe it or not, <strong>this colorpicker lives inside of Chrome and Safari devtools</strong> to make picking colors easier for web developers and designers.
  73. </p>
  74. <p>
  75. 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.
  76. </p>
  77. <p>
  78. From there, I opened a development case, entitled <a href="https://bugs.webkit.org/show_bug.cgi?id=71262">Web Inspector: Add colorpicker functionality to color swatches in Styles Sidebar</a>. 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>. Thanks to all the WebKit developers who helped me along the way with getting the code and UI ready for committing.
  79. </p>
  80. <h2 id="modes">Modes</h2>
  81. <h3 id="modes-input" class='point'>input[type=color]</h3>
  82. <p>
  83. If you just want to provide a polyfill for the native color input,
  84. the easiest way is to create an input with the type of color.
  85. Once a user's browser supports a native color control, it will opt to use their native control instead.
  86. </p>
  87. <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.
  88. </p>
  89. <pre class='prettyprint' id='code-subheading'>
  90. &lt;input type='color' name='color' /&gt;
  91. &lt;input type='color' name='color2' value='#3355cc' /&gt;
  92. </pre>
  93. <div class='example'>
  94. <form method="get">
  95. <input type='color' name='color' />
  96. <input type='color' name='color2' value='#3355cc' />
  97. <input type='color' name='color3' value='#000000' />
  98. <input type="submit" />
  99. </form>
  100. </div>
  101. <p><strong>That's it!</strong>
  102. The field will degrade to a text input if the user does not have JavaScript enabled,
  103. so that they will still be able to manually enter a color. You don't need to add a single line of code.
  104. </p>
  105. <h3 id="modes-custom" class='point'>Custom</h3>
  106. <p>If you want to get more into the functionality, just create a normal input and initialize it as a normal jQuery plugin.
  107. <strong>You can set a lot of options</strong> when initializing the colorpicker.
  108. See the 'Options' section below.
  109. </p>
  110. <pre class='prettyprint'>
  111. &lt;input type='text' id="custom" /&gt;
  112. </pre>
  113. <pre class='prettyprint'>
  114. &lt;script&gt;
  115. $("#custom").spectrum({
  116. color: "#f00"
  117. });
  118. &lt;/script&gt;
  119. </pre>
  120. <div class='example'>
  121. <input type='text' id='custom' />
  122. </div>
  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. <pre class='prettyprint'>
  130. &lt;input type='text' id="flat" /&gt;
  131. &lt;br/&gt;
  132. &lt;input type='text' id="flat" /&gt;
  133. </pre>
  134. <pre class='prettyprint'>
  135. $("#flat").spectrum({
  136. flat: true,
  137. showInput: true
  138. });
  139. $("#flatClearable").spectrum({
  140. flat: true,
  141. showInput: true,
  142. allowEmpty:true
  143. });
  144. </pre>
  145. <div class='example'>
  146. <input type='text' id='flat' value="limegreen" />
  147. <br/>
  148. <input type='text' id='flatClearable' value="limegreen" />
  149. </div>
  150. <!--
  151. <div class="alert">
  152. Heads up! Make sure you do not have a <code>maxlength</code> property set on your input. It has been known to break IE10.
  153. </div>
  154. -->
  155. <h2 id="options">Options</h2>
  156. <pre class='prettyprint'>
  157. $("#picker").spectrum({
  158. color: <strong>tinycolor</strong>,
  159. flat: bool,
  160. showInput: bool,
  161. showInitial: bool,
  162. allowEmpty: bool,
  163. showAlpha: bool,
  164. disabled: bool,
  165. localStorageKey: string,
  166. showPalette: bool,
  167. showPaletteOnly: bool,
  168. showSelectionPalette: bool,
  169. clickoutFiresChange: bool,
  170. cancelText: string,
  171. chooseText: string,
  172. containerClassName: string,
  173. replacerClassName: string,
  174. preferredFormat: string,
  175. maxSelectionSize: int,
  176. palette: [[string]],
  177. selectionPalette: [string]
  178. });
  179. </pre>
  180. <div class="alert alert-info">
  181. 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>.
  182. </div>
  183. <h3 id="options-color">Color</h3>
  184. <div class='option-content'>
  185. <div class='description'>
  186. <p>
  187. The initial color will be set with the <code>color</code> option.
  188. If you don't pass in a color, Spectrum will use the <code>value</code> attribute on the input.
  189. </p>
  190. <p>
  191. The color parsing is based on the <a href='https://github.com/bgrins/TinyColor'>TinyColor</a> plugin.
  192. This should parse any color string you throw at it.</p>
  193. </p>
  194. </div>
  195. <pre class='prettyprint'>
  196. &lt;input type='text' class='basic' value='red' /&gt;
  197. &lt;input type='text' class='basic' value='#0f0' /&gt;
  198. &lt;input type='text' class='basic' value='blue' /&gt;
  199. &lt;br /&gt;
  200. &lt;input type='text' class='override' /&gt;
  201. &lt;br /&gt;
  202. &lt;input type='text' class='startEmpty' value='' /&gt;
  203. </pre>
  204. <pre class='prettyprint'>
  205. &lt;script&gt;
  206. $(".basic").spectrum();
  207. $(".override").spectrum({
  208. color: "yellow"
  209. });
  210. (".startEmpty").spectrum({
  211. allowEmpty: true
  212. });
  213. &lt;/script&gt;
  214. </pre>
  215. <div class='example'>
  216. <input type='text' class='basic' value='red' />
  217. <input type='text' class='basic' value='green' />
  218. <input type='text' class='basic' value='blue' />
  219. <br />
  220. <input type='text' class='override' />
  221. <br/>
  222. <input type='text' class='startEmpty' value='' />
  223. </div>
  224. </div>
  225. <h3 id="options-showInput">Show Input</h3>
  226. <div class='option-content'>
  227. <div class='description'>
  228. <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.
  229. </div>
  230. <pre class='prettyprint'>
  231. $("#showInput").spectrum({
  232. showInput: true
  233. });
  234. $("#showInputWithClear").spectrum({
  235. showInput: true,
  236. allowEmpty:true
  237. });
  238. </pre>
  239. <div class='example'>
  240. <input type='text' name='showInput' id='showInput' />
  241. <br/>
  242. <input type='text' name='showInputWithClear' id='showInputWithClear' value='' />
  243. </div>
  244. </div>
  245. <h3 id="options-showAlpha">Show Alpha</h3>
  246. <div class='option-content'>
  247. <div class='description'>
  248. <p>You can allow alpha transparency selection. Check out these examples: </p>
  249. </div>
  250. <pre class='prettyprint'>
  251. $("#showAlpha").spectrum({
  252. showAlpha: true
  253. });
  254. </pre>
  255. <div class='example'>
  256. <input type='text' name='showAlpha' id='showAlpha' />
  257. </div>
  258. <div class='example'>
  259. <input type='text' name='showAlphaWithInput' id='showAlphaWithInput' />
  260. </div>
  261. </div>
  262. <h3 id="options-disabled">Disabled</h3>
  263. <div class='option-content'>
  264. <div class='description'>
  265. <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>
  266. </div>
  267. <pre class='prettyprint'>
  268. $("#disabled").spectrum({
  269. disabled: true
  270. });
  271. $("input:disabled").spectrum({
  272. });
  273. </pre>
  274. <div class='example'>
  275. <input type='text' name='disabled' id='disabled' value='lightblue' />
  276. <input type='text' disabled value='lightblue' />
  277. <button id='toggle-disabled' class='btn'>Toggle Disabled</button>
  278. </div>
  279. </div>
  280. <h3 id="options-showPalette">Show Palette</h3>
  281. <div class='option-content'>
  282. <div class='description'>
  283. <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>
  284. </div>
  285. <pre class='prettyprint'>
  286. $("#showPalette").spectrum({
  287. showPalette: true,
  288. palette: [
  289. ['black', 'white', 'blanchedalmond'],
  290. ['rgb(255, 128, 0);', 'hsv 100 70 50', 'lightyellow']
  291. ]
  292. });
  293. </pre>
  294. <div class='example'>
  295. <input type='text' name='showPalette' id='showPalette' value='lightblue' />
  296. </div>
  297. </div>
  298. <h3 id="options-showPaletteOnly">Show Palette Only</h3>
  299. <div class='option-content'>
  300. <div class='note'>see a working <a href='http://jsfiddle.net/bgrins/S45tW/'>jsFiddle example</a></div>
  301. <div class='description'>
  302. <p>If you'd like, spectrum can show the palettes you specify, and nothing else.</p>
  303. </div>
  304. <pre class='prettyprint'>
  305. $("#showPaletteOnly").spectrum({
  306. showPaletteOnly: true,
  307. showPalette:true,
  308. color: 'blanchedalmond',
  309. palette: [
  310. ['black', 'white', 'blanchedalmond',
  311. 'rgb(255, 128, 0);', 'hsv 100 70 50'],
  312. ['red', 'yellow', 'green', 'blue', 'violet']
  313. ]
  314. });
  315. </pre>
  316. <div class='example'>
  317. <span class='label label-result'>
  318. Result
  319. </span>
  320. <input type='text' name='showPaletteOnly' id='showPaletteOnly' />
  321. </div>
  322. </div>
  323. <h3 id="options-showSelectionPalette">Show Selection Palette</h3>
  324. <div class='option-content'>
  325. <div class='description'>
  326. <p>Spectrum can keep track of what has been selected by the user with the <code>showSelectionPalette</code> option.</p>
  327. <p>If the <code>localStorageKey</code> option is defined, the selection will be saved in the browser's localStorage object</p>
  328. </div>
  329. <pre class='prettyprint'>
  330. $("#showSelectionPalette").spectrum({
  331. showPalette: true,
  332. showSelectionPalette: true, // true by default
  333. palette: [ ]
  334. });
  335. $("#showSelectionPaletteStorage").spectrum({
  336. showPalette: true,
  337. showSelectionPalette: true,
  338. palette: [ ],
  339. localStorageKey: "spectrum.homepage", // Any Spectrum with the same string will share selection
  340. });
  341. </pre>
  342. <div class='example'>
  343. <span class='label label-info'>This colorpicker will store what you pick:</span><br /><br />
  344. <input type='text' name='showSelectionPalette' id='showSelectionPalette' value='lightblue' /><br /><br />
  345. <span class='label label-info'>Try switching between the two colorpickers or reloading your page, the chosen colors are always available:</span><br /><br />
  346. <input type='text' name='showSelectionPaletteStorage' id='showSelectionPaletteStorage' value='lightblue' />
  347. <input type='text' name='showSelectionPaletteStorage2' id='showSelectionPaletteStorage2' value='pink' />
  348. </div>
  349. </div>
  350. <h3 id="options-clickoutFiresChange">Clickout Fires Change</h3>
  351. <div class='option-content'>
  352. <div class='description'>
  353. <p>
  354. When clicking outside of the colorpicker, you can force it to fire a <code>change</code> event rather than having it revert the change.
  355. </p>
  356. </div>
  357. <pre class='prettyprint'>
  358. $("#clickoutFiresChange").spectrum({
  359. clickoutFiresChange: true
  360. });
  361. $("#clickoutDoesntChange").spectrum({
  362. });
  363. </pre>
  364. <div class='example'>
  365. <input type='text' name='clickoutFiresChange' id='clickoutFiresChange' value='goldenrod' />
  366. <input type='text' name='clickoutDoesntFireChange' id='clickoutDoesntFireChange' value='goldenrod' />
  367. </div>
  368. </div>
  369. <h3 id="options-showInitial">Show Initial</h3>
  370. <div class='option-content'>
  371. <div class='description'>
  372. <p>
  373. Spectrum can show the color that was initially set when opening.
  374. This provides an easy way to click back to what was set when opened.
  375. </p>
  376. </div>
  377. <pre class='prettyprint'>
  378. $("#showInitial").spectrum({
  379. showInitial: true
  380. });
  381. </pre>
  382. <div class='example'>
  383. <input type='text' name='showInitial' id='showInitial' value='goldenrod' />
  384. </div>
  385. </div>
  386. <h3 id="options-showInputAndInitial">Show Input and Initial</h3>
  387. <div class='option-content'>
  388. <div class='description'>
  389. <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>
  390. </div>
  391. <pre class='prettyprint'>
  392. $("#showInputAndInitial").spectrum({
  393. showInitial: true,
  394. showInput: true
  395. });
  396. </pre>
  397. <div class='example'>
  398. <input type='text' name='showInputAndInitial' id='showInputAndInitial' value='goldenrod' />
  399. </div>
  400. </div>
  401. <h3>Show Input, Initial, and Clear</h3>
  402. <div class='option-content'>
  403. <div class='description'>
  404. <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>
  405. </div>
  406. <pre class='prettyprint'>
  407. $("#showInputInitialClear").spectrum({
  408. allowEmpty:true,
  409. showInitial: true,
  410. showInput: true
  411. });
  412. </pre>
  413. <div class='example'>
  414. <input type='text' name='showInputInitialClear' id='showInputInitialClear' value='' />
  415. </div>
  416. </div>
  417. <h3 id="options-buttonText">Button Text</h3>
  418. <div class='option-content'>
  419. <div class='description'>
  420. <p>You can set the button's text using <code>cancelText</code> and <code>chooseText</code> properties.</p>
  421. </div>
  422. <pre class='prettyprint'>
  423. $("#buttonText").spectrum({
  424. allowEmpty:true,
  425. chooseText: "Alright",
  426. cancelText: "No way"
  427. });
  428. </pre>
  429. <div class='example'>
  430. <input type='text' name='buttonText' id='buttonText' value='orangered' />
  431. </div>
  432. </div>
  433. <h3 id="options-showButtons">Show Buttons</h3>
  434. <div class='option-content'>
  435. <div class='description'>
  436. <p>
  437. You can show or hide the buttons using the <code>showButtons</code> property.
  438. If there are no buttons, the behavior will be to fire the `change` event (and update the original input) when the picker is closed.
  439. </p>
  440. </div>
  441. <pre class='prettyprint'>
  442. $("#hideButtons").spectrum({
  443. showButtons: false
  444. });
  445. </pre>
  446. <div class='example'>
  447. <input type='text' name='hideButtons' id='hideButtons' value='orangered' />
  448. </div>
  449. </div>
  450. <h3 id="options-containerClassName">Container Class Name</h3>
  451. <div class='option-content'>
  452. <div class='description'>
  453. <p>
  454. You can add an additional class name to the just the container element using the <code>containerClassName</code> property.
  455. </p>
  456. </div>
  457. <pre class='prettyprint'>
  458. $("#containerClassName").spectrum({
  459. containerClassName: 'awesome'
  460. });
  461. </pre>
  462. <pre class='prettyprint'>
  463. .awesome {
  464. background: purple;
  465. }
  466. </pre>
  467. <style type='text/css'>
  468. .awesome {
  469. background: purple;
  470. }
  471. </style>
  472. <div class='example'>
  473. <input type='text' name='containerClassName' id='containerClassName' value='orangered' />
  474. </div>
  475. </div>
  476. <h3 id="options-replacerClassName">Replacer Class Name</h3>
  477. <div class='option-content'>
  478. <div class='description'>
  479. <p>
  480. You can add an additional class name to just the replacer element using the <code>replacerClassName</code> property.
  481. </p>
  482. </div>
  483. <pre class='prettyprint'>
  484. $("#replacerClassName").spectrum({
  485. replacerClassName: 'awesome'
  486. });
  487. </pre>
  488. <pre class='prettyprint'>
  489. .awesome {
  490. background: purple;
  491. }
  492. </pre>
  493. <style type='text/css'>
  494. .awesome {
  495. background: purple;
  496. }
  497. </style>
  498. <div class='example'>
  499. <input type='text' name='replacerClassName' id='replacerClassName' value='orangered' />
  500. </div>
  501. </div>
  502. <h3 id="options-preferredFormat">Preferred Format</h3>
  503. <div class='option-content'>
  504. <div class='description'>
  505. <p>You can set the format that is displayed in the text box.</p>
  506. <p>This will also change the format that is displayed in the titles from the palette swatches.</p>
  507. </div>
  508. <pre class='prettyprint'>
  509. $("#preferredHex").spectrum({
  510. preferredFormat: "hex",
  511. showInput: true,
  512. showPalette: true,
  513. palette: [["red", "rgba(0, 255, 0, .5)", "rgb(0, 0, 255)"]]
  514. });
  515. $("#preferredHex3").spectrum({
  516. preferredFormat: "hex3",
  517. showInput: true,
  518. showPalette: true,
  519. palette: [["red", "rgba(0, 255, 0, .5)", "rgb(0, 0, 255)"]]
  520. });
  521. $("#preferredHsl").spectrum({
  522. preferredFormat: "hsl",
  523. showInput: true,
  524. showPalette: true,
  525. palette: [["red", "rgba(0, 255, 0, .5)", "rgb(0, 0, 255)"]]
  526. });
  527. $("#preferredRgb").spectrum({
  528. preferredFormat: "rgb",
  529. showInput: true,
  530. showPalette: true,
  531. palette: [["red", "rgba(0, 255, 0, .5)", "rgb(0, 0, 255)"]]
  532. });
  533. $("#preferredName").spectrum({
  534. preferredFormat: "name",
  535. showInput: true,
  536. showPalette: true,
  537. palette: [["red", "rgba(0, 255, 0, .5)", "rgb(0, 0, 255)"]]
  538. });
  539. $("#preferredNone").spectrum({
  540. showInput: true,
  541. showPalette: true,
  542. palette: [["red", "rgba(0, 255, 0, .5)", "rgb(0, 0, 255)"]]
  543. });
  544. </pre>
  545. <div class='example'>
  546. <div class='alert alert-info'>Hex</div>
  547. <input type='text' name='preferredHex' id='preferredHex' value='orangered' />
  548. <div class='alert alert-info'>Hex (3 Characters If Possible)</div>
  549. <input type='text' name='preferredHex3' id='preferredHex3' value='orangered' />
  550. <div class='alert alert-info'>Hsl</div>
  551. <input type='text' name='preferredHsl' id='preferredHsl' value='orangered' />
  552. <div class='alert alert-info'>Rgb</div>
  553. <input type='text' name='preferredRgb' id='preferredRgb' value='orangered' />
  554. <div class='alert alert-info'>Name (Falls back to hex)</div>
  555. <input type='text' name='preferredName' id='preferredName' value='orangered' />
  556. <div class='alert alert-info'>None (Depends on input - try changing formats with the text box)</div>
  557. <input type='text' name='preferredNone' id='preferredNone' value='orangered' />
  558. </div>
  559. </div>
  560. <h2 id="events">Events</h2>
  561. <pre class='prettyprint'>
  562. $("#picker").spectrum({
  563. move: function(tinycolor) { },
  564. show: function(tinycolor) { },
  565. hide: function(tinycolor) { },
  566. beforeShow: function(tinycolor) { },
  567. });
  568. </pre>
  569. <h3 id="events-change">change</h3>
  570. <div class='option-content'>
  571. <div class='description'>
  572. <p>Called as the original input changes. Only happens when the input is closed or the 'Choose' button is clicked.</p>
  573. </div>
  574. <pre class='prettyprint'>
  575. change: function(color) {
  576. color.toHexString(); // #ff0000
  577. }
  578. </pre>
  579. <div class='example'>
  580. <input type='text' value='blanchedalmond' name='changeOnMoveNo' id='changeOnMoveNo' />
  581. <em id='changeOnMoveNoLabel' class='em-label'></em>
  582. </div>
  583. </div>
  584. <h3 id="events-move">move</h3>
  585. <div class='option-content'>
  586. <div class='description'>
  587. <p>Called as the user moves around within the colorpicker</p>
  588. </div>
  589. <pre class='prettyprint'>
  590. move: function(color) {
  591. color.toHexString(); // #ff0000
  592. }
  593. </pre>
  594. <div class='example'>
  595. <input type='text' value='blanchedalmond' name='changeOnMove' id='changeOnMove' />
  596. <em id='changeOnMoveLabel' class='em-label'></em>
  597. </div>
  598. </div>
  599. <h3 id="events-hide">hide</h3>
  600. <div class='option-content'>
  601. <div class='description'>
  602. <p>
  603. Called after the colorpicker is hidden.
  604. This happens when clicking outside of the picker while it is open.
  605. Note, when any colorpicker on the page is shown it will hide any that are already open.
  606. This event is ignored on a flat colorpicker.
  607. </p>
  608. </div>
  609. <pre class='prettyprint'>
  610. hide: function(color) {
  611. color.toHexString(); // #ff0000
  612. }
  613. </pre>
  614. <div class='example'>
  615. <input type='text' value='blanchedalmond' name='eventhide' id='eventhide' />
  616. <em id='eventhideLabel' class='em-label'></em>
  617. </div>
  618. </div>
  619. <h3 id="events-show">show</h3>
  620. <div class='option-content'>
  621. <div class='description'>
  622. <p>
  623. Called after the colorpicker is opened.
  624. This is ignored on a flat colorpicker.
  625. Note, when any colorpicker on the page is shown it will hide any that are already open.
  626. </p>
  627. </div>
  628. <pre class='prettyprint'>
  629. show: function(color) {
  630. color.toHexString(); // #ff0000
  631. }
  632. </pre>
  633. <div class='example'>
  634. <input type='text' value='blanchedalmond' name='eventshow' id='eventshow' />
  635. <em id='eventshowLabel' class='em-label'></em>
  636. </div>
  637. </div>
  638. <h3 id="events-beforeShow">beforeShow</h3>
  639. <div class='option-content'>
  640. <div class='description'>
  641. <p>
  642. You can prevent the colorpicker from showing up if you return false in the beforeShow event.
  643. This event is ignored on a flat colorpicker.
  644. </p>
  645. </div>
  646. <pre class='prettyprint'>
  647. beforeShow: function(color) {
  648. return false; // Will never show up
  649. }
  650. </pre>
  651. <div class='example'>
  652. <input type='text' value='blanchedalmond' name='beforeShow' id='beforeShow' />
  653. </div>
  654. </div>
  655. <h3 id="events-dragstart">dragstart</h3>
  656. <div class='option-content'>
  657. <div class='description'>
  658. <p>
  659. Called at the beginning of a drag event on either
  660. hue slider, alpha slider, or main color picker areas
  661. </p>
  662. </div>
  663. <pre class='prettyprint'>
  664. $(element).on("dragstart.spectrum"): function(e, color) {
  665. color.toHexString(); // #ff0000
  666. }
  667. </pre>
  668. <div class='example'>
  669. <input type='text' value='blanchedalmond' name='eventdragstart' id='eventdragstart' />
  670. <em id='eventdragstartLabel' class='em-label'></em>
  671. </div>
  672. </div>
  673. <h3 id="events-dragstop">dragstop</h3>
  674. <div class='option-content'>
  675. <div class='description'>
  676. <p>
  677. Called at the end of a drag event on either
  678. hue slider, alpha slider, or main color picker areas
  679. </p>
  680. </div>
  681. <pre class='prettyprint'>
  682. $(element).on("dragstop.spectrum"): function(e, color) {
  683. color.toHexString(); // #ff0000
  684. }
  685. </pre>
  686. <div class='example'>
  687. <input type='text' value='blanchedalmond' name='eventdragstop' id='eventdragstop' />
  688. <em id='eventdragstopLabel' class='em-label'></em>
  689. </div>
  690. </div>
  691. <h2 id="methods">Methods</h2>
  692. <pre class='prettyprint'>
  693. $("#picker").spectrum("show");
  694. $("#picker").spectrum("hide");
  695. $("#picker").spectrum("toggle");
  696. $("#picker").spectrum("get");
  697. $("#picker").spectrum("set", colorString);
  698. $("#picker").spectrum("container");
  699. $("#picker").spectrum("reflow");
  700. $("#picker").spectrum("destroy");
  701. $("#picker").spectrum("enable");
  702. $("#picker").spectrum("disable");
  703. $("#picker").spectrum("option", optionName);
  704. $("#picker").spectrum("option", optionName, newOptionValue);
  705. </pre>
  706. <h3 id="methods-show">show</h3>
  707. <div class='option-content'>
  708. <div class='description'>
  709. <p>
  710. Shows the colorpicker.
  711. </p>
  712. </div>
  713. </div>
  714. <h3 id="methods-hide">hide</h3>
  715. <div class='option-content'>
  716. <div class='description'>
  717. <p>
  718. Hides the colorpicker.
  719. </p>
  720. </div>
  721. </div>
  722. <h3 id="methods-toggle">toggle</h3>
  723. <div class='option-content'>
  724. <div class='description'>
  725. <p>
  726. Toggles the colorpicker.
  727. </p>
  728. <p class="warning">
  729. <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.
  730. </p>
  731. </div>
  732. <pre class='prettyprint'>
  733. $("#btn-toggle").click(function() {
  734. $("#toggle").spectrum("toggle");
  735. return false;
  736. });
  737. </pre>
  738. <div class='example'>
  739. <input type='text' value='blanchedalmond' name='toggle' id='toggle' />
  740. <button id='btn-toggle'>Toggle</button>
  741. </div>
  742. </div>
  743. <h3 id="methods-get">get</h3>
  744. <div class='option-content'>
  745. <div class='description'>
  746. <p>
  747. Gets the current value from the colorpicker.
  748. </p>
  749. </div>
  750. </div>
  751. <h3 id="methods-set">set</h3>
  752. <div class='option-content'>
  753. <div class='description'>
  754. <p>
  755. Setting the colorpicker programmatically will update the original input.
  756. </p>
  757. <p>
  758. Note: this will <strong>not</strong> fire the <code>change</code> event,
  759. to prevent infinite loops from calling <code>set</code> from within <code>change</code>.
  760. </p>
  761. </div>
  762. <pre class='prettyprint'>
  763. &lt;input type='text' value='blanchedalmond' name='triggerSet' id='triggerSet' /&gt;
  764. &lt;input type='text' placeholder='Enter A Color' id='enterAColor' /&gt;
  765. &lt;button id='btnEnterAColor'&gt;Trigger Set&lt;/button&gt;
  766. &lt;script&gt;
  767. $("#triggerSet").spectrum();
  768. // Show the original input to demonstrate the value changing when calling `set`
  769. $("#triggerSet").show();
  770. $("#btnEnterAColor").click(function() {
  771. $("#triggerSet").spectrum("set", $("#enterAColor").val());
  772. });
  773. &lt;/script&gt;
  774. </pre>
  775. <div class='example'>
  776. <input type='text' value='blanchedalmond' name='triggerSet' id='triggerSet' /><br /><br />
  777. <input type='text' placeholder='Enter A Color' id='enterAColor' /><button id='btnEnterAColor'>Trigger Set</button>
  778. </div>
  779. </div>
  780. <h3 id="methods-container">container</h3>
  781. <div class='option-content'>
  782. <div class='description'>
  783. <p>
  784. Retrieves the container element of the colorpicker, in case you want to manaully position it or do other things.
  785. </p>
  786. </div>
  787. </div>
  788. <h3 id="methods-reflow">reflow</h3>
  789. <div class='option-content'>
  790. <div class='description'>
  791. <p>
  792. 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.
  793. </p>
  794. </div>
  795. </div>
  796. <h3 id="methods-destroy">destroy</h3>
  797. <div class='option-content'>
  798. <div class='description'>
  799. <p>
  800. Removes the colorpicker functionality and restores the element to its original state.
  801. </p>
  802. </div>
  803. </div>
  804. <h3 id="methods-enable">enable</h3>
  805. <div class='option-content'>
  806. <div class='description'>
  807. <p>
  808. Allows selection of the colorpicker component. If it is already enabled, this method does nothing.
  809. </p>
  810. <p>
  811. Additionally, this will cause the original (now hidden) input to be set as disabled.
  812. </p>
  813. </div>
  814. </div>
  815. <h3 id="methods-disable">disable</h3>
  816. <div class='option-content'>
  817. <div class='description'>
  818. <p>
  819. 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.
  820. </p>
  821. <p>
  822. Additionally, this will remove the <code>disabled</code> property on the original (now hidden).
  823. </p>
  824. </div>
  825. </div>
  826. <h3 id="methods-option">option</h3>
  827. <div class='option-content'>
  828. <div class='description'>
  829. <p>
  830. Calling <code>option</code> with an option name will return the current value of that option. So, for example:
  831. </p>
  832. <pre class='prettyprint'>
  833. $("input").spectrum({
  834. showInput: true
  835. });
  836. $("input").spectrum("option", "showInput"); // true
  837. </pre>
  838. <p>
  839. Calling <code>option</code> with an option name and an option value will set the option to the new value.
  840. </p>
  841. <pre class='prettyprint'>
  842. $("input").spectrum({
  843. showInput: true
  844. });
  845. $("input").spectrum("option", "showInput", false);
  846. $("input").spectrum("option", "showInput"); // false
  847. </pre>
  848. </div>
  849. </div>
  850. <h2 id="skinning">Skinning</h2>
  851. <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>
  852. <h3 id="skinning-nonInput" class='point'>Non-input elements</h3>
  853. <p>
  854. 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.
  855. </p>
  856. <h2 id="details">Nitty Gritty</h2>
  857. <h3 id="details-browserSupport" class='point'>Browser Support</h3>
  858. <p>I wanted this to work in the latest and greatest browsers, but also target backwords compatibility and <strong>mobile support</strong>.
  859. Here are the currently supported browers:
  860. <ul>
  861. <li>IE <small>6+</small></li>
  862. <li>Chrome <small>4+</small></li>
  863. <li>Firefox <small>3.6+</small></li>
  864. <li>Safari <small>4+</small></li>
  865. <li>Opera <small>11.1+</small></li>
  866. <li>iOS</li>
  867. </ul>
  868. <h3 id="details-ieImplementation" class='point'>IE Implementation</h3>
  869. <p>
  870. IE Support is provided using
  871. <a href='http://msdn.microsoft.com/en-us/library/ms532997(v=vs.85).aspx'>proprietary filters</a>.
  872. Other browsers use CSS gradients.
  873. </p>
  874. <h3 id="details-acceptedColorInputs" class='point'>Accepted Color Inputs</h3>
  875. <p>Spectrum will use the color passed in to initialize. If there is no color passed in,
  876. 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>
  877. <pre class='prettyprint'>
  878. red
  879. #fff
  880. fff
  881. #ffffff
  882. ffffff
  883. rgb(255, 0, 0)
  884. rgb 255 0 0
  885. hsl(0, 100, 50)
  886. hsl(0, 100%, 50%)
  887. hsl 0 100 50
  888. hsl 0 100% 50%
  889. hsv(0, 100%, 100%)
  890. hsv(0, 100, 100)
  891. hsv 0 100% 100%
  892. hsv 0 100 100
  893. </pre>
  894. <p>It also provides the following forms of output:</p>
  895. <pre class='prettyprint'>
  896. var t = $("#element").spectrum("get");
  897. t.toHex() // "ff0000"
  898. t.toHexString() // "#ff0000"
  899. t.toRgb() // {"r":255,"g":0,"b":0}
  900. t.toRgbString() // "rgb(255, 0, 0)"
  901. t.toHsv() // {"h":0,"s":1,"v":1}
  902. t.toHsvString() // "hsv(0, 100%, 100%)"
  903. t.toHsl() // {"h":0,"s":1,"l":0.5}
  904. t.toHslString() // "hsl(0, 100%, 50%)"
  905. t.toName() // "red"
  906. </pre>
  907. <div class='footer'>
  908. <h2>Share</h2>
  909. <p>
  910. If you've made it this far, please share one of these links to help others find this project!
  911. <br />
  912. <a href='http://bgrins.github.com/spectrum'>JavaScript Colorpicker</a> |
  913. <a href='http://bgrins.github.com/spectrum'>jQuery Colorpicker</a> |
  914. <a href='http://bgrins.github.com/spectrum'>Mobile Colorpicker</a> |
  915. <a href='http://bgrins.github.com/spectrum'>Spectrum colorpicker</a>
  916. </p>
  917. <p>
  918. 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>.
  919. </p>
  920. <p>
  921. 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>!
  922. </p>
  923. </div>
  924. </div>
  925. </div>
  926. <script type="text/javascript" src="docs/prettify.js"></script>
  927. <script type="text/javascript">
  928. var _gaq = _gaq || [];
  929. _gaq.push(['_setAccount', 'UA-8259845-4']);
  930. _gaq.push(['_trackPageview']);
  931. (function() {
  932. var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  933. ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  934. var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  935. })();
  936. </script>
  937. </body>
  938. </html>