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