tinygradient
Advanced tools
Comparing version
{ | ||
"name": "tinygradient", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"authors": [{ | ||
@@ -13,3 +13,3 @@ "name": "Damien \"Mistic\" Sorel", | ||
"dependencies": { | ||
"tinycolor": "0.11.x" | ||
"tinycolor": "~1.0.0" | ||
}, | ||
@@ -16,0 +16,0 @@ "keywords": [ |
{ | ||
"name": "tinygradient", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"authors": [{ | ||
@@ -13,9 +13,9 @@ "name": "Damien \"Mistic\" Sorel", | ||
"dependencies": { | ||
"tinycolor2": "~0.10.0" | ||
"tinycolor2": "~1.0.0" | ||
}, | ||
"devDependencies": { | ||
"grunt": "~0.4.5", | ||
"grunt-contrib-uglify": "~0.4.0", | ||
"grunt-contrib-uglify": "~0.6.0", | ||
"grunt-contrib-jshint": "~0.10.0", | ||
"grunt-mocha-test": "~0.11.0" | ||
"grunt-mocha-test": "~0.12.0" | ||
}, | ||
@@ -22,0 +22,0 @@ "keywords": [ |
@@ -32,2 +32,12 @@ var tinygradient = require('../tinygradient.js'), | ||
it('should reverse gradient', function() { | ||
var grad1 = tinygradient('red', 'green', 'blue', 'yellow', 'black'); | ||
var grad2 = grad1.reverse(); | ||
assert.deepEqual( | ||
grad1.stops.map(function(c) { return c.color.toRgb(); }), | ||
grad2.stops.reverse().map(function(c) { return c.color.toRgb(); }) | ||
); | ||
}); | ||
it('should generate 11 steps gradient from black to grey in RGB', function() { | ||
@@ -34,0 +44,0 @@ var grad = tinygradient({r:0,g:0,b:0}, {r:100,g:100,b:100}); |
@@ -150,12 +150,16 @@ /*! | ||
var totalSubsteps = 1; | ||
for (var n=l-1; n--; ) totalSubsteps+= substeps[n]; | ||
for (var n=l-1; n--;) totalSubsteps+= substeps[n]; | ||
if (totalSubsteps < steps) { | ||
var min = Math.min.apply(null, substeps); | ||
substeps[substeps.indexOf(min)]++; | ||
while (totalSubsteps != steps) { | ||
if (totalSubsteps < steps) { | ||
var min = Math.min.apply(null, substeps); | ||
substeps[substeps.indexOf(min)]++; | ||
totalSubsteps++; | ||
} | ||
else { | ||
var max = Math.max.apply(null, substeps); | ||
substeps[substeps.indexOf(max)]--; | ||
totalSubsteps--; | ||
} | ||
} | ||
else if (totalSubsteps > steps) { | ||
var max = Math.max.apply(null, substeps); | ||
substeps[substeps.indexOf(max)]--; | ||
} | ||
@@ -203,3 +207,6 @@ return substeps; | ||
if (hasPosition) { | ||
stop.color = tinycolor(stop.color); | ||
stop = { | ||
color: tinycolor(stop.color), | ||
pos: stop.pos | ||
}; | ||
@@ -243,3 +250,12 @@ if (stop.pos < 0 || stop.pos > 1) { | ||
TinyGradient.prototype.reverse = function() { | ||
return new TinyGradient(this.stops.reverse()); | ||
var stops = []; | ||
this.stops.forEach(function(stop) { | ||
stops.push({ | ||
color: stop.color, | ||
pos: 1 - stop.pos | ||
}); | ||
}); | ||
return new TinyGradient(stops.reverse()); | ||
}; | ||
@@ -325,4 +341,40 @@ | ||
/** | ||
* Initialize and create gradient with RGBa interpolation | ||
* @see TinyGradient::rgb | ||
*/ | ||
TinyGradient.rgb = function(colors, steps) { | ||
colors = Array.prototype.slice.call(arguments); | ||
steps = colors.pop(); | ||
return TinyGradient.apply(null, colors).rgb(steps); | ||
}; | ||
/** | ||
* Initialize and create gradient with HSVa interpolation | ||
* @see TinyGradient::hsv | ||
*/ | ||
TinyGradient.hsv = function(colors, steps, mode) { | ||
colors = Array.prototype.slice.call(arguments); | ||
mode = colors.pop(); | ||
steps = colors.pop(); | ||
return TinyGradient.apply(null, colors).hsv(steps, mode); | ||
}; | ||
/** | ||
* Initialize and generate CSS3 command for gradient | ||
* @see TinyGradient::css | ||
*/ | ||
TinyGradient.css = function(colors, mode, direction) { | ||
colors = Array.prototype.slice.call(arguments); | ||
direction = colors.pop(); | ||
mode = colors.pop(); | ||
return TinyGradient.apply(null, colors).css(mode, direction); | ||
}; | ||
// export | ||
return TinyGradient; | ||
})); |
/*! | ||
* jQuery TinyGradient 0.2.0 | ||
* jQuery TinyGradient 0.3.0 | ||
* Copyright 2014 Damien "Mistic" Sorel (http://www.strangeplanet.fr) | ||
* Licensed under MIT (http://opensource.org/licenses/MIT) | ||
*/ | ||
!function(a,b){"undefined"!=typeof module&&module.exports?module.exports=b(require("tinycolor2")):"function"==typeof define&&define.amd?define(["tinycolor"],b):a.tinygradient=b(a.tinycolor)}(this,function(a){"use strict";var b={rgba_max:{r:256,g:256,b:256,a:1},hsva_max:{h:360,s:1,v:1,a:1},stepize:function(a,b,c){var d={};for(var e in a)a.hasOwnProperty(e)&&(d[e]=(b[e]-a[e])/c);return d},interpolate:function(a,b,c,d){var e={};for(var f in b)b.hasOwnProperty(f)&&(e[f]=a[f]*c+b[f],e[f]=e[f]<0?e[f]+d[f]:1!=d[f]?e[f]%d[f]:e[f]);return e},rgb:function(c,d,e){for(var f,g=c.color.toRgb(),h=d.color.toRgb(),i=[c.color],j=b.stepize(g,h,e),k=1;e>k;k++)f=b.interpolate(j,g,k,b.rgba_max),i.push(a(f));return i},hsv:function(c,d,e,f){var g,h,i=c.color.toHsv(),j=d.color.toHsv(),k=[c.color],l=b.stepize(i,j,e);g=i.h<=j.h&&!f||i.h>=j.h&&f?j.h-i.h:f?360-j.h+i.h:360-i.h+j.h,l.h=Math.pow(-1,f)*Math.abs(g)*1/e;for(var m=1;e>m;m++)h=b.interpolate(l,i,m,b.hsva_max),k.push(a(h));return k},substeps:function(a,b){var c=a.length;if(b=parseInt(b),isNaN(b)||2>b)throw new Error("Invalid number of steps (< 2)");if(c>b)throw new Error("Number of steps cannot be inferior to number of stops");for(var d=[],e=1;c>e;e++){var f=(b-1)*(a[e].pos-a[e-1].pos);d.push(Math.max(1,Math.round(f)))}for(var g=1,h=c-1;h--;)g+=d[h];if(b>g){var i=Math.min.apply(null,d);d[d.indexOf(i)]++}else if(g>b){var j=Math.max.apply(null,d);d[d.indexOf(j)]--}return d}},c=function(b){if(1==arguments.length){if(!(arguments[0]instanceof Array))throw new Error('"stops" is not an array');b=arguments[0]}else b=Array.prototype.slice.call(arguments);if(!(this instanceof c))return new c(b);if(b.length<2)throw new Error("Invalid number of stops (< 2)");var d=void 0!==b[0].pos,e=b.length,f=-1;this.stops=b.map(function(b,c){var g=void 0!==b.pos;if(d^g)throw new Error("Cannot mix positionned and not posionned color stops");if(g){if(b.color=a(b.color),b.pos<0||b.pos>1)throw new Error("Color stops positions must be between 0 and 1");if(b.pos<=f)throw new Error("Color stops positions are not ordered");f=b.pos}else b={color:a(b),pos:c/(e-1)};return b}),0!==this.stops[0].pos&&this.stops.unshift({color:this.stops[0].color,pos:0}),1!==this.stops[this.stops.length-1].pos&&this.stops.push({color:this.stops[this.stops.length-1].color,pos:1})};return c.prototype.reverse=function(){return new c(this.stops.reverse())},c.prototype.rgb=function(a){for(var c=b.substeps(this.stops,a),d=[],e=0,f=this.stops.length;f-1>e;e++)d=d.concat(b.rgb(this.stops[e],this.stops[e+1],c[e]));return d.push(this.stops[f-1].color),d},c.prototype.hsv=function(a,c){for(var d,e,f,g=b.substeps(this.stops,a),h=c===!0,i="string"==typeof c,j=[],k=0,l=this.stops.length;l-1>k;k++)d=this.stops[k].color.toHsv(),e=this.stops[k+1].color.toHsv(),i&&(f=d.h<e.h&&e.h-d.h<180||d.h>e.h&&d.h-e.h>180),j=j.concat(0===d.s||0===e.s?b.rgb(this.stops[k],this.stops[k+1],g[k]):b.hsv(this.stops[k],this.stops[k+1],g[k],"long"===c&&f||"short"===c&&!f||!i&&h));return j.push(this.stops[l-1].color),j},c.prototype.css=function(a,b){a=a||"linear",b=b||("linear"==a?"to right":"ellipse at center");var c=a+"-gradient("+b;return this.stops.forEach(function(a){c+=", "+a.color.toRgbString()+" "+100*a.pos+"%"}),c+=")"},c}); | ||
!function(a,b){"undefined"!=typeof module&&module.exports?module.exports=b(require("tinycolor2")):"function"==typeof define&&define.amd?define(["tinycolor"],b):a.tinygradient=b(a.tinycolor)}(this,function(a){"use strict";var b={rgba_max:{r:256,g:256,b:256,a:1},hsva_max:{h:360,s:1,v:1,a:1},stepize:function(a,b,c){var d={};for(var e in a)a.hasOwnProperty(e)&&(d[e]=(b[e]-a[e])/c);return d},interpolate:function(a,b,c,d){var e={};for(var f in b)b.hasOwnProperty(f)&&(e[f]=a[f]*c+b[f],e[f]=e[f]<0?e[f]+d[f]:1!=d[f]?e[f]%d[f]:e[f]);return e},rgb:function(c,d,e){for(var f,g=c.color.toRgb(),h=d.color.toRgb(),i=[c.color],j=b.stepize(g,h,e),k=1;e>k;k++)f=b.interpolate(j,g,k,b.rgba_max),i.push(a(f));return i},hsv:function(c,d,e,f){var g,h,i=c.color.toHsv(),j=d.color.toHsv(),k=[c.color],l=b.stepize(i,j,e);g=i.h<=j.h&&!f||i.h>=j.h&&f?j.h-i.h:f?360-j.h+i.h:360-i.h+j.h,l.h=Math.pow(-1,f)*Math.abs(g)*1/e;for(var m=1;e>m;m++)h=b.interpolate(l,i,m,b.hsva_max),k.push(a(h));return k},substeps:function(a,b){var c=a.length;if(b=parseInt(b),isNaN(b)||2>b)throw new Error("Invalid number of steps (< 2)");if(c>b)throw new Error("Number of steps cannot be inferior to number of stops");for(var d=[],e=1;c>e;e++){var f=(b-1)*(a[e].pos-a[e-1].pos);d.push(Math.max(1,Math.round(f)))}for(var g=1,h=c-1;h--;)g+=d[h];for(;g!=b;)if(b>g){var i=Math.min.apply(null,d);d[d.indexOf(i)]++,g++}else{var j=Math.max.apply(null,d);d[d.indexOf(j)]--,g--}return d}},c=function(b){if(1==arguments.length){if(!(arguments[0]instanceof Array))throw new Error('"stops" is not an array');b=arguments[0]}else b=Array.prototype.slice.call(arguments);if(!(this instanceof c))return new c(b);if(b.length<2)throw new Error("Invalid number of stops (< 2)");var d=void 0!==b[0].pos,e=b.length,f=-1;this.stops=b.map(function(b,c){var g=void 0!==b.pos;if(d^g)throw new Error("Cannot mix positionned and not posionned color stops");if(g){if(b={color:a(b.color),pos:b.pos},b.pos<0||b.pos>1)throw new Error("Color stops positions must be between 0 and 1");if(b.pos<=f)throw new Error("Color stops positions are not ordered");f=b.pos}else b={color:a(b),pos:c/(e-1)};return b}),0!==this.stops[0].pos&&this.stops.unshift({color:this.stops[0].color,pos:0}),1!==this.stops[this.stops.length-1].pos&&this.stops.push({color:this.stops[this.stops.length-1].color,pos:1})};return c.prototype.reverse=function(){var a=[];return this.stops.forEach(function(b){a.push({color:b.color,pos:1-b.pos})}),new c(a.reverse())},c.prototype.rgb=function(a){for(var c=b.substeps(this.stops,a),d=[],e=0,f=this.stops.length;f-1>e;e++)d=d.concat(b.rgb(this.stops[e],this.stops[e+1],c[e]));return d.push(this.stops[f-1].color),d},c.prototype.hsv=function(a,c){for(var d,e,f,g=b.substeps(this.stops,a),h=c===!0,i="string"==typeof c,j=[],k=0,l=this.stops.length;l-1>k;k++)d=this.stops[k].color.toHsv(),e=this.stops[k+1].color.toHsv(),i&&(f=d.h<e.h&&e.h-d.h<180||d.h>e.h&&d.h-e.h>180),j=j.concat(0===d.s||0===e.s?b.rgb(this.stops[k],this.stops[k+1],g[k]):b.hsv(this.stops[k],this.stops[k+1],g[k],"long"===c&&f||"short"===c&&!f||!i&&h));return j.push(this.stops[l-1].color),j},c.prototype.css=function(a,b){a=a||"linear",b=b||("linear"==a?"to right":"ellipse at center");var c=a+"-gradient("+b;return this.stops.forEach(function(a){c+=", "+a.color.toRgbString()+" "+100*a.pos+"%"}),c+=")"},c.rgb=function(a,b){return a=Array.prototype.slice.call(arguments),b=a.pop(),c.apply(null,a).rgb(b)},c.hsv=function(a,b,d){return a=Array.prototype.slice.call(arguments),d=a.pop(),b=a.pop(),c.apply(null,a).hsv(b,d)},c.css=function(a,b,d){return a=Array.prototype.slice.call(arguments),d=a.pop(),b=a.pop(),c.apply(null,a).css(b,d)},c}); |
29972
7.95%498
11.41%+ Added
- Removed
Updated