Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de07a9e
commit 54fffcf
Showing
1 changed file
with
370 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,370 @@ | ||
<!DOCTYPE HTML> | ||
|
||
<html> | ||
|
||
<head> | ||
<meta charset="UTF-8" /> | ||
|
||
<title>Cross-Browser CSS3 Rule Generator</title> | ||
|
||
<script type="text/javascript"> | ||
|
||
cssMath = { | ||
/* ---- General ---- */ | ||
|
||
/* Number rounded by Length */ | ||
round: function (n, l) { | ||
l = Math.pow(10, l) || 10; | ||
return Math.round(n * l) / l; | ||
}, | ||
|
||
/* ---- Rotation Adjustments ---- */ | ||
|
||
/* X and Y coordinates to rotation and strength */ | ||
xy2rs: function (x, y) { | ||
return { | ||
r: this.round(Math.atan2(x, y * -1) * 180 / Math.PI, 3), | ||
s: this.round(Math.sqrt((x * x) + (y * y)), 3) | ||
}; | ||
}, | ||
|
||
/* Rotation and Strength to x and y coordinates */ | ||
rs2xy: function (r, s) { | ||
return { | ||
x: this.round(Math.sin(r * Math.PI / 180) * s, 3), | ||
y: this.round(Math.cos(r * Math.PI / 180) * s * -1, 3) | ||
}; | ||
}, | ||
|
||
/* Rotation to degree */ | ||
r2d: function (r) { | ||
return this.round(r * 90, 4); | ||
}, | ||
|
||
/* Degree to rotation */ | ||
d2r: function (d) { | ||
return this.round(d / 90, 4); | ||
}, | ||
|
||
/* ---- Color Adjustments ---- */ | ||
|
||
/* Long Hexadecimals compressed to short hexadecimals */ | ||
lh2sh: function (lh) { | ||
return lh.replace(/^\s+|\s+$/g, '').replace(/^#?([A-f0-9])\1([A-f0-9])\2([A-f0-9])\3([A-f0-9])\4$/, '#$1$2$3$4').replace(/^#?([A-f0-9])\1([A-f0-9])\2([A-f0-9])\3$/, '#$1$2$3'); | ||
}, | ||
|
||
/* Short Hexadecimals expanded to long hexadecimals */ | ||
sh2lh: function (sh) { | ||
return sh.replace(/^\s+|\s+$/g, '').replace(/^#?([A-f0-9])([A-f0-9])([A-f0-9])([A-f0-9])$/, '#$1$1$2$2$3$3$4$4').replace(/^#?([A-f0-9])([A-f0-9])([A-f0-9])$/, '#$1$1$2$2$3$3'); | ||
}, | ||
|
||
/* Channel to hexadecimal */ | ||
c2h: function (c) { | ||
return ('0' + parseInt(c, 10).toString(16)).substr(-2).toUpperCase(); | ||
}, | ||
|
||
/* Hexadecimal to channel */ | ||
h2c: function (h) { | ||
return '' + parseInt(h, 16); | ||
}, | ||
|
||
/* Channels to array */ | ||
c2a: function (c) { | ||
return (c = /(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/.exec(c) || /(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/.exec(c)) && c.shift() ? c : false; | ||
}, | ||
|
||
/* Hexadecimals to array */ | ||
h2a: function (h) { | ||
h = this.sh2lh(h); | ||
return (h = /([A-f0-9]{2})([A-f0-9]{2})([A-f0-9]{2})([A-f0-9]{2})/.exec(h) || /([A-f0-9]{2})([A-f0-9]{2})([A-f0-9]{2})/.exec(h)) && h.shift() ? h : false; | ||
}, | ||
|
||
/* Channels to hexadecimals */ | ||
c2h2: function (c) { | ||
c = this.c2a(c); | ||
return this.hf((c[3] ? this.c2h(c[3]) : '') + this.c2h(c[0]) + this.c2h(c[1]) + this.c2h(c[2])); | ||
}, | ||
|
||
/* Hexadecimals to channels */ | ||
h2c2: function (h) { | ||
h = this.h2a(h); | ||
return this.cf((h[3] ? [this.h2c(h[1]), this.h2c(h[2]), this.h2c(h[3]), this.h2c(h[0])] : [this.h2c(h[0]), this.h2c(h[1]), this.h2c(h[2])]).join(', ')); | ||
}, | ||
|
||
/* Channels proper formatting */ | ||
cf: function (c) { | ||
c = this.c2a(c); | ||
return (c[3] ? 'rgba(' : 'rgb(') + c.join(', ') + ')'; | ||
}, | ||
|
||
/* Hexadecimals proper formatting */ | ||
hf: function (h) { | ||
return '#' + this.h2a(h).join(''); | ||
}, | ||
|
||
/* Channel Move */ | ||
cm: function (c, m) { | ||
return Math.max(Math.min(parseInt(c, 10) + m, 255), 0) + ''; | ||
}, | ||
|
||
/* Hexadecimal Move */ | ||
hm: function (h, m) { | ||
return this.c2h(Math.max(Math.min(parseInt(h, 16) + m, 255), 0)); | ||
}, | ||
|
||
/* Channels Move */ | ||
cm2: function (c, m) { | ||
var e = -1; | ||
c = this.c2a(c); | ||
while (++e < c.length) { | ||
c[e] = this.cm(c[e], m); | ||
} | ||
return this.cf(c.join(',')); | ||
}, | ||
|
||
/* Hexadecimals Move */ | ||
hm2: function (h, m) { | ||
var e = -1; | ||
h = this.h2a(h); | ||
while (++e < h.length) { | ||
h[e] = this.hm(h[e], m); | ||
} | ||
return this.hf(h.join('')); | ||
}, | ||
|
||
/* String to short hexadecimals */ | ||
s2sh: function (s) { | ||
return this.lh2sh((s.indexOf(',') > -1) ? this.c2h2(s) : s); | ||
}, | ||
|
||
/* String to long hexadecimals */ | ||
s2lh: function (s) { | ||
return this.sh2lh((s.indexOf(',') > -1) ? this.c2h2(s) : s); | ||
}, | ||
|
||
/* String to channels */ | ||
s2c: function (s) { | ||
return (s.indexOf(',') > -1) ? this.cf(s) : this.h2c2(s); | ||
}, | ||
|
||
/* String to degree */ | ||
s2d: function (s) { | ||
return (s > 4) ? s : this.round(this.r2d(s), 3); | ||
}, | ||
|
||
/* String to channels, short hexadecimals, or units Moved */ | ||
s2xm: function (s, m) { | ||
if (/^[#A-f0-9\s]+$/.test(s)) { | ||
return this.lh2sh(this.hm2(s, m)); | ||
} | ||
else if (s.indexOf(',') > -1) { | ||
return this.cm2(s, m); | ||
} | ||
else if ((s = /([\+\-\d]+)(.*)/.exec(s))) { | ||
return parseInt(s[1], 10) + m + s[2]; | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
}; | ||
|
||
/* ---- cssMath Firebug tests ---- */ | ||
|
||
console.log( | ||
'Testing round: function (n, l)', '\n', | ||
'Number rounded by Length', '\n', | ||
'cssMath.round(3.1417534)', 'returns: ', cssMath.round(3.1417534), '\n', | ||
'cssMath.round(3.1417534, 2)', 'returns: ', cssMath.round(3.1417534, 2), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing xy2rs: function (x, y)', '\n', | ||
'X and Y coordinates to rotation and strength', '\n', | ||
'cssMath.xy2rs(4, 4)', 'returns: ', cssMath.xy2rs(4, 4), '\n', | ||
'cssMath.xy2rs(10, 15)', 'returns: ', cssMath.xy2rs(10, 15), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing rs2xy: function (r, s)', '\n', | ||
'Rotation and Strength to x and y coordinates', '\n', | ||
'cssMath.rs2xy(135, 5.657)', 'returns: ', cssMath.rs2xy(135, 5.657), '\n', | ||
'cssMath.rs2xy(146.31, 18.028)', 'returns: ', cssMath.rs2xy(146.31, 18.028), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing r2d: function (r)', '\n', | ||
'Rotation to degree', '\n', | ||
'cssMath.r2d(3)', 'returns: ', cssMath.r2d(3), '\n', | ||
'cssMath.r2d(3.5)', 'returns: ', cssMath.r2d(3.5), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing d2r: function (d)', '\n', | ||
'Degree to rotation', '\n', | ||
'cssMath.d2r(270)', 'returns: ', cssMath.d2r(270), '\n', | ||
'cssMath.d2r(315)', 'returns: ', cssMath.d2r(315), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing lh2sh: function (lh)', '\n', | ||
'Long Hexadecimals compressed to short hexadecimals', '\n', | ||
'cssMath.lh2sh(\'FF33FF\')', 'returns: ', cssMath.lh2sh('FF33FF'), '\n', | ||
'cssMath.lh2sh(\'#FF33FF\')', 'returns: ', cssMath.lh2sh('#FF33FF'), '\n', | ||
'cssMath.lh2sh(\'#F3F\')', 'returns: ', cssMath.lh2sh('#F3F'), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing sh2lh: function (sh)', '\n', | ||
'Short Hexadecimals expanded to long hexadecimals', '\n', | ||
'cssMath.sh2lh(\'F3F\')', 'returns: ', cssMath.sh2lh('F3F'), '\n', | ||
'cssMath.lh2sh(\'#F3F\')', 'returns: ', cssMath.sh2lh('#F3F'), '\n', | ||
'cssMath.lh2sh(\'#FF33FF\')', 'returns: ', cssMath.sh2lh('#FF33FF'), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing c2h: function (c)', '\n', | ||
'Channel to hexadecimal', '\n', | ||
'cssMath.c2h(255)', 'returns: ', cssMath.c2h(255), '\n', | ||
'cssMath.c2h(0)', 'returns: ', cssMath.c2h(0), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing h2c: function (c)', '\n', | ||
'Hexadecimal to channel', '\n', | ||
'cssMath.h2c(\'FF\')', 'returns: ', cssMath.h2c('FF'), '\n', | ||
'cssMath.h2c(\'00\')', 'returns: ', cssMath.h2c('00'), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing c2a: function (c)', '\n', | ||
'Channels to array', '\n', | ||
'cssMath.c2a(\'255, 0, 255\')', 'returns: ', cssMath.c2a('255, 0, 255'), '\n', | ||
'cssMath.c2a(\'255,0,255\')', 'returns: ', cssMath.c2a('255,0,255'), '\n', | ||
'cssMath.c2a(\'(255,0,255)\')', 'returns: ', cssMath.c2a('(255,0,255)'), '\n', | ||
'cssMath.c2a(\'rgb(255,0,255)\')', 'returns: ', cssMath.c2a('rgb(255,0,255)'), '\n', | ||
'cssMath.c2a(\'rgb ( 255 , 0 , 255 )\')', 'returns: ', cssMath.c2a('rgb ( 255 , 0 , 255 )'), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing h2a: function (c)', '\n', | ||
'Hexadecimals to array', '\n', | ||
'cssMath.h2a(\'FF00FF\')', 'returns: ', cssMath.h2a('FF00FF'), '\n', | ||
'cssMath.h2a(\'#FF00FF\')', 'returns: ', cssMath.h2a('#FF00FF'), '\n', | ||
'cssMath.h2a(\'#F0F\')', 'returns: ', cssMath.h2a('#F0F'), '\n', | ||
'cssMath.h2a(\'F0F\')', 'returns: ', cssMath.h2a('F0F'), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing c2h2: function (c)', '\n', | ||
'Channels to hexadecimals', '\n', | ||
'cssMath.c2h2(\'255, 0, 255\')', 'returns: ', cssMath.c2h2('255, 0, 255'), '\n', | ||
'cssMath.c2h2(\'255,0,255\')', 'returns: ', cssMath.c2h2('255,0,255'), '\n', | ||
'cssMath.c2h2(\'(255,0,255)\')', 'returns: ', cssMath.c2h2('(255,0,255)'), '\n', | ||
'cssMath.c2h2(\'rgb(255,0,255)\')', 'returns: ', cssMath.c2h2('rgb(255,0,255)'), '\n', | ||
'cssMath.c2h2(\'rgb ( 255 , 0 , 255 )\')', 'returns: ', cssMath.c2h2('rgb ( 255 , 0 , 255 )'), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing h2c2: function (h)', '\n', | ||
'Hexadecimals to channels', '\n', | ||
'cssMath.h2c2(\'FF00FF\')', 'returns: ', cssMath.h2c2('FF00FF'), '\n', | ||
'cssMath.h2c2(\'#FF00FF\')', 'returns: ', cssMath.h2c2('#FF00FF'), '\n', | ||
'cssMath.h2c2(\'#F0F\')', 'returns: ', cssMath.h2c2('#F0F'), '\n', | ||
'cssMath.h2c2(\'F0F\')', 'returns: ', cssMath.h2c2('F0F'), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing cf: function (c)', '\n', | ||
'Channels proper formatting', '\n', | ||
'cssMath.cf(\'255, 0, 255\')', 'returns: ', cssMath.cf('255, 0, 255'), '\n', | ||
'cssMath.cf(\'255,0,255\')', 'returns: ', cssMath.cf('255,0,255'), '\n', | ||
'cssMath.cf(\'(255,0,255)\')', 'returns: ', cssMath.cf('(255,0,255)'), '\n', | ||
'cssMath.cf(\'rgb(255,0,255)\')', 'returns: ', cssMath.cf('rgb(255,0,255)'), '\n', | ||
'cssMath.cf(\'rgb ( 255 , 0 , 255 )\')', 'returns: ', cssMath.cf('rgb ( 255 , 0 , 255 )'), '\n', | ||
'cssMath.cf(\'255, 0, 255, 204\')', 'returns: ', cssMath.cf('255, 0, 255, 204'), '\n', | ||
'cssMath.cf(\'255,0,255,204\')', 'returns: ', cssMath.cf('255,0,255,204'), '\n', | ||
'cssMath.cf(\'(255,0,255,204)\')', 'returns: ', cssMath.cf('(255,0,255,204)'), '\n', | ||
'cssMath.cf(\'rgb(255,0,255,204)\')', 'returns: ', cssMath.cf('rgb(255,0,255,204)'), '\n', | ||
'cssMath.cf(\'rgb ( 255 , 0 , 255 , 204)\')', 'returns: ', cssMath.cf('rgb ( 255 , 0 , 255 , 204)'), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing hf: function (h)', '\n', | ||
'Hexadecimals proper formatting', '\n', | ||
'cssMath.hf(\'FF00FF\')', 'returns: ', cssMath.hf('FF00FF'), '\n', | ||
'cssMath.hf(\'#FF00FF\')', 'returns: ', cssMath.hf('#FF00FF'), '\n', | ||
'cssMath.hf(\'#F0F\')', 'returns: ', cssMath.hf('#F0F'), '\n', | ||
'cssMath.hf(\'F0F\')', 'returns: ', cssMath.hf('F0F'), '\n', | ||
'cssMath.hf(\'CCFF00FF\')', 'returns: ', cssMath.hf('CCFF00FF'), '\n', | ||
'cssMath.hf(\'CF0F\')', 'returns: ', cssMath.hf('CF0F'), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing cm: function (c, m)', '\n', | ||
'Channel Move', '\n', | ||
'cssMath.cm(254, 1)', 'returns: ', cssMath.cm(254, 1), '\n', | ||
'cssMath.cm(253, 2)', 'returns: ', cssMath.cm(253, 2), '\n', | ||
'cssMath.cm(255, -51)', 'returns: ', cssMath.cm(255, -51), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing hm: function (h, m)', '\n', | ||
'Hexadecimal Move', '\n', | ||
'cssMath.hm(\'FE\', 1)', 'returns: ', cssMath.hm('FE', 1), '\n', | ||
'cssMath.hm(\'FD\', 2)', 'returns: ', cssMath.hm('FD', 2), '\n', | ||
'cssMath.hm(\'FF\', -51)', 'returns: ', cssMath.hm('FF', -51), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing cm2: function (c, m)', '\n', | ||
'Channels Move', '\n', | ||
'cssMath.cm2(\'254, 254, 254\', 1)', 'returns: ', cssMath.cm2('254, 254, 254', 1), '\n', | ||
'cssMath.cm2(\'253, 253, 253\', 2)', 'returns: ', cssMath.cm2('253, 253, 253', 2), '\n', | ||
'cssMath.cm2(\'255, 255, 255\', -51)', 'returns: ', cssMath.cm2('255, 255, 255', -51), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing hm2: function (h, m)', '\n', | ||
'Hexadecimals Move', '\n', | ||
'cssMath.hm2(\'FEFEFE\', 1)', 'returns: ', cssMath.hm2('FEFEFE', 1), '\n', | ||
'cssMath.hm2(\'FDFDFD\', 2)', 'returns: ', cssMath.hm2('FDFDFD', 2), '\n', | ||
'cssMath.hm2(\'FFFFFF\', -51)', 'returns: ', cssMath.hm2('FFFFFF', -51), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing s2sh: function (s)', '\n', | ||
'String to short hexadecimal', '\n', | ||
'cssMath.s2sh(\'255, 255, 255\', 1)', 'returns: ', cssMath.s2sh('255, 255, 255'), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing s2lh: function (s)', '\n', | ||
'String to long hexadecimal', '\n', | ||
'cssMath.s2lh(\'255, 51, 255\', 1)', 'returns: ', cssMath.s2lh('255, 51, 255'), '\n', | ||
'cssMath.s2lh(\'255, 51, 255, 204\', 1)', 'returns: ', cssMath.s2lh('255, 51, 255, 204'), '\n', | ||
'cssMath.s2lh(\'rgb (255, 51, 255, 204)\', 1)', 'returns: ', cssMath.s2lh('rgb (255, 51, 255, 204)'), '\n', | ||
'cssMath.s2lh(\'rgba (255, 51, 255)\', 1)', 'returns: ', cssMath.s2lh('rgba (255, 51, 255)'), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing s2c: function (s)', '\n', | ||
'String to channels', '\n', | ||
'cssMath.s2c(\'FF33FF\', 1)', 'returns: ', cssMath.s2c('FF33FF'), '\n', | ||
'cssMath.s2c(\'#FF33FF\', 1)', 'returns: ', cssMath.s2c('#FF33FF'), '\n', | ||
'cssMath.s2c(\'CCFF33FF\', 1)', 'returns: ', cssMath.s2c('CCFF33FF'), '\n', | ||
'cssMath.s2c(\'#CCFF33FF\', 1)', 'returns: ', cssMath.s2c('#CCFF33FF'), '\n' | ||
); | ||
|
||
console.log( | ||
'Testing s2xm: function (s)', '\n', | ||
'String to channels, hexadecimals, or units Moved', '\n', | ||
'cssMath.s2xm(\'FFFEFF\', 1)', 'returns: ', cssMath.s2xm('FFFEFF', 1), '\n' | ||
); | ||
|
||
</script> | ||
</head> | ||
|
||
<body></body> | ||
|
||
</html> |