+ imp. version

This commit is contained in:
kelson42 2013-11-09 15:34:48 +01:00
parent 40acdfc252
commit c451e14479

View File

@ -1,75 +1,8 @@
var Toggled; var Toggled;
function initCss() { // before getting into regexps, try simple matches
var colorProperties = ['color', 'background-color', 'background']; // and overwrite the input
var simple_colors = {
$('*').each(function () {
for (var prop in colorProperties) {
prop = colorProperties[prop];
if (($(this).css(prop) === 'rgba(0, 0, 0, 0)') || ($(this).css(prop) === 'transparent')) {
if ($(this).is('body')) {
$(this).css('background', 'rgb(255, 255, 255)');
$(this).css('color', 'rgb(0, 0, 0)');
} else
if (!$(this).is('a'))
$(this).css('color', 'rgb(0, 0, 0)');
}
}
});
}
function invertColors() {
//First init for other elements
if (typeof Toggled === 'undefined') {
initCss();
Toggled = true;
}
var colorProperties = ['color', 'background-color', 'background'];
$('*').each(function () {
var color = null;
for (var prop in colorProperties) {
prop = colorProperties[prop];
if (!$(this).css(prop)) continue;
color = new RGBColor($(this).css(prop));
if (color.ok) {
$(this).css(prop, 'rgb(' + (255 - color.r) + ',' + (255 - color.g) + ',' + (255 - color.b) + ')');
}
color = null;
}
});
}
/**
* A class to parse color values
* @author Stoyan Stefanov <sstoo@gmail.com>
* @link http://www.phpied.com/rgb-color-parser-in-javascript/
* @license Use it if you like it
*/
function RGBColor(color_string) {
this.ok = false;
// strip any leading #
if (color_string.charAt(0) == '#') { // remove # if any
color_string = color_string.substr(1, 6);
}
color_string = color_string.replace(/ /g, '');
color_string = color_string.toLowerCase();
// before getting into regexps, try simple matches
// and overwrite the input
var simple_colors = {
aliceblue: 'f0f8ff', aliceblue: 'f0f8ff',
antiquewhite: 'faebd7', antiquewhite: 'faebd7',
@ -214,16 +147,13 @@ function RGBColor(color_string) {
whitesmoke: 'f5f5f5', whitesmoke: 'f5f5f5',
yellow: 'ffff00', yellow: 'ffff00',
yellowgreen: '9acd32' yellowgreen: '9acd32'
}; };
for (var key in simple_colors) {
if (color_string == key) {
color_string = simple_colors[key];
}
}
// emd of simple type-in colors
// array of color definition objects var colorCache = new Object();
var color_defs = [{ var rgbCache = new Object();
// array of color definition objects
var color_defs = [{
re: /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/, re: /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/,
example: ['rgb(123, 234, 45)', 'rgb(255,234,245)'], example: ['rgb(123, 234, 45)', 'rgb(255,234,245)'],
process: function (bits) { process: function (bits) {
@ -233,7 +163,7 @@ function RGBColor(color_string) {
parseInt(bits[3]) parseInt(bits[3])
]; ];
} }
}, { }, {
re: /^(\w{2})(\w{2})(\w{2})$/, re: /^(\w{2})(\w{2})(\w{2})$/,
example: ['#00ff00', '336699'], example: ['#00ff00', '336699'],
process: function (bits) { process: function (bits) {
@ -243,7 +173,7 @@ function RGBColor(color_string) {
parseInt(bits[3], 16) parseInt(bits[3], 16)
]; ];
} }
}, { }, {
re: /^(\w{1})(\w{1})(\w{1})$/, re: /^(\w{1})(\w{1})(\w{1})$/,
example: ['#fb0', 'f0f'], example: ['#fb0', 'f0f'],
process: function (bits) { process: function (bits) {
@ -253,7 +183,83 @@ function RGBColor(color_string) {
parseInt(bits[3] + bits[3], 16) parseInt(bits[3] + bits[3], 16)
]; ];
} }
}]; }];
var colorProperties = ['color', 'background-color', 'background'];
function initCss() {
$('*').each(function () {
for (var prop in colorProperties) {
prop = colorProperties[prop];
if (($(this).css(prop) === 'rgba(0, 0, 0, 0)') || ($(this).css(prop) === 'transparent')) {
if ($(this).is('body')) {
$(this).css('background', 'rgb(255, 255, 255)');
$(this).css('color', 'rgb(0, 0, 0)');
} else
if (!$(this).is('a'))
$(this).css('color', 'rgb(0, 0, 0)');
}
}
});
}
function invertColors() {
//First init for other elements
if (typeof Toggled === 'undefined') {
initCss();
Toggled = true;
}
var color;
var prop;
var cssProp;
$('*').each(function () {
for (prop in colorProperties) {
prop = colorProperties[prop];
if ($(this).css(prop)) {
cssProp = $(this).css(prop);
if (colorCache[cssProp] != undefined) {
$(this).css(prop, colorCache[cssProp]);
} else {
color = new RGBColor(cssProp);
if (color.ok) {
$(this).css(prop, 'rgb(' + (255 - color.r) + ',' + (255 - color.g) + ',' + (255 - color.b) + ')');
}
colorCache[cssProp] = $(this).css(prop);
}
}
}
});
}
/**
* A class to parse color values
* @author Stoyan Stefanov <sstoo@gmail.com>
* @link http://www.phpied.com/rgb-color-parser-in-javascript/
* @license Use it if you like it
*/
function RGBColor(color_string) {
this.ok = false;
// strip any leading #
if (color_string.charAt(0) == '#') { // remove # if any
color_string = color_string.substr(1, 6);
}
color_string = color_string.replace(/ /g, '');
color_string = color_string.toLowerCase();
for (var key in simple_colors) {
if (color_string == key) {
color_string = simple_colors[key];
}
}
// emd of simple type-in colors
// search through the definitions to find a match // search through the definitions to find a match
for (var i = 0; i < color_defs.length; i++) { for (var i = 0; i < color_defs.length; i++) {