source: extensions/GrumPluginClasses/js/ui.inputColorPicker.js @ 14369

Last change on this file since 14369 was 8961, checked in by grum, 13 years ago

release 3.4.0
fix bug:1984, bug:2109
js file are minified, remove packed files

  • Property svn:executable set to *
File size: 35.9 KB
Line 
1/**
2 * -----------------------------------------------------------------------------
3 * file: ui.inputColorPicker.js
4 * file version: 1.0.0
5 * date: 2010-11-04
6 *
7 * A jQuery plugin provided by the piwigo's plugin "GrumPluginClasses"
8 *
9 * -----------------------------------------------------------------------------
10 * Author     : Grum
11 *   email    : grum@piwigo.com
12 *   website  : http://photos.grum.fr
13 *   PWG user : http://forum.phpwebgallery.net/profile.php?id=3706
14 *
15 *   << May the Little SpaceFrog be with you ! >>
16 * -----------------------------------------------------------------------------
17 *
18 *
19 *
20 *
21 * :: HISTORY ::
22 *
23 * | release | date       |
24 * | 1.0.0   | 2010/11/04 | first release
25 * |         |            |
26 * |         |            |
27 * |         |            |
28 * |         |            |
29 * |         |            |
30 * |         |            |
31 *
32 */
33
34
35
36(
37  function($)
38  {
39    /*
40     * plugin 'public' functions
41     */
42    var publicMethods =
43    {
44      init : function (opt)
45        {
46          return this.each(function()
47            {
48              // default values for the plugin
49              var $this=$(this),
50                  data = $this.data('options'),
51                  objects = $this.data('objects'),
52                  properties = $this.data('properties'),
53                  options =
54                    {
55                      colors:
56                        {
57                          fg:
58                            {
59                              color:'#ffffff',
60                              opacity:1
61                            },
62                          bg:
63                            {
64                              color:'#000000',
65                              opacity:1
66                            }
67                        },
68                      alpha:true,
69                      texts:
70                        {
71                          r:'R',
72                          g:'G',
73                          b:'B',
74                          a:'A',
75                          color:'#',
76                          h:'H',
77                          s:'S',
78                          v:'V'
79                        },
80                      mode:2, // 1 (fg) - 2  (fg+bg)
81                      selected:'fg',
82                      change:null
83                    };
84
85              // if options given, merge it
86              // if(opt) $.extend(options, opt); ==> options are set by setters
87
88              $this.data('options', options);
89
90
91              if(!properties)
92              {
93                $this.data('properties',
94                  {
95                    initialized:false,
96                    refreshing:false
97                  }
98                );
99                properties=$this.data('properties');
100              }
101
102              if(!objects)
103              {
104                objects =
105                  {
106                    container:$('<div/>',
107                        {
108                          'class':'ui-inputColorPicker'
109                        }
110                    ),
111                    dotAreaSV:$('<div/>',
112                      {
113                        'class':'ui-inputColorPicker-dotAreaSV'
114                      }
115                    ),
116                    dotAreaH:$('<div/>',
117                      {
118                        'class':'ui-inputColorPicker-dotAreaH'
119                      }
120                    ),
121                    inputContainer:$('<table>',
122                      {
123                        'class':'ui-inputColorPicker-inputContainer'
124                      }
125                    ),
126                    inputCRL:$('<td/>', {'class':'label'} ), // red label
127                    inputCRI:$('<td/>', {'class':'input'} ), // red input
128                    inputCGL:$('<td/>', {'class':'label'} ),
129                    inputCGI:$('<td/>', {'class':'input'} ),
130                    inputCBL:$('<td/>', {'class':'label'} ),
131                    inputCBI:$('<td/>', {'class':'input'} ),
132                    inputCHL:$('<td/>', {'class':'label'} ),
133                    inputCHI:$('<td/>', {'class':'input'} ),
134                    inputCSL:$('<td/>', {'class':'label'} ),
135                    inputCSI:$('<td/>', {'class':'input'} ),
136                    inputCVL:$('<td/>', {'class':'label'} ),
137                    inputCVI:$('<td/>', {'class':'input'} ),
138                    inputCAL:$('<td/>', {'class':'label'} ),
139                    inputCAI:$('<td/>', {'class':'input'} ),
140                    inputCcolorL:$('<td/>', {'class':'label'} ),
141                    inputCcolorI:$('<td/>', {'class':'input'} ),
142                    inputCFB:$('<td/>',
143                      {
144                        colspan:2,
145                        rowspan:2
146                      }
147                    )
148
149                  };
150
151                $this
152                  .html('')
153                  .append(
154                      objects.container
155                        .append(objects.dotAreaSV)
156                        .append(objects.dotAreaH)
157                        .append(objects.inputContainer
158                                  .append($('<tr/>').append(objects.inputCRL).append(objects.inputCRI).append(objects.inputCHL).append(objects.inputCHI))
159                                  .append($('<tr/>').append(objects.inputCGL).append(objects.inputCGI).append(objects.inputCSL).append(objects.inputCSI))
160                                  .append($('<tr/>').append(objects.inputCBL).append(objects.inputCBI).append(objects.inputCVL).append(objects.inputCVI))
161                                  .append($('<tr/>').append(objects.inputCAL).append(objects.inputCAI).append(objects.inputCFB))
162                                  .append($('<tr/>').append(objects.inputCcolorL).append(objects.inputCcolorI))
163                        )
164                  );
165
166                objects.dotAreaSV.inputDotArea(
167                  {
168                    range:
169                      {
170                        x:[0,100],
171                        y:[0,100]
172                      },
173                    width:132,
174                    height:132,
175                    change: function (event, value)
176                      {
177                        privateMethods.computeCurrentColor($this, 'dotAreaSV');
178                      }
179                  }
180                );
181                objects.dotAreaH.inputDotArea(
182                  {
183                    range:
184                      {
185                        x:false,
186                        y:[0,359]
187                      },
188                    width:20,
189                    height:132,
190                    change: function (event, value)
191                      {
192                        privateMethods.computeCurrentColor($this, 'dotAreaH');
193                      }
194                  }
195                );
196                objects.inputCRI.inputNum(
197                  {
198                    minValue:0,
199                    maxValue:255,
200                    stepValue:1,
201                    numDec:0,
202                    unitValue:'',
203                    btInc:'+',
204                    btDec:'-',
205                    value:0,
206                    showSlider:'auto',
207                    change: function (event, value)
208                      {
209                        privateMethods.computeCurrentColor($this, 'rgbR');
210                      }
211                  }
212                );
213                objects.inputCGI.inputNum(
214                  {
215                    minValue:0,
216                    maxValue:255,
217                    stepValue:1,
218                    numDec:0,
219                    unitValue:'',
220                    btInc:'+',
221                    btDec:'-',
222                    value:0,
223                    showSlider:'auto',
224                    change: function (event, value)
225                      {
226                        privateMethods.computeCurrentColor($this, 'rgbG');
227                      }
228                  }
229                );
230                objects.inputCBI.inputNum(
231                  {
232                    minValue:0,
233                    maxValue:255,
234                    stepValue:1,
235                    numDec:0,
236                    unitValue:'',
237                    btInc:'+',
238                    btDec:'-',
239                    value:0,
240                    showSlider:'auto',
241                    change: function (event, value)
242                      {
243                        privateMethods.computeCurrentColor($this, 'rgbB');
244                      }
245                  }
246                );
247                objects.inputCAI.inputNum(
248                  {
249                    minValue:0,
250                    maxValue:100,
251                    stepValue:1,
252                    numDec:0,
253                    unitValue:'',
254                    btInc:'+',
255                    btDec:'-',
256                    value:0,
257                    showSlider:'auto',
258                    change: function (event, value)
259                      {
260                        privateMethods.computeCurrentColor($this, 'alpha');
261                      }
262                  }
263                );
264                objects.inputCHI.inputNum(
265                  {
266                    minValue:0,
267                    maxValue:359,
268                    stepValue:1,
269                    numDec:0,
270                    unitValue:'',
271                    btInc:'+',
272                    btDec:'-',
273                    value:0,
274                    showSlider:'auto',
275                    change: function (event, value)
276                      {
277                        privateMethods.computeCurrentColor($this, 'hsvH');
278                      }
279                  }
280                );
281                objects.inputCSI.inputNum(
282                  {
283                    minValue:0,
284                    maxValue:100,
285                    stepValue:1,
286                    numDec:0,
287                    unitValue:'',
288                    btInc:'+',
289                    btDec:'-',
290                    value:0,
291                    showSlider:'auto',
292                    change: function (event, value)
293                      {
294                        privateMethods.computeCurrentColor($this, 'hsvS');
295                      }
296                  }
297                );
298                objects.inputCVI.inputNum(
299                  {
300                    minValue:0,
301                    maxValue:100,
302                    stepValue:1,
303                    numDec:0,
304                    unitValue:'',
305                    btInc:'+',
306                    btDec:'-',
307                    value:0,
308                    showSlider:'auto',
309                    change: function (event, value)
310                      {
311                        privateMethods.computeCurrentColor($this, 'hsvV');
312                      }
313                  }
314                );
315                objects.inputCcolorI.inputText(
316                  {
317                    value:'',
318                    displayChar:6,
319                    maxChar:6,
320                    regExp:'/^(?:[a-f0-9]{2}){3}$/i',
321                    change: function (event, value)
322                      {
323                        privateMethods.computeCurrentColor($this, 'color');
324                      }
325                  }
326                );
327                objects.inputCFB.inputColorsFB(
328                  {
329                    width:40,
330                    height:40,
331                    change: function (event, value)
332                      {
333                        privateMethods.computeCurrentColor($this, 'fb');
334                      }
335                  }
336                );
337
338
339                $this.data('objects', objects);
340              }
341
342              privateMethods.setOptions($this, opt);
343            }
344          );
345        }, // init
346      destroy : function ()
347        {
348          return this.each(
349            function()
350            {
351              // default values for the plugin
352              var $this=$(this),
353                  objects = $this.data('objects');
354              objects.dotAreaSV.remove();
355              objects.dotAreaH.remove();
356              objects.inputCRL.remove();
357              objects.inputCRI.remove();
358              objects.inputCHL.remove();
359              objects.inputCHI.remove();
360              objects.inputCGL.remove();
361              objects.inputCGI.remove();
362              objects.inputCSL.remove();
363              objects.inputCSI.remove();
364              objects.inputCBL.remove();
365              objects.inputCBI.remove();
366              objects.inputCVL.remove();
367              objects.inputCVI.remove();
368              objects.inputCAL.remove();
369              objects.inputCAI.remove();
370              objects.inputCFB.remove();
371              objects.inputCcolorL.remove();
372              objects.inputCcolorI.remove();
373              objects.inputContainer.remove();
374              objects.container.remove();
375            }
376          );
377        }, // destroy
378
379      options: function (value)
380        {
381          return(
382            this.each(
383              function()
384              {
385                privateMethods.setOptions($(this), value);
386              }
387            )
388          );
389        }, // options
390
391      colors: function (value)
392        {
393          if(value!=null)
394          {
395            // set selected value
396            return(
397              this.each(
398                function()
399                {
400                  privateMethods.setColors($(this), value);
401                }
402              )
403            );
404          }
405          else
406          {
407            // return the selected tags
408            var options=this.data('options');
409            return(options.colors);
410          }
411        }, // colors
412
413      texts: function (value)
414        {
415          if(value!=null)
416          {
417            // set selected value
418            return(
419              this.each(
420                function()
421                {
422                  privateMethods.setTexts($(this), value, true);
423                }
424              )
425            );
426          }
427          else
428          {
429            // return the selected tags
430            var options=this.data('options');
431            return(options.texts);
432          }
433        }, // texts
434
435      alpha: function (value)
436        {
437          if(value!=null)
438          {
439            // set selected value
440            return(
441              this.each(
442                function()
443                {
444                  privateMethods.setAlpha($(this), value);
445                }
446              )
447            );
448          }
449          else
450          {
451            // return the selected tags
452            var options=this.data('options');
453            return(options.alpha);
454          }
455        }, // alpha
456
457      selected: function (value)
458        {
459          if(value!=null)
460          {
461            // set selected value
462            return(
463              this.each(
464                function()
465                {
466                  privateMethods.setSelected($(this), value);
467                }
468              )
469            );
470          }
471          else
472          {
473            // return the selected tags
474            var options=this.data('options');
475            return(options.selected);
476          }
477        }, // selected
478
479      mode: function (value)
480        {
481          if(value!=null)
482          {
483            // set selected value
484            return(
485              this.each(
486                function()
487                {
488                  privateMethods.setMode($(this), value);
489                }
490              )
491            );
492          }
493          else
494          {
495            // return the selected tags
496            var options=this.data('options');
497            return(options.mode);
498          }
499        }, // mode
500
501      change: function (value)
502        {
503          if(value!=null && $.isFunction(value))
504          {
505            // set selected value
506            return(
507              this.each(
508                function()
509                {
510                  privateMethods.setEventChange($(this), value);
511                }
512              )
513            );
514          }
515          else
516          {
517            // return the selected value
518            var options=this.data('options');
519
520            if(options)
521            {
522              return(options.change);
523            }
524            else
525            {
526              return(null);
527            }
528          }
529        } // change
530
531    }; // methods
532
533
534    /*
535     * plugin 'private' methods
536     */
537    var privateMethods =
538    {
539      setOptions : function (object, value)
540        {
541          var properties=object.data('properties'),
542              options=object.data('options');
543
544          if(!$.isPlainObject(value)) return(false);
545
546          properties.initialized=false;
547
548          privateMethods.setMode(object, (value.mode!=null)?value.mode:options.mode);
549          privateMethods.setSelected(object, (value.selected!=null)?value.selected:options.selected);
550          privateMethods.setColors(object, (value.colors!=null)?value.colors:options.colors);
551          privateMethods.setAlpha(object, (value.alpha!=null)?value.alpha:options.alpha);
552          privateMethods.setTexts(object, (value.texts!=null)?value.texts:options.texts);
553
554          privateMethods.setEventChange(object, (value.change!=null)?value.change:options.change);
555
556          properties.initialized=true;
557        },
558
559      setAlpha : function (object, value)
560        {
561          var options=object.data('options'),
562              objects=object.data('objects');
563
564          if((!properties.initialized || options.alpha!=value) && (value==true || value==false))
565          {
566            options.alpha=value;
567
568            if(options.alpha)
569            {
570              objects.inputCAI.css('visibility', 'visible');
571              objects.inputCAL.css('visibility', 'visible');
572            }
573            else
574            {
575              objects.inputCAI.css('visibility', 'hidden');
576              objects.inputCAL.css('visibility', 'hidden');
577            }
578          }
579          return(options.alpha);
580        }, //setAlpha
581
582      setMode : function (object, value)
583        {
584          var options=object.data('options'),
585              objects=object.data('objects');
586
587          if((!properties.initialized || options.mode!=value) && (value==1 || value==2))
588          {
589            options.mode=value;
590
591            objects.inputCFB.inputColorsFB('mode', options.mode);
592          }
593          return(options.mode);
594        }, //setMode
595
596
597      setSelected : function (object, value)
598        {
599          var options=object.data('options'),
600              objects=object.data('objects');
601
602          if((!properties.initialized || options.selected!=value) && (value=='fg' || value=='bg'))
603          {
604            options.selected=value;
605
606            objects.inputCFB.inputColorsFB('selected', options.selected);
607          }
608          return(options.selected);
609        }, //setSelected
610
611
612      setTexts : function (object, value)
613        {
614          var options=object.data('options'),
615              objects=object.data('objects');
616
617          if(value.r!=null)
618          {
619            options.texts.r=value.r;
620            objects.inputCRL.html(options.texts.r);
621          }
622          if(value.g!=null)
623          {
624            options.texts.g=value.g;
625            objects.inputCGL.html(options.texts.g);
626          }
627          if(value.b!=null)
628          {
629            options.texts.b=value.b;
630            objects.inputCBL.html(options.texts.b);
631          }
632          if(value.h!=null)
633          {
634            options.texts.h=value.h;
635            objects.inputCHL.html(options.texts.h);
636          }
637          if(value.s!=null)
638          {
639            options.texts.s=value.s;
640            objects.inputCSL.html(options.texts.s);
641          }
642          if(value.v!=null)
643          {
644            options.texts.v=value.v;
645            objects.inputCVL.html(options.texts.v);
646          }
647          if(value.a!=null)
648          {
649            options.texts.a=value.a;
650            objects.inputCAL.html(options.texts.a);
651          }
652          if(value.color!=null)
653          {
654            options.texts.color=value.color;
655            objects.inputCcolorL.html(options.texts.color);
656          }
657
658          return(options.texts);
659        }, //setColors
660
661      /**
662       * @param String color : a '#rrggbb' string
663       * @param Integer opacity : [0..100] value
664       * @param String target : 'fg' or 'bg'
665       * @param String from : '' for refreshing all items
666       */
667      setColor : function (object, color, opacity, target, from)
668        {
669          var objects=object.data('objects'),
670              properties=object.data('properties'),
671              options=object.data('options'),
672              rgbColor={r:0, g:0, b:0},
673              hsvColor={h:0, s:0, v:0},
674              svColorHue=0;
675
676          if(properties.refreshing || !(target=='fg' || target=='bg')) return(false);
677
678          properties.refreshing=true;
679
680          rgbColor=privateMethods.strToRGB(color);
681          hsvColor=privateMethods.RGBtoHSV(rgbColor);
682
683          if(from=='hsvH') hsvColor.h=objects.inputCHI.inputNum('value');
684          if(from=='hsvS') hsvColor.s=objects.inputCSI.inputNum('value');
685          if(from=='hsvV') hsvColor.v=objects.inputCVI.inputNum('value');
686          if(from=='dotAreaSV')
687          {
688            sv=objects.dotAreaSV.inputDotArea('values');
689            hsvColor.s=100-Math.round(sv.y);
690            hsvColor.v=Math.round(sv.x);
691          }
692          if(from=='dotAreaH') hsvColor.h=360-Math.round(objects.dotAreaH.inputDotArea('values').y);
693          svColorHue=hsvColor.h;
694
695          if(from=='' || from=='fb') objects.inputCAI.inputNum('value', opacity);
696
697          if(from!='hsvH' && from!='dotAreaSV' && from!='alpha') objects.dotAreaSV.inputDotArea('values', { x:hsvColor.v, y:100-hsvColor.s } );
698          if(from!='dotAreaH' && from!='alpha' && from!='hsvS' && from!='hsvV' && color!='#000000') objects.dotAreaH.inputDotArea('values', { y:360-hsvColor.h } );
699          if(from!='rgbR' && from!='rgbG' && from!='rgbB' && from!='alpha')
700          {
701            objects.inputCRI.inputNum('value', rgbColor.r);
702            objects.inputCGI.inputNum('value', rgbColor.g);
703            objects.inputCBI.inputNum('value', rgbColor.b);
704          }
705          if(from!='hsvH' && from!='hsvS' && from!='hsvV' && from!='alpha')
706          {
707            if(color!='#000000')
708            {
709              objects.inputCHI.inputNum('value', hsvColor.h);
710            }
711            else
712            {
713              svColorHue=objects.inputCHI.inputNum('value');
714            }
715            objects.inputCSI.inputNum('value', hsvColor.s);
716            objects.inputCVI.inputNum('value', hsvColor.v);
717          }
718          if(from!='color' && from!='alpha') objects.inputCcolorI.inputText('value', color.substr(1));
719
720          if(from!='alpha') objects.dotAreaSV
721                              .find('div.ui-inputDotArea')
722                              .css('background-color',
723                                privateMethods.RGBToStr(privateMethods.HSVtoRGB({h:svColorHue,s:100,v:100}))
724                              );
725
726          if(from!='fb') objects.inputCFB.inputColorsFB(target, {color:color, opacity:opacity/100} );
727
728          if(from!='')
729          {
730            if(target=='fg')
731            {
732              options.colors.fg.color=color;
733              options.colors.fg.opacity=opacity/100;
734            }
735            else
736            {
737              options.colors.bg.color=color;
738              options.colors.bg.opacity=opacity/100;
739            }
740          }
741
742          properties.refreshing=false;
743
744          if(options.change) object.trigger('inputColorPickerChange', {target:target, color:{color:color, opacity:opacity/100} } );
745        },
746
747      setColors : function (object, value)
748        {
749          var objects=object.data('objects'),
750              options=object.data('options');
751
752          if(value.fg!=null)
753          {
754            if(value.fg.color!=null) options.colors.fg.color=value.fg.color;
755            if(value.fg.opacity!=null) options.colors.fg.opacity=value.fg.opacity;
756            if(objects.inputCFB.inputColorsFB('selected')=='fg')
757            {
758              privateMethods.setColor(object, options.colors.fg.color, Math.round(options.colors.fg.opacity*100), 'fg', '');
759            }
760            else
761            {
762              objects.inputCFB.inputColorsFB('fg', options.colors.fg);
763            }
764          }
765
766          if(value.bg!=null)
767          {
768            if(value.bg.color!=null) options.colors.bg.color=value.bg.color;
769            if(value.bg.opacity!=null) options.colors.bg.opacity=value.bg.opacity;
770            if(objects.inputCFB.inputColorsFB('selected')=='bg')
771            {
772              privateMethods.setColor(object, options.colors.bg.color, Math.round(options.colors.bg.opacity*100), 'bg', '');
773            }
774            else
775            {
776              objects.inputCFB.inputColorsFB('bg', options.colors.bg);
777            }
778          }
779
780          return(options.colors);
781        }, //setFG
782
783      setEventChange : function (object, value)
784        {
785          var options=object.data('options');
786
787          options.change=value;
788          object.unbind('inputColorPickerChange');
789          if(value) object.bind('inputColorPickerChange', options.change);
790          return(options.change);
791        },
792
793      computeCurrentColor : function (object, from)
794        {
795          var options=object.data('options'),
796              objects=object.data('objects'),
797              properties=object.data('properties'),
798              colors={},
799              target=objects.inputCFB.inputColorsFB('selected');
800
801          if(properties.refreshing) return(false);
802
803          switch(from)
804          {
805            case 'dotAreaSV':
806            case 'dotAreaH':
807              var sv=objects.dotAreaSV.inputDotArea('values');
808              colors.color=privateMethods.RGBToStr(
809                privateMethods.HSVtoRGB(
810                  {
811                    h:360-objects.dotAreaH.inputDotArea('values').y,
812                    s:100-sv.y,
813                    v:sv.x
814                  }
815                )
816              );
817              if(target=='fg')
818              {
819                colors.opacity=Math.round(options.colors.fg.opacity*100);
820              }
821              else
822              {
823                colors.opacity=Math.round(options.colors.bg.opacity*100);
824              }
825              break;
826            case 'hsvH':
827            case 'hsvS':
828            case 'hsvV':
829              colors.color=privateMethods.RGBToStr(
830                privateMethods.HSVtoRGB(
831                  {
832                    h:objects.inputCHI.inputNum('value'),
833                    s:objects.inputCSI.inputNum('value'),
834                    v:objects.inputCVI.inputNum('value')
835                  }
836                )
837              );
838              if(target=='fg')
839              {
840                colors.opacity=Math.round(options.colors.fg.opacity*100);
841              }
842              else
843              {
844                colors.opacity=Math.round(options.colors.bg.opacity*100);
845              }
846              break;
847            case 'rgbR':
848            case 'rgbG':
849            case 'rgbB':
850              colors.color=privateMethods.RGBToStr(
851                {
852                  r:objects.inputCRI.inputNum('value'),
853                  g:objects.inputCGI.inputNum('value'),
854                  b:objects.inputCBI.inputNum('value')
855                }
856              );
857              if(target=='fg')
858              {
859                colors.opacity=Math.round(options.colors.fg.opacity*100);
860              }
861              else
862              {
863                colors.opacity=Math.round(options.colors.bg.opacity*100);
864              }
865              break;
866            case 'alpha':
867              if(target=='fg')
868              {
869                colors.color=options.colors.fg.color;
870              }
871              else
872              {
873                colors.color=options.colors.bg.color;
874              }
875              colors.opacity=objects.inputCAI.inputNum('value');
876              break;
877            case 'fb':
878              if(target=='fg')
879              {
880                colors.color=options.colors.fg.color;
881                colors.opacity=Math.round(options.colors.fg.opacity*100);
882              }
883              else
884              {
885                colors.color=options.colors.bg.color;
886                colors.opacity=Math.round(options.colors.bg.opacity*100);
887              }
888              break;
889            case 'color':
890              colors.color='#'+objects.inputCcolorI.inputText('value');
891              if(target=='fg')
892              {
893                colors.opacity=Math.round(options.colors.fg.opacity*100);
894              }
895              else
896              {
897                colors.opacity=Math.round(options.colors.bg.opacity*100);
898              }
899              break;
900          }
901
902          privateMethods.setColor(object, colors.color, colors.opacity, target, from);
903        },
904
905      /**
906       * convert a RGB object {r,g,b} to String '#rrggbb'
907       * @param Object color : {r,g,b} object
908       * @return String
909       */
910      RGBToStr : function (color)
911        {
912          var r=color.r.toString(16),
913              g=color.g.toString(16),
914              b=color.b.toString(16);
915          if(r.length<2) r='0'+r;
916          if(g.length<2) g='0'+g;
917          if(b.length<2) b='0'+b;
918          return('#'+r+g+b);
919        },
920
921      /**
922       * convert a string to RGB object {r,g,b}
923       * string can be '#rrggbb' or 'rgb(r,g,b)'
924       * @param String color
925       * @return Object
926       */
927      strToRGB : function (color)
928        {
929          var returned={
930                r:0,
931                g:0,
932                b:0
933              },
934              re1=/^rgb\((\d){1,3},(\d){1,3},(\d){1,3}\)$/i,
935              re2=/^#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i,
936              colors=re1.exec(color);
937
938          if(colors!=null)
939          {
940            returned.r=parseInt(colors[1]);
941            returned.g=parseInt(colors[2]);
942            returned.b=parseInt(colors[3]);
943          }
944          else
945          {
946            colors=re2.exec(color);
947
948            if(colors!=null)
949            {
950              returned.r=parseInt('0x'+colors[1]);
951              returned.g=parseInt('0x'+colors[2]);
952              returned.b=parseInt('0x'+colors[3]);
953            }
954          }
955          return(returned);
956        },
957
958      /**
959       * convert an RGB object {r,g,b} to HSV object {h,s,v}
960       * @param Object color : {r,g,b}
961       * @return Object
962       */
963      RGBtoHSV : function (color)
964        {
965          var returned={
966                h:0,
967                s:0,
968                v:0
969          },
970          color={
971            r:color.r/255,
972            g:color.g/255,
973            b:color.b/255
974          },
975          max=Math.max(color.r, color.g, color.b),
976          min=Math.min(color.r, color.g, color.b);
977
978          if(max==min)
979          {
980            returned.h=0;
981          }
982          else if(max==color.r)
983          {
984            returned.h=Math.round((60*(color.g-color.b)/(max-min)+360)%360);
985          }
986          else if(max==color.g)
987          {
988            returned.h=Math.round((60*(color.b-color.r)/(max-min)+120));
989          }
990          else if(max==color.b)
991          {
992            returned.h=Math.round((60*(color.r-color.g)/(max-min)+240));
993          }
994
995          returned.s=Math.round(100*(max==0?0:1-min/max));
996          returned.v=Math.round(100*max);
997
998          return(returned);
999        },
1000
1001      /**
1002       * convert an HSV object {h,s,v} to RGB object {r,g,b}
1003       * @param Object color : {h,s,v}
1004       * @return Object : {r,g,b}
1005       */
1006      HSVtoRGB : function (color)
1007        {
1008          var h=Math.floor(Math.abs(color.h/60))%6,
1009              f=color.h/60-h,
1010              l=Math.round(2.55*color.v*(1-color.s/100)),
1011              m=Math.round(2.55*color.v*(1-f*color.s/100)),
1012              n=Math.round(2.55*color.v*(1-(1-f)*color.s/100)),
1013              v=Math.round(2.55*color.v);
1014
1015          switch(h)
1016          {
1017            case 0:
1018              return({r:v, g:n, b:l});
1019              break;
1020            case 1:
1021              return({r:m, g:v, b:l});
1022              break;
1023            case 2:
1024              return({r:l, g:v, b:n});
1025              break;
1026            case 3:
1027              return({r:l, g:m, b:v});
1028              break;
1029            case 4:
1030              return({r:n, g:l, b:v});
1031              break;
1032            case 5:
1033              return({r:v, g:l, b:m});
1034              break;
1035          }
1036          return({r:0, g:0, b:0});
1037        }
1038    };
1039
1040
1041    $.fn.inputColorPicker = function(method)
1042    {
1043      if(publicMethods[method])
1044      {
1045        return publicMethods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
1046      }
1047      else if(typeof method === 'object' || ! method)
1048      {
1049        return publicMethods.init.apply(this, arguments);
1050      }
1051      else
1052      {
1053        $.error( 'Method ' +  method + ' does not exist on jQuery.inputColorPicker' );
1054      }
1055    } // $.fn.inputColorPicker
1056
1057  }
1058)(jQuery);
1059
1060$.inputDialogColor = function(opt)
1061{
1062  var options=
1063        {
1064          modal:true,
1065          mode:2,
1066          alpha:true,
1067          colors:
1068            {
1069              fg:{ color:'#ffffff', opacity:1 },
1070              bg:{ color:'#000000', opacity:1 }
1071            },
1072          title:'Colors selector',
1073          texts:null,
1074          buttons:
1075            {
1076              ok:'Ok',
1077              cancel:'Cancel'
1078            },
1079          selected:'fg',
1080          change:null
1081        },
1082      objects=
1083        {
1084          dialogBox:$('<div/>'),
1085          colorPicker:$('<div/>', {'class':'dialogColor'} )
1086        },
1087
1088  setOptions = function (opt)
1089    {
1090      if(opt.modal==true || opt.modal==false) options.modal=opt.modal;
1091      if(opt.mode==1 || opt.mode==2) options.mode=opt.mode;
1092      if(opt.alpha==true || opt.alpha==false) options.alpha=opt.alpha;
1093      if(opt.colors!=null) options.colors=opt.colors;
1094      options.texts=opt.texts;
1095      if(opt.selected=='fg' || opt.selected=='bg') options.selected=opt.selected;
1096      if(opt.title) options.title=opt.title;
1097      if(opt.buttons && opt.buttons.ok) options.buttons.ok=opt.buttons.ok;
1098      if(opt.buttons && opt.buttons.cancel) options.buttons.cancel=opt.buttons.cancel;
1099      if(opt.change && $.isFunction(opt.change)) options.change=opt.change;
1100    },
1101
1102  initDialog = function ()
1103    {
1104      var dialogOpt=
1105          {
1106            buttons:{},
1107            width:354,
1108            closeText:'x',
1109            dialogClass:'ui-inputDialogColor',
1110            modal:options.modal,
1111            resizable:false,
1112            title:options.title,
1113            open: null,
1114            close: function ()
1115                    {
1116                      objects.colorPicker.inputColorPicker('destroy').remove();
1117                      $(this).dialog('destroy');
1118                    }
1119          };
1120
1121      if(options.modal)
1122      {
1123        dialogOpt.buttons=
1124          {
1125            'ok' : function (event)
1126                      {
1127                        if(options.change)
1128                        {
1129                          if(options.mode==1)
1130                          {
1131                            options.change(event, objects.colorPicker.inputColorPicker('colors').fg );
1132                          }
1133                          else
1134                          {
1135                            options.change(event, objects.colorPicker.inputColorPicker('colors') );
1136                          }
1137                        }
1138                        $(this).dialog('close');
1139                      },
1140            'cancel' : function (event)
1141                      {
1142                        $(this).dialog('close');
1143                      }
1144          };
1145        dialogOpt.open= function ()
1146          {
1147            objects.colorPicker
1148              .inputColorPicker(
1149                {
1150                  mode:options.mode,
1151                  alpha:options.alpha,
1152                  texts:options.texts,
1153                  colors:options.colors,
1154                  selected:options.selected
1155                }
1156              );
1157          };
1158      }
1159      else
1160      {
1161        dialogOpt.open= function ()
1162          {
1163            objects.colorPicker
1164              .inputColorPicker(
1165                {
1166                  mode:options.mode,
1167                  alpha:options.alpha,
1168                  texts:options.texts,
1169                  colors:options.colors,
1170                  change:options.change,
1171                  selected:options.selected
1172                }
1173              );
1174          };
1175      }
1176
1177      objects.dialogBox
1178        .append(objects.colorPicker)
1179        .dialog(dialogOpt);
1180    };
1181
1182  setOptions(opt);
1183  initDialog();
1184
1185} // $.fn.inputDialogColor
1186
1187
Note: See TracBrowser for help on using the repository browser.