source: Dev/branches/rest-dojo-ui/client/dojox/gfx/gradutils.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 2.4 KB
Line 
1// Various generic utilities to deal with a linear gradient
2
3define(["./_base", "dojo/_base/lang", "./matrix", "dojo/_base/Color"],
4  function(g, lang, m, Color){
5 
6        /*===== g= dojox.gfx =====*/
7        var gradutils = g.gradutils = {};
8        /*===== g= dojox.gfx; gradutils = dojox.gfx.gradutils; =====*/
9
10        function findColor(o, c){
11                if(o <= 0){
12                        return c[0].color;
13                }
14                var len = c.length;
15                if(o >= 1){
16                        return c[len - 1].color;
17                }
18                //TODO: use binary search
19                for(var i = 0; i < len; ++i){
20                        var stop = c[i];
21                        if(stop.offset >= o){
22                                if(i){
23                                        var prev = c[i - 1];
24                                        return Color.blendColors(new Color(prev.color), new Color(stop.color),
25                                                (o - prev.offset) / (stop.offset - prev.offset));
26                                }
27                                return stop.color;
28                        }
29                }
30                return c[len - 1].color;
31        }
32
33        gradutils.getColor = function(fill, pt){
34                // summary:
35                //              sample a color from a gradient using a point
36                // fill: Object:
37                //              fill object
38                // pt: dojox.gfx.Point:
39                //              point where to sample a color
40                var o;
41                if(fill){
42                        switch(fill.type){
43                                case "linear":
44                                        var angle = Math.atan2(fill.y2 - fill.y1, fill.x2 - fill.x1),
45                                                rotation = m.rotate(-angle),
46                                                projection = m.project(fill.x2 - fill.x1, fill.y2 - fill.y1),
47                                                p = m.multiplyPoint(projection, pt),
48                                                pf1 = m.multiplyPoint(projection, fill.x1, fill.y1),
49                                                pf2 = m.multiplyPoint(projection, fill.x2, fill.y2),
50                                                scale = m.multiplyPoint(rotation, pf2.x - pf1.x, pf2.y - pf1.y).x;
51                                        o = m.multiplyPoint(rotation, p.x - pf1.x, p.y - pf1.y).x / scale;
52                                        break;
53                                case "radial":
54                                        var dx = pt.x - fill.cx, dy = pt.y - fill.cy;
55                                        o = Math.sqrt(dx * dx + dy * dy) / fill.r;
56                                        break;
57                        }
58                        return findColor(o, fill.colors);       // dojo.Color
59                }
60                // simple color
61                return new Color(fill || [0, 0, 0, 0]); // dojo.Color
62        };
63
64        gradutils.reverse = function(fill){
65                // summary:
66                //              reverses a gradient
67                // fill: Object:
68                //              fill object
69                if(fill){
70                        switch(fill.type){
71                                case "linear":
72                                case "radial":
73                                        fill = lang.delegate(fill);
74                                        if(fill.colors){
75                                                var c = fill.colors, l = c.length, i = 0, stop,
76                                                        n = fill.colors = new Array(c.length);
77                                                for(; i < l; ++i){
78                                                        stop = c[i];
79                                                        n[i] = {
80                                                                offset: 1 - stop.offset,
81                                                                color:  stop.color
82                                                        };
83                                                }
84                                                n.sort(function(a, b){ return a.offset - b.offset; });
85                                        }
86                                        break;
87                        }
88                }
89                return fill;    // Object
90        };
91
92        return gradutils;
93});
Note: See TracBrowser for help on using the repository browser.