Advertisement
Guest User

Untitled

a guest
Dec 28th, 2012
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * jQuery miniColors: A small color selector
  3.  *
  4.  * Copyright 2012 Cory LaViska for A Beautiful Site, LLC. (http://www.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 prefix = !o || typeof o.prefix === 'undefined' ? '#' : o.prefix ;
  16.            
  17.             function create(input, o, data) {
  18.                
  19.                 // Determine initial color (defaults to white)
  20.                 var color = expandHex(input.val()) || 'ffffff',
  21.                     hsb = hex2hsb(color),
  22.                     rgb = hsb2rgb(hsb),
  23.                     alpha = parseFloat(input.attr('data-opacity')).toFixed(2),
  24.                     opacity = false,
  25.                     trigger;
  26.                
  27.                 if( alpha > 1 ) alpha = 1;
  28.                 if( alpha < 0 ) alpha = 0;
  29.                
  30.                 // Handle opacity
  31.                 if( input.attr('data-opacity') !== undefined || o.opacity === true ) {
  32.                     opacity = true;
  33.                     alpha = input.attr('data-opacity');
  34.                     if( alpha === '' ) {
  35.                         alpha = 1;
  36.                     } else {
  37.                         alpha = parseFloat(alpha).toFixed(2);
  38.                     }
  39.                     if( alpha > 1 ) alpha = 1;
  40.                     if( alpha < 0 ) alpha = 0;
  41.                     input.attr('data-opacity', alpha);
  42.                 }              
  43.                
  44.                 // Create trigger
  45.                 trigger = $('<a class="miniColors-trigger" style="background-color: #' + color + '" href="#"></a>');
  46.                 trigger.insertAfter(input);
  47.                 trigger.wrap('<span class="miniColors-triggerWrap"></span>');
  48.                 if( o.opacity ) {
  49.                     trigger.css('backgroundColor', 'rgba(' + rgb.r + ', ' + rgb.g + ', ' + rgb.b + ', ' + alpha + ')');
  50.                 }
  51.                
  52.                 // Set input data and update attributes
  53.                 input
  54.                     .addClass('miniColors')
  55.                     .data('original-maxlength', input.attr('maxlength') || null)
  56.                     .data('original-autocomplete', input.attr('autocomplete') || null)
  57.                     .data('letterCase', o.letterCase === 'uppercase' ? 'uppercase' : 'lowercase')
  58.                     .data('opacity', opacity)
  59.                     .data('alpha', alpha)
  60.                     .data('trigger', trigger)
  61.                     .data('hsb', hsb)
  62.                     .data('change', o.change ? o.change : null)
  63.                     .data('submit', o.submit ? o.submit : null)
  64.                     .data('close', o.close ? o.close : null)
  65.                     .data('open', o.open ? o.open : null)
  66.                     .attr('maxlength', 7)
  67.                     .attr('autocomplete', 'off')
  68.                     .val(prefix + convertCase(color, o.letterCase));
  69.                
  70.                 // Handle options
  71.                 if( o.readonly || input.prop('readonly') ) input.prop('readonly', true);
  72.                 if( o.disabled || input.prop('disabled') ) disable(input);
  73.                
  74.                 // Show selector when trigger is clicked
  75.                 trigger.on('click.miniColors', function(event) {
  76.                     event.preventDefault();
  77.                     if( input.val() === '' ) input.val(prefix);
  78.                     show(input);
  79.  
  80.                 });
  81.                
  82.                 // Show selector when input receives focus
  83.                 input.on('focus.miniColors', function(event) {
  84.                     if( input.val() === '' ) input.val(prefix);
  85.                     show(input);
  86.                 });
  87.                
  88.                 // Hide on blur
  89.                 input.on('blur.miniColors', function(event) {
  90.                     var hex = expandHex(input.val());
  91.                     input.val( hex ? prefix + convertCase(hex, input.data('letterCase')) : '#' + hsb2hex(input.data('hsb')) );
  92.                 });
  93.                
  94.                 // Hide when tabbing out of the input
  95.                 input.on('keydown.miniColors', function(event) {
  96.                     if( event.keyCode === 9 || event.keyCode === 27 ) hide(input);
  97.                 });
  98.                
  99.                 // Update when color is typed in
  100.                 input.on('keyup.miniColors', function(event) {
  101.                     setColorFromInput(input);
  102.                 });
  103.                
  104.                 // Handle pasting
  105.                 input.on('paste.miniColors', function(event) {
  106.                     // Short pause to wait for paste to complete
  107.                     setTimeout( function() {
  108.                         setColorFromInput(input);
  109.                     }, 5);
  110.                 });
  111.                
  112.             }
  113.            
  114.             function destroy(input) {
  115.                 hide();
  116.                 input = $(input);
  117.                
  118.                 // Restore to original state
  119.                 input.data('trigger').parent().remove();
  120.                 input
  121.                     .attr('autocomplete', input.data('original-autocomplete'))
  122.                     .attr('maxlength', input.data('original-maxlength'))
  123.                     .removeData()
  124.                     .removeClass('miniColors')
  125.                     .off('.miniColors');
  126.                 $(document).off('.miniColors');
  127.             }
  128.            
  129.             function enable(input) {
  130.                 input
  131.                     .prop('disabled', false)
  132.                     .data('trigger').parent().removeClass('disabled');
  133.             }
  134.            
  135.             function disable(input) {
  136.                 hide(input);
  137.                 input
  138.                     .prop('disabled', true)
  139.                     .data('trigger').parent().addClass('disabled');
  140.             }
  141.            
  142.             function show(input) {
  143.                
  144.                 var hex,
  145.                     colorPosition,
  146.                     huePosition,
  147.                     opacityPosition,
  148.                     hidden,
  149.                     top,
  150.                     left,
  151.                     trigger,
  152.                     triggerWidth,
  153.                     triggerHeight,
  154.                     selector,
  155.                     selectorWidth,
  156.                     selectorHeight,
  157.                     windowHeight,
  158.                     windowWidth,
  159.                     scrollTop,
  160.                     scrollLeft;            
  161.                
  162.                 if( input.prop('disabled') ) return false;
  163.                
  164.                 // Hide all other instances
  165.                 hide();            
  166.                
  167.                 // Generate the selector
  168.                 selector = $('<div class="miniColors-selector"></div>')
  169.                     .append('<div class="miniColors-hues"><div class="miniColors-huePicker"></div></div>')
  170.                     .append('<div class="miniColors-colors" style="background-color: #FFF;"><div class="miniColors-colorPicker"><div class="miniColors-colorPicker-inner"></div></div>')
  171.                     .css('display', 'none')
  172.                     .addClass( input.attr('class') );
  173.                
  174.                 // Opacity
  175.                 if( input.data('opacity') ) {
  176.                     selector
  177.                         .addClass('opacity')
  178.                         .prepend('<div class="miniColors-opacity"><div class="miniColors-opacityPicker"></div></div>');
  179.                 }
  180.                
  181.                 // Set background for colors
  182.                 hsb = input.data('hsb');
  183.                 selector
  184.                     .find('.miniColors-colors').css('backgroundColor', '#' + hsb2hex({ h: hsb.h, s: 100, b: 100 })).end()
  185.                     .find('.miniColors-opacity').css('backgroundColor', '#' + hsb2hex({ h: hsb.h, s: hsb.s, b: hsb.b })).end();
  186.                
  187.                 // Set colorPicker position
  188.                 colorPosition = input.data('colorPosition');
  189.                 if( !colorPosition ) colorPosition = getColorPositionFromHSB(hsb);
  190.                 selector.find('.miniColors-colorPicker')
  191.                     .css('top', colorPosition.y + 'px')
  192.                     .css('left', colorPosition.x + 'px');
  193.                
  194.                 // Set huePicker position
  195.                 huePosition = input.data('huePosition');
  196.                 if( !huePosition ) huePosition = getHuePositionFromHSB(hsb);
  197.                 selector.find('.miniColors-huePicker').css('top', huePosition + 'px');
  198.                
  199.                 // Set opacity position
  200.                 opacityPosition = input.data('opacityPosition');
  201.                 if( !opacityPosition ) opacityPosition = getOpacityPositionFromAlpha(input.attr('data-opacity'));
  202.                 selector.find('.miniColors-opacityPicker').css('top', opacityPosition + 'px');
  203.                
  204.                 // Set input data
  205.                 input
  206.                     .data('selector', selector)
  207.                     .data('huePicker', selector.find('.miniColors-huePicker'))
  208.                     .data('opacityPicker', selector.find('.miniColors-opacityPicker'))
  209.                     .data('colorPicker', selector.find('.miniColors-colorPicker'))
  210.                     .data('mousebutton', 0);
  211.                
  212.                 $('BODY').append(selector);
  213.                
  214.                 // Position the selector
  215.                 trigger = input.data('trigger');
  216.                 hidden = !input.is(':visible');
  217.                 top = hidden ? trigger.offset().top + trigger.outerHeight() : input.offset().top + input.outerHeight();
  218.                 left = hidden ? trigger.offset().left : input.offset().left;
  219.                 selectorWidth = selector.outerWidth();
  220.                 selectorHeight = selector.outerHeight();
  221.                 triggerWidth = trigger.outerWidth();
  222.                 triggerHeight = trigger.outerHeight();
  223.                 windowHeight = $(window).height();
  224.                 windowWidth = $(window).width();
  225.                 scrollTop = $(window).scrollTop();
  226.                 scrollLeft = $(window).scrollLeft();
  227.                
  228.                 // Adjust based on viewport
  229.                 if( (top + selectorHeight) > windowHeight + scrollTop ) top = top - selectorHeight - triggerHeight;
  230.                 if( (left + selectorWidth) > windowWidth + scrollLeft ) left = left - selectorWidth + triggerWidth;
  231.                
  232.                 // Set position and show
  233.                 selector.css({
  234.                     top: top,
  235.                     left: left
  236.                 }).fadeIn(100);
  237.                
  238.                 // Prevent text selection in IE
  239.                 selector.on('selectstart', function() { return false; });
  240.                
  241.                 // Hide on resize (IE7/8 trigger this when any element is resized...)
  242.                 if( !$.browser.msie || ($.browser.msie && $.browser.version >= 9) ) {
  243.                     $(window).on('resize.miniColors', function(event) {
  244.                         hide(input);
  245.                     });
  246.                 }
  247.                
  248.                 $(document)
  249.                     .on('mousedown.miniColors touchstart.miniColors', function(event) {
  250.                        
  251.                         var testSubject = $(event.target).parents().andSelf();
  252.                        
  253.                         input.data('mousebutton', 1);
  254.                        
  255.                         if( testSubject.hasClass('miniColors-colors') ) {
  256.                             event.preventDefault();
  257.                             input.data('moving', 'colors');
  258.                             moveColor(input, event);
  259.                         }
  260.                        
  261.                         if( testSubject.hasClass('miniColors-hues') ) {
  262.                             event.preventDefault();
  263.                             input.data('moving', 'hues');
  264.                             moveHue(input, event);
  265.                         }
  266.                        
  267.                         if( testSubject.hasClass('miniColors-opacity') ) {
  268.                             event.preventDefault();
  269.                             input.data('moving', 'opacity');
  270.                             moveOpacity(input, event);
  271.                         }
  272.                        
  273.                         if( testSubject.hasClass('miniColors-selector') ) {
  274.                             event.preventDefault();
  275.                             return;
  276.                         }
  277.                        
  278.                         if( testSubject.hasClass('miniColors') ) return;
  279.                        
  280.                         hide(input);
  281.                        
  282.                     })
  283.                     .on('mouseup.miniColors touchend.miniColors', function(event) {
  284.                         event.preventDefault();
  285.                         input.data('mousebutton', 0).removeData('moving');
  286.                         if( input.data('submit') ) {
  287.                             var hsb = input.data('hsb'),
  288.                                 hex = hsb2hex(hsb);
  289.                             input.data('submit').call(input.get(0), '#' + hex, $.extend(hsb2rgb(hsb), { a: parseFloat(input.attr('data-opacity')) }));
  290.                         }
  291.                     })
  292.                     .on('mousemove.miniColors touchmove.miniColors', function(event) {
  293.                         event.preventDefault();
  294.                         if( input.data('mousebutton') === 1 ) {
  295.                             if( input.data('moving') === 'colors' ) moveColor(input, event);
  296.                             if( input.data('moving') === 'hues' ) moveHue(input, event);
  297.                             if( input.data('moving') === 'opacity' ) moveOpacity(input, event);
  298.                         }
  299.                     });
  300.                
  301.                 // Fire open callback
  302.                 if( input.data('open') ) {                 
  303.                     setInterval(function(){ SafeSubmit = true; }, 1000);
  304.                     input.data('open').call(input.get(0), '#' + hsb2hex(hsb), $.extend(hsb2rgb(hsb), { a: parseFloat(input.attr('data-opacity')) }));
  305.                 }
  306.                
  307.             }
  308.            
  309.             function hide(input) {
  310.                
  311.                 // Hide all other instances if input isn't specified
  312.                 if( !input ) input = $('.miniColors');
  313.                
  314.                 input.each( function() {
  315.                     var selector = $(this).data('selector');
  316.                     $(this).removeData('selector');
  317.                     $(selector).fadeOut(100, function() {
  318.                         // Fire close callback
  319.                         if( input.data('close') ) {
  320.                             var hsb = input.data('hsb'),
  321.                                 hex = hsb2hex(hsb);
  322.                             input.data('close').call(input.get(0), '#' + hex, $.extend(hsb2rgb(hsb), { a: parseFloat(input.attr('data-opacity')) }));
  323.                         }
  324.                         $(this).remove();
  325.                     });
  326.                 });
  327.                
  328.                 $(document).off('.miniColors');
  329.                
  330.             }
  331.            
  332.             function moveColor(input, event) {
  333.  
  334.                 var colorPicker = input.data('colorPicker'),
  335.                     position, s, b, hsb;
  336.                
  337.                 colorPicker.hide();
  338.                
  339.                 position = {
  340.                     x: event.pageX,
  341.                     y: event.pageY
  342.                 };
  343.                
  344.                 // Touch support
  345.                 if( event.originalEvent.changedTouches ) {
  346.                     position.x = event.originalEvent.changedTouches[0].pageX;
  347.                     position.y = event.originalEvent.changedTouches[0].pageY;
  348.                 }
  349.                 position.x = position.x - input.data('selector').find('.miniColors-colors').offset().left - 6;
  350.                 position.y = position.y - input.data('selector').find('.miniColors-colors').offset().top - 6;
  351.                 if( position.x <= -5 ) position.x = -5;
  352.                 if( position.x >= 144 ) position.x = 144;
  353.                 if( position.y <= -5 ) position.y = -5;
  354.                 if( position.y >= 144 ) position.y = 144;
  355.                
  356.                 input.data('colorPosition', position);
  357.                 colorPicker.css('left', position.x).css('top', position.y).show();
  358.                
  359.                 // Calculate saturation
  360.                 s = Math.round((position.x + 5) * 0.67);
  361.                 if( s < 0 ) s = 0;
  362.                 if( s > 100 ) s = 100;
  363.                
  364.                 // Calculate brightness
  365.                 b = 100 - Math.round((position.y + 5) * 0.67);
  366.                 if( b < 0 ) b = 0;
  367.                 if( b > 100 ) b = 100;
  368.                
  369.                 // Update HSB values
  370.                 hsb = input.data('hsb');
  371.                 hsb.s = s;
  372.                 hsb.b = b;
  373.                
  374.                 // Set color
  375.                 setColor(input, hsb, true);
  376.             }
  377.            
  378.             function moveHue(input, event) {
  379.                
  380.                 var huePicker = input.data('huePicker'),
  381.                     position = event.pageY,
  382.                     h, hsb;
  383.                
  384.                 huePicker.hide();
  385.                
  386.                 // Touch support
  387.                 if( event.originalEvent.changedTouches ) {
  388.                     position = event.originalEvent.changedTouches[0].pageY;
  389.                 }
  390.                
  391.                 position = position - input.data('selector').find('.miniColors-colors').offset().top - 1;
  392.                 if( position <= -1 ) position = -1;
  393.                 if( position >= 149 ) position = 149;
  394.                 input.data('huePosition', position);
  395.                 huePicker.css('top', position).show();
  396.                
  397.                 // Calculate hue
  398.                 h = Math.round((150 - position - 1) * 2.4);
  399.                 if( h < 0 ) h = 0;
  400.                 if( h > 360 ) h = 360;
  401.                
  402.                 // Update HSB values
  403.                 hsb = input.data('hsb');
  404.                 hsb.h = h;
  405.                
  406.                 // Set color
  407.                 setColor(input, hsb, true);
  408.                
  409.             }
  410.            
  411.             function moveOpacity(input, event) {
  412.                
  413.                 var opacityPicker = input.data('opacityPicker'),
  414.                     position = event.pageY,
  415.                     alpha;
  416.                
  417.                 opacityPicker.hide();
  418.                
  419.                 // Touch support
  420.                 if( event.originalEvent.changedTouches ) {
  421.                     position = event.originalEvent.changedTouches[0].pageY;
  422.                 }
  423.                
  424.                 position = position - input.data('selector').find('.miniColors-colors').offset().top - 1;
  425.                 if( position <= -1 ) position = -1;
  426.                 if( position >= 149 ) position = 149;
  427.                 input.data('opacityPosition', position);
  428.                 opacityPicker.css('top', position).show();
  429.                
  430.                 // Calculate opacity
  431.                 alpha = parseFloat((150 - position - 1) / 150).toFixed(2);
  432.                 if( alpha < 0 ) alpha = 0;
  433.                 if( alpha > 1 ) alpha = 1;
  434.                
  435.                 // Update opacity
  436.                 input
  437.                     .data('alpha', alpha)
  438.                     .attr('data-opacity', alpha);
  439.                
  440.                 // Set color
  441.                 setColor(input, input.data('hsb'), true);
  442.                
  443.             }
  444.            
  445.             function setColor(input, hsb, updateInput) {
  446.                
  447.                 var hex = hsb2hex(hsb),
  448.                     selector = $(input.data('selector')),
  449.                     rgb = hsb2rgb(hsb);
  450.                    
  451.                 input.data('hsb', hsb);
  452.                
  453.                 if( updateInput ) input.val( prefix + convertCase(hex, input.data('letterCase')) );
  454.                
  455.                 selector
  456.                     .find('.miniColors-colors').css('backgroundColor', '#' + hsb2hex({ h: hsb.h, s: 100, b: 100 })).end()
  457.                     .find('.miniColors-opacity').css('backgroundColor', '#' + hex).end();
  458.                
  459.                 // Set background color (also fallback for non RGBA browsers)
  460.                 input.data('trigger').css('backgroundColor', '#' + hex);
  461.                
  462.                 // Set background color + opacity
  463.                 if( input.data('opacity') ) {
  464.                     input.data('trigger').css('backgroundColor', 'rgba(' + rgb.r + ', ' + rgb.g + ', ' + rgb.b + ', ' + input.attr('data-opacity') + ')');
  465.                 }
  466.                
  467.                 // Fire change callback
  468.                 if( input.data('change') ) {
  469.                     if( (hex + ',' + input.attr('data-opacity')) === input.data('lastChange') ) return;
  470.                     input.data('change').call(input.get(0), '#' + hex, $.extend(hsb2rgb(hsb), { a: parseFloat(input.attr('data-opacity')) }));
  471.                     input.data('lastChange', hex + ',' + input.attr('data-opacity'));
  472.                 }
  473.                
  474.             }
  475.            
  476.             function setColorFromInput(input) {
  477.                 var hex, hsb, colorPosition, colorPicker, huePosition, huePicker, opacityPosition, opacityPicker;
  478.                
  479.                 input.val(prefix + cleanHex(input.val()));
  480.                 hex = expandHex(input.val());
  481.                 if( !hex ) return false;
  482.                
  483.                 // Get HSB equivalent
  484.                 hsb = hex2hsb(hex);
  485.                
  486.                 // Set colorPicker position
  487.                 colorPosition = getColorPositionFromHSB(hsb);
  488.                 colorPicker = $(input.data('colorPicker'));
  489.                 colorPicker.css('top', colorPosition.y + 'px').css('left', colorPosition.x + 'px');
  490.                 input.data('colorPosition', colorPosition);
  491.                
  492.                 // Set huePosition position
  493.                 huePosition = getHuePositionFromHSB(hsb);
  494.                 huePicker = $(input.data('huePicker'));
  495.                 huePicker.css('top', huePosition + 'px');
  496.                 input.data('huePosition', huePosition);
  497.                
  498.                 // Set opacity position
  499.                 opacityPosition = getOpacityPositionFromAlpha(input.attr('data-opacity'));
  500.                 opacityPicker = $(input.data('opacityPicker'));
  501.                 opacityPicker.css('top', opacityPosition + 'px');
  502.                 input.data('opacityPosition', opacityPosition);
  503.                 setColor(input, hsb);
  504.                
  505.                 return true;
  506.                
  507.             }
  508.            
  509.             function convertCase(string, letterCase) {
  510.                 if( letterCase === 'uppercase' ) {
  511.                     return string.toUpperCase();
  512.                 } else {
  513.                     return string.toLowerCase();
  514.                 }
  515.             }
  516.            
  517.             function getColorPositionFromHSB(hsb) {            
  518.                 var x = Math.ceil(hsb.s / 0.67);
  519.                 if( x < 0 ) x = 0;
  520.                 if( x > 150 ) x = 150;
  521.                 var y = 150 - Math.ceil(hsb.b / 0.67);
  522.                 if( y < 0 ) y = 0;
  523.                 if( y > 150 ) y = 150;
  524.                 return { x: x - 5, y: y - 5 };
  525.             }
  526.            
  527.             function getHuePositionFromHSB(hsb) {
  528.                 var y = 150 - (hsb.h / 2.4);
  529.                 if( y < 0 ) h = 0;
  530.                 if( y > 150 ) h = 150;             
  531.                 return y;
  532.             }
  533.            
  534.             function getOpacityPositionFromAlpha(alpha) {
  535.                 var y = 150 * alpha;
  536.                 if( y < 0 ) y = 0;
  537.                 if( y > 150 ) y = 150;
  538.                 return 150 - y;
  539.             }
  540.            
  541.             function cleanHex(hex) {
  542.                 return hex.replace(/[^A-F0-9]/ig, '');
  543.             }
  544.            
  545.             function expandHex(hex) {
  546.                 hex = cleanHex(hex);
  547.                 if( !hex ) return null;
  548.                 if( hex.length === 3 ) hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
  549.                 return hex.length === 6 ? hex : null;
  550.             }          
  551.            
  552.             function hsb2rgb(hsb) {
  553.                 var rgb = {};
  554.                 var h = Math.round(hsb.h);
  555.                 var s = Math.round(hsb.s*255/100);
  556.                 var v = Math.round(hsb.b*255/100);
  557.                 if(s === 0) {
  558.                     rgb.r = rgb.g = rgb.b = v;
  559.                 } else {
  560.                     var t1 = v;
  561.                     var t2 = (255 - s) * v / 255;
  562.                     var t3 = (t1 - t2) * (h % 60) / 60;
  563.                     if( h === 360 ) h = 0;
  564.                     if( h < 60 ) { rgb.r = t1; rgb.b = t2; rgb.g = t2 + t3; }
  565.                     else if( h < 120 ) {rgb.g = t1; rgb.b = t2; rgb.r = t1 - t3; }
  566.                     else if( h < 180 ) {rgb.g = t1; rgb.r = t2; rgb.b = t2 + t3; }
  567.                     else if( h < 240 ) {rgb.b = t1; rgb.r = t2; rgb.g = t1 - t3; }
  568.                     else if( h < 300 ) {rgb.b = t1; rgb.g = t2; rgb.r = t2 + t3; }
  569.                     else if( h < 360 ) {rgb.r = t1; rgb.g = t2; rgb.b = t1 - t3; }
  570.                     else { rgb.r = 0; rgb.g = 0; rgb.b = 0; }
  571.                 }
  572.                 return {
  573.                     r: Math.round(rgb.r),
  574.                     g: Math.round(rgb.g),
  575.                     b: Math.round(rgb.b)
  576.                 };
  577.             }
  578.            
  579.             function rgb2hex(rgb) {
  580.                 var hex = [
  581.                     rgb.r.toString(16),
  582.                     rgb.g.toString(16),
  583.                     rgb.b.toString(16)
  584.                 ];
  585.                 $.each(hex, function(nr, val) {
  586.                     if (val.length === 1) hex[nr] = '0' + val;
  587.                 });
  588.                 return hex.join('');
  589.             }
  590.            
  591.             function hex2rgb(hex) {
  592.                 hex = parseInt(((hex.indexOf('#') > -1) ? hex.substring(1) : hex), 16);
  593.                
  594.                 return {
  595.                     r: hex >> 16,
  596.                     g: (hex & 0x00FF00) >> 8,
  597.                     b: (hex & 0x0000FF)
  598.                 };
  599.             }
  600.            
  601.             function rgb2hsb(rgb) {
  602.                 var hsb = { h: 0, s: 0, b: 0 };
  603.                 var min = Math.min(rgb.r, rgb.g, rgb.b);
  604.                 var max = Math.max(rgb.r, rgb.g, rgb.b);
  605.                 var delta = max - min;
  606.                 hsb.b = max;
  607.                 hsb.s = max !== 0 ? 255 * delta / max : 0;
  608.                 if( hsb.s !== 0 ) {
  609.                     if( rgb.r === max ) {
  610.                         hsb.h = (rgb.g - rgb.b) / delta;
  611.                     } else if( rgb.g === max ) {
  612.                         hsb.h = 2 + (rgb.b - rgb.r) / delta;
  613.                     } else {
  614.                         hsb.h = 4 + (rgb.r - rgb.g) / delta;
  615.                     }
  616.                 } else {
  617.                     hsb.h = -1;
  618.                 }
  619.                 hsb.h *= 60;
  620.                 if( hsb.h < 0 ) {
  621.                     hsb.h += 360;
  622.                 }
  623.                 hsb.s *= 100/255;
  624.                 hsb.b *= 100/255;
  625.                 return hsb;
  626.             }          
  627.            
  628.             function hex2hsb(hex) {
  629.                 var hsb = rgb2hsb(hex2rgb(hex));
  630.                 // Zero out hue marker for black, white, and grays (saturation === 0)
  631.                 if( hsb.s === 0 ) hsb.h = 360;
  632.                 return hsb;
  633.             }
  634.            
  635.             function hsb2hex(hsb) {
  636.                 return rgb2hex(hsb2rgb(hsb));
  637.             }
  638.  
  639.            
  640.             // Handle calls to $([selector]).miniColors()
  641.             switch(o) {
  642.                
  643.                 case 'hide':
  644.                     hide( $(this) );
  645.                     return $(this);
  646.                
  647.                 case 'show':
  648.                     show( $(this) );
  649.                     return $(this);
  650.                
  651.                 case 'readonly':
  652.                    
  653.                     $(this).each( function() {
  654.                         if( !$(this).hasClass('miniColors') ) return;
  655.                         $(this).prop('readonly', data);
  656.                     });
  657.                    
  658.                     return $(this);
  659.                
  660.                 case 'disabled':
  661.                    
  662.                     $(this).each( function() {
  663.                         if( !$(this).hasClass('miniColors') ) return;
  664.                         if( data ) {
  665.                             disable($(this));
  666.                         } else {
  667.                             enable($(this));
  668.                         }
  669.                     });
  670.                                        
  671.                     return $(this);
  672.            
  673.                 case 'value':
  674.                    
  675.                     // Getter
  676.                     if( data === undefined ) {
  677.                         if( !$(this).hasClass('miniColors') ) return;
  678.                         var hex = expandHex($(this).val());
  679.                         return hex ? prefix + convertCase(hex, $(this).data('letterCase')) : null;
  680.                     }
  681.                    
  682.                     // Setter
  683.                     $(this).each( function() {                     
  684.                         if( !$(this).hasClass('miniColors') ) return;
  685.                         $(this).val(data);
  686.                         setColorFromInput($(this));
  687.                     });
  688.                    
  689.                     return $(this);
  690.                
  691.                 case 'opacity':
  692.                    
  693.                     // Getter
  694.                     if( data === undefined ) {
  695.                         if( !$(this).hasClass('miniColors') ) return;
  696.                         if( $(this).data('opacity') ) {
  697.                             return parseFloat($(this).attr('data-opacity'));
  698.                         } else {
  699.                             return null;
  700.                         }
  701.                     }
  702.                    
  703.                     // Setter
  704.                     $(this).each( function() {                     
  705.                         if( !$(this).hasClass('miniColors') ) return;
  706.                         if( data < 0 ) data = 0;
  707.                         if( data > 1 ) data = 1;
  708.                         $(this).attr('data-opacity', data).data('alpha', data);
  709.                         setColorFromInput($(this));
  710.                     });
  711.                    
  712.                     return $(this);
  713.                    
  714.                 case 'destroy':
  715.                    
  716.                     $(this).each( function() {
  717.                         if( !$(this).hasClass('miniColors') ) return;
  718.                         destroy($(this));
  719.                     });
  720.                                        
  721.                     return $(this);
  722.                
  723.                 default:
  724.                    
  725.                     if( !o ) o = {};
  726.                    
  727.                     $(this).each( function() {
  728.                        
  729.                         // Must be called on an input element
  730.                         if( $(this)[0].tagName.toLowerCase() !== 'input' ) return;
  731.                        
  732.                         // If a trigger is present, the control was already created
  733.                         if( $(this).data('trigger') ) return;
  734.                        
  735.                         // Create the control
  736.                         create($(this), o, data);
  737.                        
  738.                     });
  739.                    
  740.                     return $(this);
  741.                    
  742.             }
  743.            
  744.         }
  745.            
  746.     });
  747.    
  748. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement