| 1 | /* |
| 2 | * jQuery miniColors: A small color selector |
| 3 | * |
| 4 | * Copyright 2011 Cory LaViska for A Beautiful Site, LLC. (http://abeautifulsite.net/) |
| 5 | * |
| 6 | * Dual licensed under the MIT or GPL Version 2 licenses |
| 7 | * |
| 8 | */ |
| 9 | if(jQuery) (function($) { |
| 10 | |
| 11 | $.extend($.fn, { |
| 12 | |
| 13 | miniColors: function(o, data) { |
| 14 | |
| 15 | var create = function(input, o, data) { |
| 16 | // |
| 17 | // Creates a new instance of the miniColors selector |
| 18 | // |
| 19 | |
| 20 | // Determine initial color (defaults to white) |
| 21 | var color = expandHex(input.val()); |
| 22 | // CHANGED - if( !color ) color = 'ffffff'; |
| 23 | if( !color ) color = 'F1F1F1'; |
| 24 | var hsb = hex2hsb(color); |
| 25 | |
| 26 | // Create trigger |
| 27 | // CHANGED - var trigger = $('<a class="miniColors-trigger" style="background-color: #' + color + '" href="#"></a>'); |
| 28 | var trigger = (o.trigger) ? o.trigger : $('#pickcolor'); |
| 29 | // REMOVED - trigger.insertAfter(input); |
| 30 | |
| 31 | // Set input data and update attributes |
| 32 | input |
| 33 | .addClass('miniColors') |
| 34 | .data('original-maxlength', input.attr('maxlength') || null) |
| 35 | .data('original-autocomplete', input.attr('autocomplete') || null) |
| 36 | .data('letterCase', 'uppercase') |
| 37 | .data('trigger', trigger) |
| 38 | .data('hsb', hsb) |
| 39 | .data('change', o.change ? o.change : null) |
| 40 | .data('close', o.close ? o.close : null) |
| 41 | .data('open', o.open ? o.open : null) |
| 42 | .attr('maxlength', 7) |
| 43 | .attr('autocomplete', 'off') |
| 44 | .val('#' + convertCase(color, o.letterCase)); |
| 45 | |
| 46 | // Handle options |
| 47 | if( o.readonly ) input.prop('readonly', true); |
| 48 | if( o.disabled ) disable(input); |
| 49 | |
| 50 | // Show selector when trigger is clicked |
| 51 | trigger.bind('click.miniColors', function(event) { |
| 52 | event.preventDefault(); |
| 53 | if( input.val() === '' ) input.val('#'); |
| 54 | show(input); |
| 55 | |
| 56 | }); |
| 57 | |
| 58 | // Show selector when input receives focus |
| 59 | input.bind('focus.miniColors', function(event) { |
| 60 | if( input.val() === '' ) input.val('#'); |
| 61 | show(input); |
| 62 | }); |
| 63 | |
| 64 | // Hide on blur |
| 65 | input.bind('blur.miniColors', function(event) { |
| 66 | var hex = expandHex( hsb2hex(input.data('hsb')) ); |
| 67 | input.val( hex ? '#' + convertCase(hex, input.data('letterCase')) : '' ); |
| 68 | }); |
| 69 | |
| 70 | // Hide when tabbing out of the input |
| 71 | input.bind('keydown.miniColors', function(event) { |
| 72 | if( event.keyCode === 9 ) hide(input); |
| 73 | }); |
| 74 | |
| 75 | // Update when color is typed in |
| 76 | input.bind('keyup.miniColors', function(event) { |
| 77 | setColorFromInput(input); |
| 78 | }); |
| 79 | |
| 80 | // Handle pasting |
| 81 | input.bind('paste.miniColors', function(event) { |
| 82 | // Short pause to wait for paste to complete |
| 83 | setTimeout( function() { |
| 84 | setColorFromInput(input); |
| 85 | }, 5); |
| 86 | }); |
| 87 | |
| 88 | }; |
| 89 | |
| 90 | var destroy = function(input) { |
| 91 | // |
| 92 | // Destroys an active instance of the miniColors selector |
| 93 | // |
| 94 | |
| 95 | hide(); |
| 96 | input = $(input); |
| 97 | |
| 98 | // Restore to original state |
| 99 | input.data('trigger').remove(); |
| 100 | input |
| 101 | .attr('autocomplete', input.data('original-autocomplete')) |
| 102 | .attr('maxlength', input.data('original-maxlength')) |
| 103 | .removeData() |
| 104 | .removeClass('miniColors') |
| 105 | .unbind('.miniColors'); |
| 106 | $(document).unbind('.miniColors'); |
| 107 | }; |
| 108 | |
| 109 | var enable = function(input) { |
| 110 | // |
| 111 | // Enables the input control and the selector |
| 112 | // |
| 113 | input |
| 114 | .prop('disabled', false) |
| 115 | .data('trigger') |
| 116 | .css('opacity', 1); |
| 117 | }; |
| 118 | |
| 119 | var disable = function(input) { |
| 120 | // |
| 121 | // Disables the input control and the selector |
| 122 | // |
| 123 | hide(input); |
| 124 | input |
| 125 | .prop('disabled', true) |
| 126 | .data('trigger') |
| 127 | .css('opacity', 0.5); |
| 128 | }; |
| 129 | |
| 130 | var show = function(input) { |
| 131 | // |
| 132 | // Shows the miniColors selector |
| 133 | // |
| 134 | if( input.prop('disabled') ) return false; |
| 135 | |
| 136 | // Hide all other instances |
| 137 | hide(); |
| 138 | |
| 139 | // Generate the selector |
| 140 | var selector = $('<div class="miniColors-selector"></div>'); |
| 141 | selector |
| 142 | .append('<div class="miniColors-colors" style="background-color: #FFF;"><div class="miniColors-colorPicker"><div class="miniColors-colorPicker-inner"></div></div>') |
| 143 | .append('<div class="miniColors-hues"><div class="miniColors-huePicker"></div></div>') |
| 144 | .css({ |
| 145 | top: input.is(':visible') ? input.offset().top + input.outerHeight() : input.data('trigger').offset().top + input.data('trigger').outerHeight(), |
| 146 | left: input.is(':visible') ? input.offset().left : input.data('trigger').offset().left, |
| 147 | display: 'none' |
| 148 | }) |
| 149 | .addClass( input.attr('class') ); |
| 150 | |
| 151 | // Set background for colors |
| 152 | var hsb = input.data('hsb'); |
| 153 | selector |
| 154 | .find('.miniColors-colors') |
| 155 | .css('backgroundColor', '#' + hsb2hex({ h: hsb.h, s: 100, b: 100 })); |
| 156 | |
| 157 | // Set colorPicker position |
| 158 | var colorPosition = input.data('colorPosition'); |
| 159 | if( !colorPosition ) colorPosition = getColorPositionFromHSB(hsb); |
| 160 | selector.find('.miniColors-colorPicker') |
| 161 | .css('top', colorPosition.y + 'px') |
| 162 | .css('left', colorPosition.x + 'px'); |
| 163 | |
| 164 | // Set huePicker position |
| 165 | var huePosition = input.data('huePosition'); |
| 166 | if( !huePosition ) huePosition = getHuePositionFromHSB(hsb); |
| 167 | selector.find('.miniColors-huePicker').css('top', huePosition.y + 'px'); |
| 168 | |
| 169 | // Set input data |
| 170 | input |
| 171 | .data('selector', selector) |
| 172 | .data('huePicker', selector.find('.miniColors-huePicker')) |
| 173 | .data('colorPicker', selector.find('.miniColors-colorPicker')) |
| 174 | .data('mousebutton', 0); |
| 175 | |
| 176 | $('BODY').append(selector); |
| 177 | selector.fadeIn(100); |
| 178 | |
| 179 | // Prevent text selection in IE |
| 180 | selector.bind('selectstart', function() { return false; }); |
| 181 | |
| 182 | $(document).bind('mousedown.miniColors touchstart.miniColors', function(event) { |
| 183 | |
| 184 | input.data('mousebutton', 1); |
| 185 | var testSubject = $(event.target).parents().andSelf(); |
| 186 | |
| 187 | if( testSubject.hasClass('miniColors-colors') ) { |
| 188 | event.preventDefault(); |
| 189 | input.data('moving', 'colors'); |
| 190 | moveColor(input, event); |
| 191 | } |
| 192 | |
| 193 | if( testSubject.hasClass('miniColors-hues') ) { |
| 194 | event.preventDefault(); |
| 195 | input.data('moving', 'hues'); |
| 196 | moveHue(input, event); |
| 197 | } |
| 198 | |
| 199 | if( testSubject.hasClass('miniColors-selector') ) { |
| 200 | event.preventDefault(); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | if( testSubject.hasClass('miniColors') ) return; |
| 205 | |
| 206 | hide(input); |
| 207 | }); |
| 208 | |
| 209 | $(document) |
| 210 | .bind('mouseup.miniColors touchend.miniColors', function(event) { |
| 211 | event.preventDefault(); |
| 212 | input.data('mousebutton', 0).removeData('moving'); |
| 213 | }) |
| 214 | .bind('mousemove.miniColors touchmove.miniColors', function(event) { |
| 215 | event.preventDefault(); |
| 216 | if( input.data('mousebutton') === 1 ) { |
| 217 | if( input.data('moving') === 'colors' ) moveColor(input, event); |
| 218 | if( input.data('moving') === 'hues' ) moveHue(input, event); |
| 219 | } |
| 220 | }); |
| 221 | |
| 222 | // Fire open callback |
| 223 | if( input.data('open') ) { |
| 224 | input.data('open').call(input.get(0), '#' + hsb2hex(hsb), hsb2rgb(hsb)); |
| 225 | } |
| 226 | |
| 227 | }; |
| 228 | |
| 229 | var hide = function(input) { |
| 230 | |
| 231 | // |
| 232 | // Hides one or more miniColors selectors |
| 233 | // |
| 234 | |
| 235 | // Hide all other instances if input isn't specified |
| 236 | if( !input ) input = '.miniColors'; |
| 237 | |
| 238 | $(input).each( function() { |
| 239 | var selector = $(this).data('selector'); |
| 240 | $(this).removeData('selector'); |
| 241 | $(selector).fadeOut(100, function() { |
| 242 | // Fire close callback |
| 243 | if( input.data('close') ) { |
| 244 | var hsb = input.data('hsb'), hex = hsb2hex(hsb); |
| 245 | input.data('close').call(input.get(0), '#' + hex, hsb2rgb(hsb)); |
| 246 | } |
| 247 | $(this).remove(); |
| 248 | }); |
| 249 | }); |
| 250 | |
| 251 | $(document).unbind('.miniColors'); |
| 252 | |
| 253 | }; |
| 254 | |
| 255 | var moveColor = function(input, event) { |
| 256 | |
| 257 | var colorPicker = input.data('colorPicker'); |
| 258 | |
| 259 | colorPicker.hide(); |
| 260 | |
| 261 | var position = { |
| 262 | x: event.pageX, |
| 263 | y: event.pageY |
| 264 | }; |
| 265 | |
| 266 | // Touch support |
| 267 | if( event.originalEvent.changedTouches ) { |
| 268 | position.x = event.originalEvent.changedTouches[0].pageX; |
| 269 | position.y = event.originalEvent.changedTouches[0].pageY; |
| 270 | } |
| 271 | position.x = position.x - input.data('selector').find('.miniColors-colors').offset().left - 5; |
| 272 | position.y = position.y - input.data('selector').find('.miniColors-colors').offset().top - 5; |
| 273 | if( position.x <= -5 ) position.x = -5; |
| 274 | if( position.x >= 144 ) position.x = 144; |
| 275 | if( position.y <= -5 ) position.y = -5; |
| 276 | if( position.y >= 144 ) position.y = 144; |
| 277 | |
| 278 | input.data('colorPosition', position); |
| 279 | colorPicker.css('left', position.x).css('top', position.y).show(); |
| 280 | |
| 281 | // Calculate saturation |
| 282 | var s = Math.round((position.x + 5) * 0.67); |
| 283 | if( s < 0 ) s = 0; |
| 284 | if( s > 100 ) s = 100; |
| 285 | |
| 286 | // Calculate brightness |
| 287 | var b = 100 - Math.round((position.y + 5) * 0.67); |
| 288 | if( b < 0 ) b = 0; |
| 289 | if( b > 100 ) b = 100; |
| 290 | |
| 291 | // Update HSB values |
| 292 | var hsb = input.data('hsb'); |
| 293 | hsb.s = s; |
| 294 | hsb.b = b; |
| 295 | |
| 296 | // Set color |
| 297 | setColor(input, hsb, true); |
| 298 | }; |
| 299 | |
| 300 | var moveHue = function(input, event) { |
| 301 | |
| 302 | var huePicker = input.data('huePicker'); |
| 303 | |
| 304 | huePicker.hide(); |
| 305 | |
| 306 | var position = { |
| 307 | y: event.pageY |
| 308 | }; |
| 309 | |
| 310 | // Touch support |
| 311 | if( event.originalEvent.changedTouches ) { |
| 312 | position.y = event.originalEvent.changedTouches[0].pageY; |
| 313 | } |
| 314 | |
| 315 | position.y = position.y - input.data('selector').find('.miniColors-colors').offset().top - 1; |
| 316 | if( position.y <= -1 ) position.y = -1; |
| 317 | if( position.y >= 149 ) position.y = 149; |
| 318 | input.data('huePosition', position); |
| 319 | huePicker.css('top', position.y).show(); |
| 320 | |
| 321 | // Calculate hue |
| 322 | var h = Math.round((150 - position.y - 1) * 2.4); |
| 323 | if( h < 0 ) h = 0; |
| 324 | if( h > 360 ) h = 360; |
| 325 | |
| 326 | // Update HSB values |
| 327 | var hsb = input.data('hsb'); |
| 328 | hsb.h = h; |
| 329 | |
| 330 | // Set color |
| 331 | setColor(input, hsb, true); |
| 332 | |
| 333 | }; |
| 334 | |
| 335 | var setColor = function(input, hsb, updateInput) { |
| 336 | input.data('hsb', hsb); |
| 337 | var hex = hsb2hex(hsb); |
| 338 | if( updateInput ) input.val( '#' + convertCase(hex, input.data('letterCase')) ); |
| 339 | //REMOVED - input.data('trigger').css('backgroundColor', '#' + hex); |
| 340 | if( input.data('selector') ) input.data('selector').find('.miniColors-colors').css('backgroundColor', '#' + hsb2hex({ h: hsb.h, s: 100, b: 100 })); |
| 341 | |
| 342 | // Fire change callback |
| 343 | if( input.data('change') ) { |
| 344 | if( hex === input.data('lastChange') ) return; |
| 345 | input.data('change').call(input.get(0), '#' + hex, hsb2rgb(hsb)); |
| 346 | input.data('lastChange', hex); |
| 347 | } |
| 348 | |
| 349 | }; |
| 350 | |
| 351 | var setColorFromInput = function(input) { |
| 352 | |
| 353 | input.val('#' + cleanHex(input.val())); |
| 354 | var hex = expandHex(input.val()); |
| 355 | if( !hex ) return false; |
| 356 | |
| 357 | // Get HSB equivalent |
| 358 | var hsb = hex2hsb(hex); |
| 359 | |
| 360 | // If color is the same, no change required |
| 361 | var currentHSB = input.data('hsb'); |
| 362 | if( hsb.h === currentHSB.h && hsb.s === currentHSB.s && hsb.b === currentHSB.b ) return true; |
| 363 | |
| 364 | // Set colorPicker position |
| 365 | var colorPosition = getColorPositionFromHSB(hsb); |
| 366 | var colorPicker = $(input.data('colorPicker')); |
| 367 | colorPicker.css('top', colorPosition.y + 'px').css('left', colorPosition.x + 'px'); |
| 368 | input.data('colorPosition', colorPosition); |
| 369 | |
| 370 | // Set huePosition position |
| 371 | var huePosition = getHuePositionFromHSB(hsb); |
| 372 | var huePicker = $(input.data('huePicker')); |
| 373 | huePicker.css('top', huePosition.y + 'px'); |
| 374 | input.data('huePosition', huePosition); |
| 375 | |
| 376 | setColor(input, hsb); |
| 377 | |
| 378 | return true; |
| 379 | |
| 380 | }; |
| 381 | |
| 382 | var convertCase = function(string, letterCase) { |
| 383 | if( letterCase === 'lowercase' ) return string.toLowerCase(); |
| 384 | if( letterCase === 'uppercase' ) return string.toUpperCase(); |
| 385 | return string; |
| 386 | }; |
| 387 | |
| 388 | var getColorPositionFromHSB = function(hsb) { |
| 389 | var x = Math.ceil(hsb.s / 0.67); |
| 390 | if( x < 0 ) x = 0; |
| 391 | if( x > 150 ) x = 150; |
| 392 | var y = 150 - Math.ceil(hsb.b / 0.67); |
| 393 | if( y < 0 ) y = 0; |
| 394 | if( y > 150 ) y = 150; |
| 395 | return { x: x - 5, y: y - 5 }; |
| 396 | }; |
| 397 | |
| 398 | var getHuePositionFromHSB = function(hsb) { |
| 399 | var y = 150 - (hsb.h / 2.4); |
| 400 | if( y < 0 ) h = 0; |
| 401 | if( y > 150 ) h = 150; |
| 402 | return { y: y - 1 }; |
| 403 | }; |
| 404 | |
| 405 | var cleanHex = function(hex) { |
| 406 | return hex.replace(/[^A-F0-9]/ig, ''); |
| 407 | }; |
| 408 | |
| 409 | var expandHex = function(hex) { |
| 410 | hex = cleanHex(hex); |
| 411 | if( !hex ) return null; |
| 412 | if( hex.length === 3 ) hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2]; |
| 413 | return hex.length === 6 ? hex : null; |
| 414 | }; |
| 415 | |
| 416 | var hsb2rgb = function(hsb) { |
| 417 | var rgb = {}; |
| 418 | var h = Math.round(hsb.h); |
| 419 | var s = Math.round(hsb.s*255/100); |
| 420 | var v = Math.round(hsb.b*255/100); |
| 421 | if(s === 0) { |
| 422 | rgb.r = rgb.g = rgb.b = v; |
| 423 | } else { |
| 424 | var t1 = v; |
| 425 | var t2 = (255 - s) * v / 255; |
| 426 | var t3 = (t1 - t2) * (h % 60) / 60; |
| 427 | if( h === 360 ) h = 0; |
| 428 | if( h < 60 ) { rgb.r = t1; rgb.b = t2; rgb.g = t2 + t3; } |
| 429 | else if( h < 120 ) {rgb.g = t1; rgb.b = t2; rgb.r = t1 - t3; } |
| 430 | else if( h < 180 ) {rgb.g = t1; rgb.r = t2; rgb.b = t2 + t3; } |
| 431 | else if( h < 240 ) {rgb.b = t1; rgb.r = t2; rgb.g = t1 - t3; } |
| 432 | else if( h < 300 ) {rgb.b = t1; rgb.g = t2; rgb.r = t2 + t3; } |
| 433 | else if( h < 360 ) {rgb.r = t1; rgb.g = t2; rgb.b = t1 - t3; } |
| 434 | else { rgb.r = 0; rgb.g = 0; rgb.b = 0; } |
| 435 | } |
| 436 | return { |
| 437 | r: Math.round(rgb.r), |
| 438 | g: Math.round(rgb.g), |
| 439 | b: Math.round(rgb.b) |
| 440 | }; |
| 441 | }; |
| 442 | |
| 443 | var rgb2hex = function(rgb) { |
| 444 | var hex = [ |
| 445 | rgb.r.toString(16), |
| 446 | rgb.g.toString(16), |
| 447 | rgb.b.toString(16) |
| 448 | ]; |
| 449 | $.each(hex, function(nr, val) { |
| 450 | if (val.length === 1) hex[nr] = '0' + val; |
| 451 | }); |
| 452 | return hex.join(''); |
| 453 | }; |
| 454 | |
| 455 | var hex2rgb = function(hex) { |
| 456 | hex = parseInt(((hex.indexOf('#') > -1) ? hex.substring(1) : hex), 16); |
| 457 | |
| 458 | return { |
| 459 | r: hex >> 16, |
| 460 | g: (hex & 0x00FF00) >> 8, |
| 461 | b: (hex & 0x0000FF) |
| 462 | }; |
| 463 | }; |
| 464 | |
| 465 | var rgb2hsb = function(rgb) { |
| 466 | var hsb = { h: 0, s: 0, b: 0 }; |
| 467 | var min = Math.min(rgb.r, rgb.g, rgb.b); |
| 468 | var max = Math.max(rgb.r, rgb.g, rgb.b); |
| 469 | var delta = max - min; |
| 470 | hsb.b = max; |
| 471 | hsb.s = max !== 0 ? 255 * delta / max : 0; |
| 472 | if( hsb.s !== 0 ) { |
| 473 | if( rgb.r === max ) { |
| 474 | hsb.h = (rgb.g - rgb.b) / delta; |
| 475 | } else if( rgb.g === max ) { |
| 476 | hsb.h = 2 + (rgb.b - rgb.r) / delta; |
| 477 | } else { |
| 478 | hsb.h = 4 + (rgb.r - rgb.g) / delta; |
| 479 | } |
| 480 | } else { |
| 481 | hsb.h = -1; |
| 482 | } |
| 483 | hsb.h *= 60; |
| 484 | if( hsb.h < 0 ) { |
| 485 | hsb.h += 360; |
| 486 | } |
| 487 | hsb.s *= 100/255; |
| 488 | hsb.b *= 100/255; |
| 489 | return hsb; |
| 490 | }; |
| 491 | |
| 492 | var hex2hsb = function(hex) { |
| 493 | var hsb = rgb2hsb(hex2rgb(hex)); |
| 494 | // Zero out hue marker for black, white, and grays (saturation === 0) |
| 495 | if( hsb.s === 0 ) hsb.h = 360; |
| 496 | return hsb; |
| 497 | }; |
| 498 | |
| 499 | var hsb2hex = function(hsb) { |
| 500 | return rgb2hex(hsb2rgb(hsb)); |
| 501 | }; |
| 502 | |
| 503 | |
| 504 | // Handle calls to $([selector]).miniColors() |
| 505 | switch(o) { |
| 506 | |
| 507 | case 'readonly': |
| 508 | |
| 509 | $(this).each( function() { |
| 510 | if( !$(this).hasClass('miniColors') ) return; |
| 511 | $(this).prop('readonly', data); |
| 512 | }); |
| 513 | |
| 514 | return $(this); |
| 515 | |
| 516 | case 'disabled': |
| 517 | |
| 518 | $(this).each( function() { |
| 519 | if( !$(this).hasClass('miniColors') ) return; |
| 520 | if( data ) { |
| 521 | disable($(this)); |
| 522 | } else { |
| 523 | enable($(this)); |
| 524 | } |
| 525 | }); |
| 526 | |
| 527 | return $(this); |
| 528 | |
| 529 | case 'value': |
| 530 | |
| 531 | // Getter |
| 532 | if( data === undefined ) { |
| 533 | if( !$(this).hasClass('miniColors') ) return; |
| 534 | var input = $(this), |
| 535 | hex = expandHex(input.val()); |
| 536 | return hex ? '#' + convertCase(hex, input.data('letterCase')) : null; |
| 537 | } |
| 538 | |
| 539 | // Setter |
| 540 | $(this).each( function() { |
| 541 | if( !$(this).hasClass('miniColors') ) return; |
| 542 | $(this).val(data); |
| 543 | setColorFromInput($(this)); |
| 544 | }); |
| 545 | |
| 546 | return $(this); |
| 547 | |
| 548 | case 'destroy': |
| 549 | |
| 550 | $(this).each( function() { |
| 551 | if( !$(this).hasClass('miniColors') ) return; |
| 552 | destroy($(this)); |
| 553 | }); |
| 554 | |
| 555 | return $(this); |
| 556 | |
| 557 | default: |
| 558 | |
| 559 | if( !o ) o = {}; |
| 560 | |
| 561 | $(this).each( function() { |
| 562 | |
| 563 | // Must be called on an input element |
| 564 | if( $(this)[0].tagName.toLowerCase() !== 'input' ) return; |
| 565 | |
| 566 | // If a trigger is present, the control was already created |
| 567 | if( $(this).data('trigger') ) return; |
| 568 | |
| 569 | // Create the control |
| 570 | create($(this), o, data); |
| 571 | |
| 572 | }); |
| 573 | |
| 574 | return $(this); |
| 575 | |
| 576 | } |
| 577 | |
| 578 | } |
| 579 | |
| 580 | }); |
| 581 | |
| 582 | })(jQuery); |
| 583 | No newline at end of file |