Skip to content

Commit 9913b63

Browse files
committed
Change parents txt color according backgorund
1 parent c483816 commit 9913b63

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

dist/jquery.adaptive-background.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/jquery.adaptive-background.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
var DEFAULTS = {
1212
selector: 'img[data-adaptive-background="1"]',
1313
parent: null,
14+
changeTxtColor: false,
15+
parentTxtLight: "#fff",
16+
parentTxtDark: "#000",
1417
};
1518

1619
// Include RGBaster - https://github.com/briangonzalez/rgbaster.js
@@ -40,6 +43,7 @@
4043

4144
/* Subscribe to our color-found event. */
4245
$this.on( EVENT_CF, function(ev, data){
46+
var $data = data;
4347
var $parent;
4448
if ( $this.attr( DATA_PARENT ) ){
4549
$parent = $this.parents( $this.attr( DATA_PARENT ) );
@@ -52,6 +56,17 @@
5256
}
5357

5458
$parent.css({ backgroundColor: data.color });
59+
60+
if (opts.changeTxtColor){
61+
/* Small helper function for calculating color contrast */
62+
var getTxtColor = function (){
63+
var rgb = $data.color.match(/\d+/g);
64+
var yiq = ((rgb[0]*299)+(rgb[1]*587)+(rgb[2]*114))/1000;
65+
return yiq >= 128 ? opts.parentTxtDark : opts.parentTxtLight;
66+
};
67+
68+
$parent.css({ color: getTxtColor });
69+
}
5570
});
5671

5772
/* Handle the colors. */
@@ -61,4 +76,4 @@
6176
},
6277
};
6378

64-
})(jQuery);
79+
})(jQuery);

0 commit comments

Comments
 (0)