summaryrefslogtreecommitdiff
path: root/js/dojo-1.7.2/dojox/gfx/gradutils.js
diff options
context:
space:
mode:
authorTristan Zur <tzur@web.web.ccwn.org>2014-03-27 22:27:47 +0100
committerTristan Zur <tzur@web.web.ccwn.org>2014-03-27 22:27:47 +0100
commitb62676ca5d3d6f6ba3f019ea3f99722e165a98d8 (patch)
tree86722cb80f07d4569f90088eeaea2fc2f6e2ef94 /js/dojo-1.7.2/dojox/gfx/gradutils.js
Initial commit of intern.ccwn.org contentsHEADmaster
Diffstat (limited to 'js/dojo-1.7.2/dojox/gfx/gradutils.js')
-rw-r--r--js/dojo-1.7.2/dojox/gfx/gradutils.js94
1 files changed, 94 insertions, 0 deletions
diff --git a/js/dojo-1.7.2/dojox/gfx/gradutils.js b/js/dojo-1.7.2/dojox/gfx/gradutils.js
new file mode 100644
index 0000000..e93e5cb
--- /dev/null
+++ b/js/dojo-1.7.2/dojox/gfx/gradutils.js
@@ -0,0 +1,94 @@
+//>>built
+// Various generic utilities to deal with a linear gradient
+
+define("dojox/gfx/gradutils", ["./_base", "dojo/_base/lang", "./matrix", "dojo/_base/Color"],
+ function(g, lang, m, Color){
+
+ /*===== g= dojox.gfx =====*/
+ var gradutils = g.gradutils = {};
+ /*===== g= dojox.gfx; gradutils = dojox.gfx.gradutils; =====*/
+
+ function findColor(o, c){
+ if(o <= 0){
+ return c[0].color;
+ }
+ var len = c.length;
+ if(o >= 1){
+ return c[len - 1].color;
+ }
+ //TODO: use binary search
+ for(var i = 0; i < len; ++i){
+ var stop = c[i];
+ if(stop.offset >= o){
+ if(i){
+ var prev = c[i - 1];
+ return Color.blendColors(new Color(prev.color), new Color(stop.color),
+ (o - prev.offset) / (stop.offset - prev.offset));
+ }
+ return stop.color;
+ }
+ }
+ return c[len - 1].color;
+ }
+
+ gradutils.getColor = function(fill, pt){
+ // summary:
+ // sample a color from a gradient using a point
+ // fill: Object:
+ // fill object
+ // pt: dojox.gfx.Point:
+ // point where to sample a color
+ var o;
+ if(fill){
+ switch(fill.type){
+ case "linear":
+ var angle = Math.atan2(fill.y2 - fill.y1, fill.x2 - fill.x1),
+ rotation = m.rotate(-angle),
+ projection = m.project(fill.x2 - fill.x1, fill.y2 - fill.y1),
+ p = m.multiplyPoint(projection, pt),
+ pf1 = m.multiplyPoint(projection, fill.x1, fill.y1),
+ pf2 = m.multiplyPoint(projection, fill.x2, fill.y2),
+ scale = m.multiplyPoint(rotation, pf2.x - pf1.x, pf2.y - pf1.y).x;
+ o = m.multiplyPoint(rotation, p.x - pf1.x, p.y - pf1.y).x / scale;
+ break;
+ case "radial":
+ var dx = pt.x - fill.cx, dy = pt.y - fill.cy;
+ o = Math.sqrt(dx * dx + dy * dy) / fill.r;
+ break;
+ }
+ return findColor(o, fill.colors); // dojo.Color
+ }
+ // simple color
+ return new Color(fill || [0, 0, 0, 0]); // dojo.Color
+ };
+
+ gradutils.reverse = function(fill){
+ // summary:
+ // reverses a gradient
+ // fill: Object:
+ // fill object
+ if(fill){
+ switch(fill.type){
+ case "linear":
+ case "radial":
+ fill = lang.delegate(fill);
+ if(fill.colors){
+ var c = fill.colors, l = c.length, i = 0, stop,
+ n = fill.colors = new Array(c.length);
+ for(; i < l; ++i){
+ stop = c[i];
+ n[i] = {
+ offset: 1 - stop.offset,
+ color: stop.color
+ };
+ }
+ n.sort(function(a, b){ return a.offset - b.offset; });
+ }
+ break;
+ }
+ }
+ return fill; // Object
+ };
+
+ return gradutils;
+});