Debian Codegraph @

ui/vendor/jquery-color/jquery.color.js

raw
1 /*!
2 * jQuery Color Animations v3.0.0
3 * https://github.com/jquery/jquery-color
4 *
5 * Copyright OpenJS Foundation and other contributors
6 * Released under the MIT license.
7 * https://jquery.org/license
8 *
9 * Date: Wed May 15 16:49:44 2024 +0200
10 */
11
12 ( function( root, factory ) {
13 "use strict";
14
15 if ( typeof define === "function" && define.amd ) {
16
17 // AMD. Register as an anonymous module.
18 define( [ "jquery" ], factory );
19 } else if ( typeof exports === "object" ) {
20 module.exports = factory( require( "jquery" ) );
21 } else {
22 factory( root.jQuery );
23 }
24 } )( this, function( jQuery, undefined ) {
25 "use strict";
26
27 var stepHooks = "backgroundColor borderBottomColor borderLeftColor borderRightColor " +
28 "borderTopColor color columnRuleColor outlineColor textDecorationColor textEmphasisColor",
29
30 class2type = {},
31 toString = class2type.toString,
32
33 // plusequals test for += 100 -= 100
34 rplusequals = /^([\-+])=\s*(\d+\.?\d*)/,
35
36 // a set of RE's that can match strings and generate color tuples.
37 stringParsers = [ {
38 re: /rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(?:,\s*(\d?(?:\.\d+)?)\s*)?\)/,
39 parse: function( execResult ) {
40 return [
41 execResult[ 1 ],
42 execResult[ 2 ],
43 execResult[ 3 ],
44 execResult[ 4 ]
45 ];
46 }
47 }, {
48 re: /rgba?\(\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*(?:,\s*(\d?(?:\.\d+)?)\s*)?\)/,
49 parse: function( execResult ) {
50 return [
51 execResult[ 1 ] * 2.55,
52 execResult[ 2 ] * 2.55,
53 execResult[ 3 ] * 2.55,
54 execResult[ 4 ]
55 ];
56 }
57 }, {
58
59 // this regex ignores A-F because it's compared against an already lowercased string
60 re: /#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})?/,
61 parse: function( execResult ) {
62 return [
63 parseInt( execResult[ 1 ], 16 ),
64 parseInt( execResult[ 2 ], 16 ),
65 parseInt( execResult[ 3 ], 16 ),
66 execResult[ 4 ] ?
67 ( parseInt( execResult[ 4 ], 16 ) / 255 ).toFixed( 2 ) :
68 1
69 ];
70 }
71 }, {
72
73 // this regex ignores A-F because it's compared against an already lowercased string
74 re: /#([a-f0-9])([a-f0-9])([a-f0-9])([a-f0-9])?/,
75 parse: function( execResult ) {
76 return [
77 parseInt( execResult[ 1 ] + execResult[ 1 ], 16 ),
78 parseInt( execResult[ 2 ] + execResult[ 2 ], 16 ),
79 parseInt( execResult[ 3 ] + execResult[ 3 ], 16 ),
80 execResult[ 4 ] ?
81 ( parseInt( execResult[ 4 ] + execResult[ 4 ], 16 ) / 255 )
82 .toFixed( 2 ) :
83 1
84 ];
85 }
86 }, {
87 re: /hsla?\(\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*(?:,\s*(\d?(?:\.\d+)?)\s*)?\)/,
88 space: "hsla",
89 parse: function( execResult ) {
90 return [
91 execResult[ 1 ],
92 execResult[ 2 ] / 100,
93 execResult[ 3 ] / 100,
94 execResult[ 4 ]
95 ];
96 }
97 } ],
98
99 // jQuery.Color( )
100 color = jQuery.Color = function( color, green, blue, alpha ) {
101 return new jQuery.Color.fn.parse( color, green, blue, alpha );
102 },
103 spaces = {
104 rgba: {
105 props: {
106 red: {
107 idx: 0,
108 type: "byte"
109 },
110 green: {
111 idx: 1,
112 type: "byte"
113 },
114 blue: {
115 idx: 2,
116 type: "byte"
117 }
118 }
119 },
120
121 hsla: {
122 props: {
123 hue: {
124 idx: 0,
125 type: "degrees"
126 },
127 saturation: {
128 idx: 1,
129 type: "percent"
130 },
131 lightness: {
132 idx: 2,
133 type: "percent"
134 }
135 }
136 }
137 },
138 propTypes = {
139 "byte": {
140 floor: true,
141 max: 255
142 },
143 "percent": {
144 max: 1
145 },
146 "degrees": {
147 mod: 360,
148 floor: true
149 }
150 },
151
152 // colors = jQuery.Color.names
153 colors,
154
155 // local aliases of functions called often
156 each = jQuery.each;
157
158 // define cache name and alpha properties
159 // for rgba and hsla spaces
160 each( spaces, function( spaceName, space ) {
161 space.cache = "_" + spaceName;
162 space.props.alpha = {
163 idx: 3,
164 type: "percent",
165 def: 1
166 };
167 } );
168
169 // Populate the class2type map
170 jQuery.each( "Boolean Number String Function Array Date RegExp Object Error Symbol".split( " " ),
171 function( _i, name ) {
172 class2type[ "[object " + name + "]" ] = name.toLowerCase();
173 } );
174
175 function getType( obj ) {
176 if ( obj == null ) {
177 return obj + "";
178 }
179
180 return typeof obj === "object" ?
181 class2type[ toString.call( obj ) ] || "object" :
182 typeof obj;
183 }
184
185 function clamp( value, prop, allowEmpty ) {
186 var type = propTypes[ prop.type ] || {};
187
188 if ( value == null ) {
189 return ( allowEmpty || !prop.def ) ? null : prop.def;
190 }
191
192 // ~~ is an short way of doing floor for positive numbers
193 value = type.floor ? ~~value : parseFloat( value );
194
195 if ( type.mod ) {
196
197 // we add mod before modding to make sure that negatives values
198 // get converted properly: -10 -> 350
199 return ( value + type.mod ) % type.mod;
200 }
201
202 // for now all property types without mod have min and max
203 return Math.min( type.max, Math.max( 0, value ) );
204 }
205
206 function stringParse( string ) {
207 var inst = color(),
208 rgba = inst._rgba = [];
209
210 string = string.toLowerCase();
211
212 each( stringParsers, function( _i, parser ) {
213 var parsed,
214 match = parser.re.exec( string ),
215 values = match && parser.parse( match ),
216 spaceName = parser.space || "rgba";
217
218 if ( values ) {
219 parsed = inst[ spaceName ]( values );
220
221 // if this was an rgba parse the assignment might happen twice
222 // oh well....
223 inst[ spaces[ spaceName ].cache ] = parsed[ spaces[ spaceName ].cache ];
224 rgba = inst._rgba = parsed._rgba;
225
226 // exit each( stringParsers ) here because we matched
227 return false;
228 }
229 } );
230
231 // Found a stringParser that handled it
232 if ( rgba.length ) {
233
234 // if this came from a parsed string, force "transparent" when alpha is 0
235 // chrome, (and maybe others) return "transparent" as rgba(0,0,0,0)
236 if ( rgba.join() === "0,0,0,0" ) {
237 jQuery.extend( rgba, colors.transparent );
238 }
239 return inst;
240 }
241
242 // named colors
243 return colors[ string ];
244 }
245
246 color.fn = jQuery.extend( color.prototype, {
247 parse: function( red, green, blue, alpha ) {
248 if ( red === undefined ) {
249 this._rgba = [ null, null, null, null ];
250 return this;
251 }
252 if ( red.jquery || red.nodeType ) {
253 red = jQuery( red ).css( green );
254 green = undefined;
255 }
256
257 var inst = this,
258 type = getType( red ),
259 rgba = this._rgba = [];
260
261 // more than 1 argument specified - assume ( red, green, blue, alpha )
262 if ( green !== undefined ) {
263 red = [ red, green, blue, alpha ];
264 type = "array";
265 }
266
267 if ( type === "string" ) {
268 return this.parse( stringParse( red ) || colors._default );
269 }
270
271 if ( type === "array" ) {
272 each( spaces.rgba.props, function( _key, prop ) {
273 rgba[ prop.idx ] = clamp( red[ prop.idx ], prop );
274 } );
275 return this;
276 }
277
278 if ( type === "object" ) {
279 if ( red instanceof color ) {
280 each( spaces, function( _spaceName, space ) {
281 if ( red[ space.cache ] ) {
282 inst[ space.cache ] = red[ space.cache ].slice();
283 }
284 } );
285 } else {
286 each( spaces, function( _spaceName, space ) {
287 var cache = space.cache;
288 each( space.props, function( key, prop ) {
289
290 // if the cache doesn't exist, and we know how to convert
291 if ( !inst[ cache ] && space.to ) {
292
293 // if the value was null, we don't need to copy it
294 // if the key was alpha, we don't need to copy it either
295 if ( key === "alpha" || red[ key ] == null ) {
296 return;
297 }
298 inst[ cache ] = space.to( inst._rgba );
299 }
300
301 // this is the only case where we allow nulls for ALL properties.
302 // call clamp with alwaysAllowEmpty
303 inst[ cache ][ prop.idx ] = clamp( red[ key ], prop, true );
304 } );
305
306 // everything defined but alpha?
307 if ( inst[ cache ] && jQuery.inArray(
308 null,
309 inst[ cache ].slice( 0, 3 )
310 ) < 0 ) {
311
312 // use the default of 1
313 if ( inst[ cache ][ 3 ] == null ) {
314 inst[ cache ][ 3 ] = 1;
315 }
316
317 if ( space.from ) {
318 inst._rgba = space.from( inst[ cache ] );
319 }
320 }
321 } );
322 }
323 return this;
324 }
325 },
326 is: function( compare ) {
327 var is = color( compare ),
328 same = true,
329 inst = this;
330
331 each( spaces, function( _, space ) {
332 var localCache,
333 isCache = is[ space.cache ];
334 if ( isCache ) {
335 localCache = inst[ space.cache ] || space.to && space.to( inst._rgba ) || [];
336 each( space.props, function( _, prop ) {
337 if ( isCache[ prop.idx ] != null ) {
338 same = ( isCache[ prop.idx ] === localCache[ prop.idx ] );
339 return same;
340 }
341 } );
342 }
343 return same;
344 } );
345 return same;
346 },
347 _space: function() {
348 var used = [],
349 inst = this;
350 each( spaces, function( spaceName, space ) {
351 if ( inst[ space.cache ] ) {
352 used.push( spaceName );
353 }
354 } );
355 return used.pop();
356 },
357 transition: function( other, distance ) {
358 var end = color( other ),
359 spaceName = end._space(),
360 space = spaces[ spaceName ],
361 startColor = this.alpha() === 0 ? color( "transparent" ) : this,
362 start = startColor[ space.cache ] || space.to( startColor._rgba ),
363 result = start.slice();
364
365 end = end[ space.cache ];
366 each( space.props, function( _key, prop ) {
367 var index = prop.idx,
368 startValue = start[ index ],
369 endValue = end[ index ],
370 type = propTypes[ prop.type ] || {};
371
372 // if null, don't override start value
373 if ( endValue === null ) {
374 return;
375 }
376
377 // if null - use end
378 if ( startValue === null ) {
379 result[ index ] = endValue;
380 } else {
381 if ( type.mod ) {
382 if ( endValue - startValue > type.mod / 2 ) {
383 startValue += type.mod;
384 } else if ( startValue - endValue > type.mod / 2 ) {
385 startValue -= type.mod;
386 }
387 }
388 result[ index ] = clamp( ( endValue - startValue ) * distance + startValue, prop );
389 }
390 } );
391 return this[ spaceName ]( result );
392 },
393 blend: function( opaque ) {
394
395 // if we are already opaque - return ourself
396 if ( this._rgba[ 3 ] === 1 ) {
397 return this;
398 }
399
400 var rgb = this._rgba.slice(),
401 a = rgb.pop(),
402 blend = color( opaque )._rgba;
403
404 return color( jQuery.map( rgb, function( v, i ) {
405 return ( 1 - a ) * blend[ i ] + a * v;
406 } ) );
407 },
408 toRgbaString: function() {
409 var prefix = "rgba(",
410 rgba = jQuery.map( this._rgba, function( v, i ) {
411 if ( v != null ) {
412 return v;
413 }
414 return i > 2 ? 1 : 0;
415 } );
416
417 if ( rgba[ 3 ] === 1 ) {
418 rgba.pop();
419 prefix = "rgb(";
420 }
421
422 return prefix + rgba.join( ", " ) + ")";
423 },
424 toHslaString: function() {
425 var prefix = "hsla(",
426 hsla = jQuery.map( this.hsla(), function( v, i ) {
427 if ( v == null ) {
428 v = i > 2 ? 1 : 0;
429 }
430
431 // catch 1 and 2
432 if ( i && i < 3 ) {
433 v = Math.round( v * 100 ) + "%";
434 }
435 return v;
436 } );
437
438 if ( hsla[ 3 ] === 1 ) {
439 hsla.pop();
440 prefix = "hsl(";
441 }
442 return prefix + hsla.join( ", " ) + ")";
443 },
444 toHexString: function( includeAlpha ) {
445 var rgba = this._rgba.slice(),
446 alpha = rgba.pop();
447
448 if ( includeAlpha ) {
449 rgba.push( ~~( alpha * 255 ) );
450 }
451
452 return "#" + jQuery.map( rgba, function( v ) {
453
454 // default to 0 when nulls exist
455 return ( "0" + ( v || 0 ).toString( 16 ) ).substr( -2 );
456 } ).join( "" );
457 },
458 toString: function() {
459 return this.toRgbaString();
460 }
461 } );
462 color.fn.parse.prototype = color.fn;
463
464 // hsla conversions adapted from:
465 // https://code.google.com/p/maashaack/source/browse/packages/graphics/trunk/src/graphics/colors/HUE2RGB.as?r=5021
466
467 function hue2rgb( p, q, h ) {
468 h = ( h + 1 ) % 1;
469 if ( h * 6 < 1 ) {
470 return p + ( q - p ) * h * 6;
471 }
472 if ( h * 2 < 1 ) {
473 return q;
474 }
475 if ( h * 3 < 2 ) {
476 return p + ( q - p ) * ( ( 2 / 3 ) - h ) * 6;
477 }
478 return p;
479 }
480
481 spaces.hsla.to = function( rgba ) {
482 if ( rgba[ 0 ] == null || rgba[ 1 ] == null || rgba[ 2 ] == null ) {
483 return [ null, null, null, rgba[ 3 ] ];
484 }
485 var r = rgba[ 0 ] / 255,
486 g = rgba[ 1 ] / 255,
487 b = rgba[ 2 ] / 255,
488 a = rgba[ 3 ],
489 max = Math.max( r, g, b ),
490 min = Math.min( r, g, b ),
491 diff = max - min,
492 add = max + min,
493 l = add * 0.5,
494 h, s;
495
496 if ( min === max ) {
497 h = 0;
498 } else if ( r === max ) {
499 h = ( 60 * ( g - b ) / diff ) + 360;
500 } else if ( g === max ) {
501 h = ( 60 * ( b - r ) / diff ) + 120;
502 } else {
503 h = ( 60 * ( r - g ) / diff ) + 240;
504 }
505
506 // chroma (diff) == 0 means greyscale which, by definition, saturation = 0%
507 // otherwise, saturation is based on the ratio of chroma (diff) to lightness (add)
508 if ( diff === 0 ) {
509 s = 0;
510 } else if ( l <= 0.5 ) {
511 s = diff / add;
512 } else {
513 s = diff / ( 2 - add );
514 }
515 return [ Math.round( h ) % 360, s, l, a == null ? 1 : a ];
516 };
517
518 spaces.hsla.from = function( hsla ) {
519 if ( hsla[ 0 ] == null || hsla[ 1 ] == null || hsla[ 2 ] == null ) {
520 return [ null, null, null, hsla[ 3 ] ];
521 }
522 var h = hsla[ 0 ] / 360,
523 s = hsla[ 1 ],
524 l = hsla[ 2 ],
525 a = hsla[ 3 ],
526 q = l <= 0.5 ? l * ( 1 + s ) : l + s - l * s,
527 p = 2 * l - q;
528
529 return [
530 Math.round( hue2rgb( p, q, h + ( 1 / 3 ) ) * 255 ),
531 Math.round( hue2rgb( p, q, h ) * 255 ),
532 Math.round( hue2rgb( p, q, h - ( 1 / 3 ) ) * 255 ),
533 a
534 ];
535 };
536
537
538 each( spaces, function( spaceName, space ) {
539 var props = space.props,
540 cache = space.cache,
541 to = space.to,
542 from = space.from;
543
544 // makes rgba() and hsla()
545 color.fn[ spaceName ] = function( value ) {
546
547 // generate a cache for this space if it doesn't exist
548 if ( to && !this[ cache ] ) {
549 this[ cache ] = to( this._rgba );
550 }
551 if ( value === undefined ) {
552 return this[ cache ].slice();
553 }
554
555 var ret,
556 type = getType( value ),
557 arr = ( type === "array" || type === "object" ) ? value : arguments,
558 local = this[ cache ].slice();
559
560 each( props, function( key, prop ) {
561 var val = arr[ type === "object" ? key : prop.idx ];
562 if ( val == null ) {
563 val = local[ prop.idx ];
564 }
565 local[ prop.idx ] = clamp( val, prop );
566 } );
567
568 if ( from ) {
569 ret = color( from( local ) );
570 ret[ cache ] = local;
571 return ret;
572 } else {
573 return color( local );
574 }
575 };
576
577 // makes red() green() blue() alpha() hue() saturation() lightness()
578 each( props, function( key, prop ) {
579
580 // alpha is included in more than one space
581 if ( color.fn[ key ] ) {
582 return;
583 }
584 color.fn[ key ] = function( value ) {
585 var local, cur, match, fn,
586 vtype = getType( value );
587
588 if ( key === "alpha" ) {
589 fn = this._hsla ? "hsla" : "rgba";
590 } else {
591 fn = spaceName;
592 }
593 local = this[ fn ]();
594 cur = local[ prop.idx ];
595
596 if ( vtype === "undefined" ) {
597 return cur;
598 }
599
600 if ( vtype === "function" ) {
601 value = value.call( this, cur );
602 vtype = getType( value );
603 }
604 if ( value == null && prop.empty ) {
605 return this;
606 }
607 if ( vtype === "string" ) {
608 match = rplusequals.exec( value );
609 if ( match ) {
610 value = cur + parseFloat( match[ 2 ] ) * ( match[ 1 ] === "+" ? 1 : -1 );
611 }
612 }
613 local[ prop.idx ] = value;
614 return this[ fn ]( local );
615 };
616 } );
617 } );
618
619 // add cssHook and .fx.step function for each named hook.
620 // accept a space separated string of properties
621 color.hook = function( hook ) {
622 var hooks = hook.split( " " );
623 each( hooks, function( _i, hook ) {
624 jQuery.cssHooks[ hook ] = {
625 set: function( elem, value ) {
626 var parsed;
627
628 if ( value !== "transparent" &&
629 ( getType( value ) !== "string" ||
630 ( parsed = stringParse( value ) ) ) ) {
631 value = color( parsed || value );
632 value = value.toRgbaString();
633 }
634 elem.style[ hook ] = value;
635 }
636 };
637 jQuery.fx.step[ hook ] = function( fx ) {
638 if ( !fx.colorInit ) {
639 fx.start = color( fx.elem, hook );
640 fx.end = color( fx.end );
641 fx.colorInit = true;
642 }
643 jQuery.cssHooks[ hook ].set( fx.elem, fx.start.transition( fx.end, fx.pos ) );
644 };
645 } );
646
647 };
648
649 color.hook( stepHooks );
650
651 jQuery.cssHooks.borderColor = {
652 expand: function( value ) {
653 var expanded = {};
654
655 each( [ "Top", "Right", "Bottom", "Left" ], function( _i, part ) {
656 expanded[ "border" + part + "Color" ] = value;
657 } );
658 return expanded;
659 }
660 };
661
662 // Basic color names only.
663 // Usage of any of the other color names requires adding yourself or including
664 // jquery.color.svg-names.js.
665 colors = jQuery.Color.names = {
666
667 // 4.1. Basic color keywords
668 aqua: "#00ffff",
669 black: "#000000",
670 blue: "#0000ff",
671 fuchsia: "#ff00ff",
672 gray: "#808080",
673 green: "#008000",
674 lime: "#00ff00",
675 maroon: "#800000",
676 navy: "#000080",
677 olive: "#808000",
678 purple: "#800080",
679 red: "#ff0000",
680 silver: "#c0c0c0",
681 teal: "#008080",
682 white: "#ffffff",
683 yellow: "#ffff00",
684
685 // 4.2.3. "transparent" color keyword
686 transparent: [ null, null, null, 0 ],
687
688 _default: "#ffffff"
689 };
690
691 } );