PageSourceSearch

https://youslade.com/themes/wondertag/javascript/jquery.adaptive-backgrounds.js

js youslade.com collected 2026-09-25 09:16:16 UTC 4,243 bytes, 86 lines download raw bytes

1;(function($){
2  var DATA_COLOR    = 'data-ab-color';
3  var DATA_PARENT   = 'data-ab-parent';
4  var DATA_CSS_BG   = 'data-ab-css-background';
5  var EVENT_CF      = 'ab-color-found';
6
7  var DEFAULTS      = {
8    selector:             '[data-adaptive-background="1"]',
9    parent:               null,
10    exclude:              [ 'rgb(0,0,0)', 'rgba(255,255,255)' ],
11    normalizeTextColor:   true,
12    normalizedTextColors:  {
13      light:      "#fff",
14      dark:       "#000"
15    },
16    lumaClasses:  {
17      light:      "ab-light",
18      dark:       "ab-dark"
19    }
20  };
21
22  // Include RGBaster - https://github.com/briangonzalez/rgbaster.js
23  !function(n){"use strict";var t=function(){return document.createElement("canvas").getContext("2d")},e=function(n,e){var a=new Image,o=n.src||n;"data:"!==o.substring(0,5)&&(a.crossOrigin="Anonymous"),a.onload=function(){var n=t("2d");n.drawImage(a,0,0);var o=n.getImageData(0,0,a.width,a.height);e&&e(o.data)},a.src=o},a=function(n){return["rgb(",n,")"].join("")},o=function(n){return n.map(function(n){return a(n.name)})},r=5,i=10,c={};c.colors=function(n,t){t=t||{};var c=t.exclude||[],u=t.paletteSize||i;e(n,function(e){for(var i=n.width*n.height||e.length,m={},s="",d=[],f={dominant:{name:"",count:0},palette:Array.apply(null,new Array(u)).map(Boolean).map(function(){return{name:"0,0,0",count:0}})},l=0;i>l;){if(d[0]=e[l],d[1]=e[l+1],d[2]=e[l+2],s=d.join(","),m[s]=s in m?m[s]+1:1,-1===c.indexOf(a(s))){var g=m[s];g>f.dominant.count?(f.dominant.name=s,f.dominant.count=g):f.palette.some(function(n){return g>n.count?(n.name=s,n.count=g,!0):void 0})}l+=4*r}if(t.success){var p=o(f.palette);t.success({dominant:a(f.dominant.name),secondary:p[0],palette:p})}})},n.RGBaster=n.RGBaster||c}(window);
24
25  $.adaptiveBackground = {
26    run: function( options ){
27      var opts = $.extend({}, DEFAULTS, options);
28      $( opts.selector ).each(function(index, el){
29        var $this = $(this);
30        var handleColors = function () {
31          var img = useCSSBackground() ? getCSSBackground() : $this[0];
32          RGBaster.colors(img, {
33            paletteSize: 20,
34            exclude: opts.exclude,
35            success: function(colors) {
36              $this.attr(DATA_COLOR, colors.dominant);
37              $this.trigger(EVENT_CF, { color: colors.dominant, palette: colors.palette });
38            }
39          });
40        };
41        var useCSSBackground = function(){
42          return $this.attr( DATA_CSS_BG );
43        };
44        var getCSSBackground = function(){
45          return $this.css('background-image')
46                      .replace('url(','').replace(')','');
47        };
48        $this.on( EVENT_CF, function(ev, data){
49          var $parent;
50          if ( opts.parent && $this.parents( opts.parent ).length ) {
51            $parent = $this.parents( opts.parent );
52          }
53          else if ( $this.attr( DATA_PARENT ) && $this.parents( $this.attr( DATA_PARENT ) ).length ){
54            $parent = $this.parents( $this.attr( DATA_PARENT ) );
55          }
56          else if ( useCSSBackground() ){
57            $parent = $this;
58          }
59          else if (opts.parent) {
60            $parent = $this.parents( opts.parent );
61          }
62          else {
63            $parent = $this.parent();
64          }
65          $parent.css({ backgroundColor: data.color });
66          var getYIQ = function(color){
67            var rgb = data.color.match(/\d+/g);
68            return ((rgb[0]*299)+(rgb[1]*587)+(rgb[2]*114))/1000;
69          };
70          var getNormalizedTextColor = function (color){
71            return getYIQ(color) >= 128 ? opts.normalizedTextColors.dark : opts.normalizedTextColors.light;
72          };
73          var getLumaClass = function (color){
74            return getYIQ(color) <= 128 ? opts.lumaClasses.dark : opts.lumaClasses.light;
75          };
76          if ( opts.normalizeTextColor )
77            $parent.css({ color: getNormalizedTextColor(data.color) });
78          $parent.addClass( getLumaClass(data.color) )
79                 .attr('data-ab-yaq', getYIQ(data.color));
80          opts.success && opts.success($this, data);
81        });
82        handleColors();
83      });
84    },
85  };
86})(jQuery);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.