├── .gitignore ├── LICENSE.txt ├── README.md ├── bower.json ├── demo.html ├── jquery-simplecolorpicker-inline.png ├── jquery-simplecolorpicker-picker.png ├── jquery.simplecolorpicker-fontawesome.css ├── jquery.simplecolorpicker-glyphicons.css ├── jquery.simplecolorpicker-regularfont.css ├── jquery.simplecolorpicker.css ├── jquery.simplecolorpicker.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .idea/ -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012-2013 Tanguy Krotoff 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **I don't use jquery-simplecolorpicker anymore so I won't update this repository. If you have a well maintained fork, I will be happy to promote it here.** 2 | 3 | # Very simple jQuery color picker 4 | 5 | Yet another jQuery color picker. This plugin is unobtrusive and integrates well with Twitter Bootstrap (it works just fine without). 6 | The source code only requires jQuery and is about 200 lines of JavaScript and 100 lines of CSS. 7 | 8 | See the [online demo](http://plnkr.co/edit/VVclW0?p=preview). 9 | 10 | ![jquery-simplecolorpicker-inline.png](jquery-simplecolorpicker-inline.png) 11 | 12 | ![jquery-simplecolorpicker-picker.png](jquery-simplecolorpicker-picker.png) 13 | 14 | ## npm 15 | 16 | ``` 17 | npm install jquery-simplecolorpicker 18 | ``` 19 | 20 | ## Bower 21 | 22 | ``` 23 | bower install jquery-simplecolorpicker 24 | ``` 25 | 26 | ## How to use 27 | 28 | Create a HTML select: 29 | 30 | ```HTML 31 | 45 | ``` 46 | 47 | add the plugin files: 48 | 49 | ```HTML 50 | 51 | 52 | 53 | 54 | ``` 55 | 56 | then call the plugin: 57 | 58 | ```JavaScript 59 | $('select[name="colorpicker"]').simplecolorpicker(); 60 | $('select[name="colorpicker"]').simplecolorpicker('selectColor', '#7bd148'); 61 | $('select[name="colorpicker"]').simplecolorpicker('destroy'); 62 | ``` 63 | 64 | and pass some options if needed: 65 | 66 | ```JavaScript 67 | $('select[name="colorpicker"]').simplecolorpicker({ 68 | picker: true 69 | }).on('change', function() { 70 | $(document.body).css('background-color', $('select[name="colorpicker"]').val()); 71 | }); 72 | ``` 73 | 74 | ### Options 75 | 76 | - theme: font to use for the ok/check mark (default: `''`), available themes: [`regularfont`](https://github.com/tkrotoff/jquery-simplecolorpicker/blob/master/jquery.simplecolorpicker-regularfont.css), [`fontawesome`](https://github.com/tkrotoff/jquery-simplecolorpicker/blob/master/jquery.simplecolorpicker-fontawesome.css), [`glyphicons`](https://github.com/tkrotoff/jquery-simplecolorpicker/blob/master/jquery.simplecolorpicker-glyphicons.css) 77 | - picker: show the colors inside a picker instead of inline (default: `false`) 78 | - pickerDelay: show and hide animation delay in milliseconds (default: `0`) 79 | 80 | ## Browser support 81 | 82 | Simplecolorpicker supports all modern browsers starting with Internet Explorer 8 included. 83 | It is recommended to not use any font theme with IE8. 84 | 85 | ## HTML5 new color input 86 | 87 | HTML5 provides a new input to select colors. Its implementation inside modern browsers is still lacking. 88 | The new color input does not provide any option to customize the color list and 89 | most of the time the widget is less user-friendly than the one provided by simplecolorpicker. 90 | 91 | See http://slides.html5rocks.com/#new-form-types 92 | 93 | See http://dev.w3.org/html5/markup/input.color.html#input.color 94 | 95 | ## AngularJS directive 96 | 97 | See [simplecolorpicker directive](http://plnkr.co/edit/rKM3QWXDC3vGVPe3QFWV?p=preview). 98 | If you find a solution for the `setTimeout()` hack, please tell me. 99 | 100 | Here [another directive](http://plnkr.co/edit/zlP0RSH3m0ghsefHeaLI?p=preview) written by [KGZM](https://github.com/KGZM) that re-implements simplecolorpicker. 101 | For the explanations, read this [Google Groups discussion](https://groups.google.com/d/topic/angular/nBZsvLOZxvI/discussion). 102 | 103 | ## Ruby on Rails 104 | 105 | A gem is available at https://github.com/tkrotoff/jquery-simplecolorpicker-rails 106 | 107 | ## Copyright and license 108 | 109 | Licensed under the MIT license. 110 | Copyright (C) 2012-2013 Tanguy Krotoff 111 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jquery-simplecolorpicker", 3 | "version": "0.3.1", 4 | "dependencies": { 5 | "jquery": ">=1.10" 6 | }, 7 | "repository": { 8 | "type": "git", 9 | "url": "git://github.com/tkrotoff/jquery-simplecolorpicker.git" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /demo.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Color picker for jQuery 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 |

25 | 26 |

Short list of colors

27 | colorpicker-shortlist: 28 | 42 | 43 |

Long list of colors

44 | colorpicker-longlist: 45 | 71 | 72 |

No theme

73 | colorpicker-notheme: 74 | 88 | 89 |

'regularfont' theme

90 | colorpicker-regularfont: 91 | 105 | 106 |

'glyphicons' theme

107 | colorpicker-glyphicons: 108 | 122 | 123 |

'fontawesome' theme

124 | colorpicker-fontawesome: 125 | 139 | 140 |

Bootstrap 3 form

141 |
142 |
143 | 144 |
145 | 146 |
147 |
148 |
149 |
150 |
151 | 154 |
155 |
156 |
157 |
158 | 159 |
160 | 174 |
175 |
176 |
177 | 178 |
179 | 186 |
187 |
188 |
189 | 190 |
191 | 192 |
193 |
194 |
195 |
196 | 197 | 198 |
199 |
200 |
201 | 202 |

Bootstrap 3 modal window

203 | Launch demo modal 204 | 251 | 252 |

Option selected

253 | colorpicker-option-selected: 254 | 268 | 269 |

Options disabled

270 | colorpicker-options-disabled: 271 | 285 | 286 |

Option selected and disabled at the same time

287 | colorpicker-option-selected-disabled: 288 | 302 | 303 |

Optgroups

304 | colorpicker-optgroups: 305 | 321 | 322 |

Change background color

323 | colorpicker-change-background-color: 324 | 338 | 339 |

selectColor('#fbd75b') after 5 seconds

340 | colorpicker-selectColor-#fbd75b: 341 | 355 | 356 |

selectColor('#FBD75B') after 5 seconds

357 | colorpicker-selectColor-#FBD75B: 358 | 372 | 373 |

Multiple selectColor('#fbd75b') after 5 seconds

374 | colorpicker-selectColor-#fbd75b-multiple: 375 | 391 | 392 |

selectColor('unknown') after 5 seconds

393 | colorpicker-selectColor-unknown: 394 | 408 | 409 |

Picker short list

410 | colorpicker-picker-shortlist: 411 | 425 | 426 |

Picker long list

427 | colorpicker-picker-longlist: 428 | 454 | 455 |

Picker with 1s animation delay

456 | colorpicker-picker-delay: 457 | 471 | 472 |

Picker with option selected

473 | colorpicker-picker-option-selected: 474 | 488 | 489 |

Picker with options disabled

490 | colorpicker-picker-options-disabled: 491 | 505 | 506 |

Picker with option selected and disabled at the same time

507 | colorpicker-picker-option-selected-disabled: 508 | 522 | 523 |

Picker with Optgroups

524 | colorpicker-picker-optgroups: 525 | 541 | 542 |

Picker with selectColor('#fbd75b') after 5 seconds

543 | colorpicker-picker-selectColor-#fbd75b: 544 | 558 | 559 |

Picker with selectColor('unknown') after 5 seconds

560 | colorpicker-picker-selectColor-unknown: 561 | 575 | 576 | 577 | 578 | 579 | 580 | 653 | 654 | 655 | 656 | -------------------------------------------------------------------------------- /jquery-simplecolorpicker-inline.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tkrotoff/jquery-simplecolorpicker/81b652ac5ff40065a2f4bb50392d727d69a23af0/jquery-simplecolorpicker-inline.png -------------------------------------------------------------------------------- /jquery-simplecolorpicker-picker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tkrotoff/jquery-simplecolorpicker/81b652ac5ff40065a2f4bb50392d727d69a23af0/jquery-simplecolorpicker-picker.png -------------------------------------------------------------------------------- /jquery.simplecolorpicker-fontawesome.css: -------------------------------------------------------------------------------- 1 | .simplecolorpicker.fontawesome span.color[data-selected]:after { 2 | font-family: 'FontAwesome'; 3 | -webkit-font-smoothing: antialiased; 4 | 5 | content: '\f00c'; /* Ok/check mark */ 6 | 7 | margin-right: 1px; 8 | margin-left: 1px; 9 | } 10 | -------------------------------------------------------------------------------- /jquery.simplecolorpicker-glyphicons.css: -------------------------------------------------------------------------------- 1 | .simplecolorpicker.glyphicons span.color[data-selected]:after { 2 | /* Taken from glyphicon class. */ 3 | position: relative; 4 | top: 1px; 5 | font-family: 'Glyphicons Halflings'; 6 | line-height: .9; 7 | -webkit-font-smoothing: antialiased; 8 | 9 | content: '\e013'; /* Ok/check mark */ 10 | 11 | margin-right: 1px; 12 | margin-left: 1px; 13 | } 14 | -------------------------------------------------------------------------------- /jquery.simplecolorpicker-regularfont.css: -------------------------------------------------------------------------------- 1 | .simplecolorpicker.regularfont span.color[data-selected]:after { 2 | content: '\2714'; /* Ok/check mark */ 3 | 4 | margin-right: 2.2px; 5 | margin-left: 2.2px; 6 | } 7 | -------------------------------------------------------------------------------- /jquery.simplecolorpicker.css: -------------------------------------------------------------------------------- 1 | /*! 2 | * Very simple jQuery Color Picker 3 | * https://github.com/tkrotoff/jquery-simplecolorpicker 4 | * 5 | * Copyright (C) 2012-2013 Tanguy Krotoff 6 | * 7 | * Licensed under the MIT license 8 | */ 9 | 10 | /** 11 | * Inspired by Bootstrap Twitter. 12 | * See https://github.com/twbs/bootstrap/blob/master/less/navbar.less 13 | * See https://github.com/twbs/bootstrap/blob/master/less/dropdowns.less 14 | */ 15 | 16 | .simplecolorpicker.picker { 17 | position: absolute; 18 | top: 100%; 19 | left: 0; 20 | z-index: 1051; /* Above Bootstrap modal (@zindex-modal = 1050) */ 21 | display: none; 22 | float: left; 23 | 24 | min-width: 160px; 25 | max-width: 283px; /* @popover-max-width = 276px + 7 */ 26 | 27 | padding: 5px 0 0 5px; 28 | margin: 2px 0 0; 29 | list-style: none; 30 | background-color: #fff; /* @dropdown-bg */ 31 | 32 | border: 1px solid #ccc; /* @dropdown-fallback-border */ 33 | border: 1px solid rgba(0, 0, 0, .15); /* @dropdown-border */ 34 | 35 | -webkit-border-radius: 4px; /* @border-radius-base */ 36 | -moz-border-radius: 4px; 37 | border-radius: 4px; 38 | 39 | -webkit-box-shadow: 0 6px 12px rgba(0, 0, 0, .175); 40 | -moz-box-shadow: 0 6px 12px rgba(0, 0, 0, .175); 41 | box-shadow: 0 6px 12px rgba(0, 0, 0, .175); 42 | 43 | -webkit-background-clip: padding-box; 44 | -moz-background-clip: padding; 45 | background-clip: padding-box; 46 | } 47 | 48 | .simplecolorpicker.inline { 49 | display: inline-block; 50 | padding: 6px 0; 51 | } 52 | 53 | .simplecolorpicker span { 54 | margin: 0 5px 5px 0; 55 | } 56 | 57 | .simplecolorpicker.icon, 58 | .simplecolorpicker span.color { 59 | display: inline-block; 60 | 61 | cursor: pointer; 62 | border: 1px solid transparent; 63 | } 64 | 65 | .simplecolorpicker.icon:after, 66 | .simplecolorpicker span.color:after { 67 | content: '\00a0\00a0\00a0\00a0'; /* Spaces */ 68 | } 69 | 70 | .simplecolorpicker span.color[data-disabled]:hover { 71 | cursor: not-allowed; 72 | border: 1px solid transparent; 73 | } 74 | 75 | .simplecolorpicker span.color:hover, 76 | .simplecolorpicker span.color[data-selected], 77 | .simplecolorpicker span.color[data-selected]:hover { 78 | border: 1px solid #222; /* @gray-dark */ 79 | } 80 | .simplecolorpicker span.color[data-selected]:after { 81 | color: #fff; 82 | } 83 | 84 | /* Vertical separator, replaces optgroup. */ 85 | .simplecolorpicker span.vr { 86 | border-left: 1px solid #222; /* @gray-dark */ 87 | } 88 | -------------------------------------------------------------------------------- /jquery.simplecolorpicker.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * Very simple jQuery Color Picker 3 | * https://github.com/tkrotoff/jquery-simplecolorpicker 4 | * 5 | * Copyright (C) 2012-2013 Tanguy Krotoff 6 | * 7 | * Licensed under the MIT license 8 | */ 9 | 10 | (function($) { 11 | 'use strict'; 12 | 13 | /** 14 | * Constructor. 15 | */ 16 | var SimpleColorPicker = function(select, options) { 17 | this.init('simplecolorpicker', select, options); 18 | }; 19 | 20 | /** 21 | * SimpleColorPicker class. 22 | */ 23 | SimpleColorPicker.prototype = { 24 | constructor: SimpleColorPicker, 25 | 26 | init: function(type, select, options) { 27 | var self = this; 28 | 29 | self.type = type; 30 | 31 | self.$select = $(select); 32 | self.$select.hide(); 33 | 34 | self.options = $.extend({}, $.fn.simplecolorpicker.defaults, options); 35 | 36 | self.$colorList = null; 37 | 38 | if (self.options.picker === true) { 39 | var selectText = self.$select.find('> option:selected').text(); 40 | self.$icon = $('' 44 | + '').insertAfter(self.$select); 45 | self.$icon.on('click.' + self.type, $.proxy(self.showPicker, self)); 46 | 47 | self.$picker = $('').appendTo(document.body); 48 | self.$colorList = self.$picker; 49 | 50 | // Hide picker when clicking outside 51 | $(document).on('mousedown.' + self.type, $.proxy(self.hidePicker, self)); 52 | self.$picker.on('mousedown.' + self.type, $.proxy(self.mousedown, self)); 53 | } else { 54 | self.$inline = $('').insertAfter(self.$select); 55 | self.$colorList = self.$inline; 56 | } 57 | 58 | // Build the list of colors 59 | // 60 | self.$select.find('> option').each(function() { 61 | var $option = $(this); 62 | var color = $option.val(); 63 | 64 | var isSelected = $option.is(':selected'); 65 | var isDisabled = $option.is(':disabled'); 66 | 67 | var selected = ''; 68 | if (isSelected === true) { 69 | selected = ' data-selected'; 70 | } 71 | 72 | var disabled = ''; 73 | if (isDisabled === true) { 74 | disabled = ' data-disabled'; 75 | } 76 | 77 | var title = ''; 78 | if (isDisabled === false) { 79 | title = ' title="' + $option.text() + '"'; 80 | } 81 | 82 | var role = ''; 83 | if (isDisabled === false) { 84 | role = ' role="button" tabindex="0"'; 85 | } 86 | 87 | var $colorSpan = $('' 94 | + ''); 95 | 96 | self.$colorList.append($colorSpan); 97 | $colorSpan.on('click.' + self.type, $.proxy(self.colorSpanClicked, self)); 98 | 99 | var $next = $option.next(); 100 | if ($next.is('optgroup') === true) { 101 | // Vertical break, like hr 102 | self.$colorList.append(''); 103 | } 104 | }); 105 | }, 106 | 107 | /** 108 | * Changes the selected color. 109 | * 110 | * @param color the hexadecimal color to select, ex: '#fbd75b' 111 | */ 112 | selectColor: function(color) { 113 | var self = this; 114 | 115 | var $colorSpan = self.$colorList.find('> span.color').filter(function() { 116 | return $(this).data('color').toLowerCase() === color.toLowerCase(); 117 | }); 118 | 119 | if ($colorSpan.length > 0) { 120 | self.selectColorSpan($colorSpan); 121 | } else { 122 | console.error("The given color '" + color + "' could not be found"); 123 | } 124 | }, 125 | 126 | showPicker: function() { 127 | var pos = this.$icon.offset(); 128 | this.$picker.css({ 129 | // Remove some pixels to align the picker icon with the icons inside the dropdown 130 | left: pos.left - 6, 131 | top: pos.top + this.$icon.outerHeight() 132 | }); 133 | 134 | this.$picker.show(this.options.pickerDelay); 135 | }, 136 | 137 | hidePicker: function() { 138 | this.$picker.hide(this.options.pickerDelay); 139 | }, 140 | 141 | /** 142 | * Selects the given span inside $colorList. 143 | * 144 | * The given span becomes the selected one. 145 | * It also changes the HTML select value, this will emit the 'change' event. 146 | */ 147 | selectColorSpan: function($colorSpan) { 148 | var color = $colorSpan.data('color'); 149 | var title = $colorSpan.prop('title'); 150 | 151 | // Mark this span as the selected one 152 | $colorSpan.siblings().removeAttr('data-selected'); 153 | $colorSpan.attr('data-selected', ''); 154 | 155 | if (this.options.picker === true) { 156 | this.$icon.css('background-color', color); 157 | this.$icon.prop('title', title); 158 | this.hidePicker(); 159 | } 160 | 161 | // Change HTML select value 162 | this.$select.val(color); 163 | }, 164 | 165 | /** 166 | * The user clicked on a color inside $colorList. 167 | */ 168 | colorSpanClicked: function(e) { 169 | // When a color is clicked, make it the new selected one (unless disabled) 170 | if ($(e.target).is('[data-disabled]') === false) { 171 | this.selectColorSpan($(e.target)); 172 | this.$select.trigger('change'); 173 | } 174 | }, 175 | 176 | /** 177 | * Prevents the mousedown event from "eating" the click event. 178 | */ 179 | mousedown: function(e) { 180 | e.stopPropagation(); 181 | e.preventDefault(); 182 | }, 183 | 184 | destroy: function() { 185 | if (this.options.picker === true) { 186 | this.$icon.off('.' + this.type); 187 | this.$icon.remove(); 188 | $(document).off('.' + this.type); 189 | } 190 | 191 | this.$colorList.off('.' + this.type); 192 | this.$colorList.remove(); 193 | 194 | this.$select.removeData(this.type); 195 | this.$select.show(); 196 | } 197 | }; 198 | 199 | /** 200 | * Plugin definition. 201 | * How to use: $('#id').simplecolorpicker() 202 | */ 203 | $.fn.simplecolorpicker = function(option) { 204 | var args = $.makeArray(arguments); 205 | args.shift(); 206 | 207 | // For HTML element passed to the plugin 208 | return this.each(function() { 209 | var $this = $(this), 210 | data = $this.data('simplecolorpicker'), 211 | options = typeof option === 'object' && option; 212 | if (data === undefined) { 213 | $this.data('simplecolorpicker', (data = new SimpleColorPicker(this, options))); 214 | } 215 | if (typeof option === 'string') { 216 | data[option].apply(data, args); 217 | } 218 | }); 219 | }; 220 | 221 | /** 222 | * Default options. 223 | */ 224 | $.fn.simplecolorpicker.defaults = { 225 | // No theme by default 226 | theme: '', 227 | 228 | // Show the picker or make it inline 229 | picker: false, 230 | 231 | // Animation delay in milliseconds 232 | pickerDelay: 0 233 | }; 234 | 235 | })(jQuery); 236 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jquery-simplecolorpicker", 3 | "version": "0.3.1", 4 | "description": "Very simple jQuery color picker", 5 | "main": "jquery.simplecolorpicker.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "git://github.com/tkrotoff/jquery-simplecolorpicker" 9 | }, 10 | "keywords": [ 11 | "jQuery", 12 | "color", 13 | "picker", 14 | "colorpicker" 15 | ], 16 | "author": "Tanguy Krotoff", 17 | "license": "MIT", 18 | "bugs": { 19 | "url": "https://github.com/tkrotoff/jquery-simplecolorpicker/issues" 20 | }, 21 | "homepage": "https://github.com/tkrotoff/jquery-simplecolorpicker" 22 | } 23 | --------------------------------------------------------------------------------