Home

Awesome

artboard

jquery.adaptive-backgrounds.js

A simple jQuery plugin to extract the dominant color of an image and apply it to the background of its parent element.

Check it out on the web!

OpenCollective Backers OpenCollective Sponsors

Getting Started

Install via bower:

bower install --save adaptive.background

Then simply include jQuery and the script in your page, and invoke it like so:

$(document).ready(function(){
  $.adaptiveBackground.run();
});

The script looks for image(s) with the data-adaptive-background attribute:

<img src="/image.jpg" data-adaptive-background>

Using an element with a CSS background image

Instead of using an <img> element nested inside of parent element, AB supports grabbing the dominant color of a background image of a standalone element, then applying the corresponding dominant color as the background color of said element.

Enable this functionality by adding a data property, data-ab-css-background to the element. See the example below:

<div style='background-image: url(/some-image.jpg)' data-adaptive-background data-ab-css-background></div>

Demo

Here's a little demo of how it works. (1) The page loads (2) the dominant background color of the image is extracted (3) said color is applied to parent of image. Demo drastically slowed down to show effect.


<img src="https://raw.github.com/briangonzalez/jquery.adaptive-background.js/master/misc/ab.gif">

API

This plugin exposes one method:

Default Options

Example: Call the run method, passing in any options you'd like to override.

var defaults      = {
  selector:             '[data-adaptive-background="1"]',
  parent:               null,
  exclude:              [ 'rgb(0,0,0)', 'rgba(255,255,255)' ],
  shadeVariation:   false,
  shadePercentage:  0,
  shadeColors:  {
    light:      'rgb(255,255,255)',
    dark:       'rgb(0,0,0)' 
  },
  normalizeTextColor:   false,
  normalizedTextColors:  {
    light:      "#fff",
    dark:       "#000"
  },
  lumaClasses:  {
    light:      "ab-light",
    dark:       "ab-dark"
  },
  transparent: null
};
$.adaptiveBackground.run(defaults)

Events Emitted

Example: Bind to the ab-color-found event like so:

$('img.my-image').on('ab-color-found', function(ev,payload){
  console.log(payload.color);   // The dominant color in the image.
  console.log(payload.palette); // The color palette found in the image.
  console.log(ev);   // The jQuery.Event object
});

Success Callback

You may wish to supply a callback function which is called once the magic has been performed.

$.adaptiveBackground.run({
  success: function($img, data) {
    console.log('Success!', $img, data);
  }
});

Note, this callback is called once for each image.

Caveats

This plugin utlizes the <canvas> element and the ImageData object, and due to cross-site security limitations, the script will fail if one tries to extract the colors from an image not hosted on the current domain, unless the image allows for Cross Origin Resource Sharing.

Enabling CORS on S3

To enable CORS for images hosted on S3 buckets, follow the Amazon guide here; adding the following to the bucket's CORS configuration:

<CORSRule>
 <AllowedOrigin>*</AllowedOrigin>
 <AllowedMethod>GET</AllowedMethod>
</CORSRule>

For all images, you can optionally also include a cross-origin attribute in your image. This is not absolutely necessary since the anonymous origin is set in the Javascript code, but kudos to you for being a super-developer.

<img src="/image.jpg" data-adaptive-background cross-origin="anonymous"/>

Credit

This plugin is built on top of a script called RGBaster.

Collaborators

Brian GonzalezScott SternAlfred J Kwack
Brian GonzalezScott SternAlfred J Kwack

This project exists thanks to all the people who contribute. [Contribute]. <a href="graphs/contributors"><img src="https://opencollective.com/jquery-adaptive-backgrounds/contributors.svg?width=890" /></a>

Backers

Thank you to all our backers! 🙏 [Become a backer]

<a href="https://opencollective.com/jquery-adaptive-backgrounds#backers" target="_blank"><img src="https://opencollective.com/jquery-adaptive-backgrounds/backers.svg?width=890"></a>

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

<a href="https://opencollective.com/jquery-adaptive-backgrounds/sponsor/0/website" target="_blank"><img src="https://opencollective.com/jquery-adaptive-backgrounds/sponsor/0/avatar.svg"></a> <a href="https://opencollective.com/jquery-adaptive-backgrounds/sponsor/1/website" target="_blank"><img src="https://opencollective.com/jquery-adaptive-backgrounds/sponsor/1/avatar.svg"></a> <a href="https://opencollective.com/jquery-adaptive-backgrounds/sponsor/2/website" target="_blank"><img src="https://opencollective.com/jquery-adaptive-backgrounds/sponsor/2/avatar.svg"></a> <a href="https://opencollective.com/jquery-adaptive-backgrounds/sponsor/3/website" target="_blank"><img src="https://opencollective.com/jquery-adaptive-backgrounds/sponsor/3/avatar.svg"></a> <a href="https://opencollective.com/jquery-adaptive-backgrounds/sponsor/4/website" target="_blank"><img src="https://opencollective.com/jquery-adaptive-backgrounds/sponsor/4/avatar.svg"></a> <a href="https://opencollective.com/jquery-adaptive-backgrounds/sponsor/5/website" target="_blank"><img src="https://opencollective.com/jquery-adaptive-backgrounds/sponsor/5/avatar.svg"></a> <a href="https://opencollective.com/jquery-adaptive-backgrounds/sponsor/6/website" target="_blank"><img src="https://opencollective.com/jquery-adaptive-backgrounds/sponsor/6/avatar.svg"></a> <a href="https://opencollective.com/jquery-adaptive-backgrounds/sponsor/7/website" target="_blank"><img src="https://opencollective.com/jquery-adaptive-backgrounds/sponsor/7/avatar.svg"></a> <a href="https://opencollective.com/jquery-adaptive-backgrounds/sponsor/8/website" target="_blank"><img src="https://opencollective.com/jquery-adaptive-backgrounds/sponsor/8/avatar.svg"></a> <a href="https://opencollective.com/jquery-adaptive-backgrounds/sponsor/9/website" target="_blank"><img src="https://opencollective.com/jquery-adaptive-backgrounds/sponsor/9/avatar.svg"></a>

License

MIT, yo.