| 1 |
define(["dojo/_base/lang", "./matrix", "dojo/_base/Color"], |
| 2 |
function(lang, m, Color){ |
| 3 |
|
| 4 |
var grad = lang.getObject("dojox.gfx.gradient", true); |
| 5 |
var C = Color; |
| 6 |
|
| 7 |
grad.rescale = function(stops, from, to){ |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
var len = stops.length, reverseFlag = (to < from), newStops; |
| 21 |
|
| 22 |
|
| 23 |
if(reverseFlag){ |
| 24 |
var tmp = from; |
| 25 |
from = to; |
| 26 |
to = tmp; |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
if(!len){ |
| 31 |
|
| 32 |
return []; |
| 33 |
} |
| 34 |
if(to <= stops[0].offset){ |
| 35 |
|
| 36 |
newStops = [ |
| 37 |
{offset: 0, color: stops[0].color}, |
| 38 |
{offset: 1, color: stops[0].color} |
| 39 |
]; |
| 40 |
}else if(from >= stops[len - 1].offset){ |
| 41 |
|
| 42 |
newStops = [ |
| 43 |
{offset: 0, color: stops[len - 1].color}, |
| 44 |
{offset: 1, color: stops[len - 1].color} |
| 45 |
]; |
| 46 |
}else{ |
| 47 |
|
| 48 |
var span = to - from, stop, prev, i; |
| 49 |
newStops = []; |
| 50 |
if(from < 0){ |
| 51 |
newStops.push({offset: 0, color: new C(stops[0].color)}); |
| 52 |
} |
| 53 |
for(i = 0; i < len; ++i){ |
| 54 |
stop = stops[i]; |
| 55 |
if(stop.offset >= from){ |
| 56 |
break; |
| 57 |
} |
| 58 |
|
| 59 |
} |
| 60 |
if(i){ |
| 61 |
prev = stops[i - 1]; |
| 62 |
newStops.push({ |
| 63 |
offset: 0, |
| 64 |
color: Color.blendColors(new C(prev.color), new C(stop.color), (from - prev.offset) / (stop.offset - prev.offset)) |
| 65 |
}); |
| 66 |
}else{ |
| 67 |
newStops.push({offset: 0, color: new C(stop.color)}); |
| 68 |
} |
| 69 |
for(; i < len; ++i){ |
| 70 |
stop = stops[i]; |
| 71 |
if(stop.offset >= to){ |
| 72 |
break; |
| 73 |
} |
| 74 |
newStops.push({offset: (stop.offset - from) / span, color: new C(stop.color)}); |
| 75 |
} |
| 76 |
if(i < len){ |
| 77 |
prev = stops[i - 1]; |
| 78 |
newStops.push({ |
| 79 |
offset: 1, |
| 80 |
color: Color.blendColors(new C(prev.color), new C(stop.color), (to - prev.offset) / (stop.offset - prev.offset)) |
| 81 |
}); |
| 82 |
}else{ |
| 83 |
newStops.push({offset: 1, color: new C(stops[len - 1].color)}); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
if(reverseFlag){ |
| 89 |
newStops.reverse(); |
| 90 |
for(i = 0, len = newStops.length; i < len; ++i){ |
| 91 |
stop = newStops[i]; |
| 92 |
stop.offset = 1 - stop.offset; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return newStops; |
| 97 |
}; |
| 98 |
|
| 99 |
function getPoint(x, y, matrix, project, shiftAndRotate, scale){ |
| 100 |
var r = m.multiplyPoint(matrix, x, y), |
| 101 |
p = m.multiplyPoint(project, r); |
| 102 |
return {r: r, p: p, o: m.multiplyPoint(shiftAndRotate, p).x / scale}; |
| 103 |
} |
| 104 |
|
| 105 |
function sortPoints(a, b){ |
| 106 |
return a.o - b.o; |
| 107 |
} |
| 108 |
|
| 109 |
grad.project = function(matrix, gradient, tl, rb, ttl, trb){ |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
matrix = matrix || m.identity; |
| 126 |
|
| 127 |
var f1 = m.multiplyPoint(matrix, gradient.x1, gradient.y1), |
| 128 |
f2 = m.multiplyPoint(matrix, gradient.x2, gradient.y2), |
| 129 |
angle = Math.atan2(f2.y - f1.y, f2.x - f1.x), |
| 130 |
project = m.project(f2.x - f1.x, f2.y - f1.y), |
| 131 |
pf1 = m.multiplyPoint(project, f1), |
| 132 |
pf2 = m.multiplyPoint(project, f2), |
| 133 |
shiftAndRotate = new m.Matrix2D([m.rotate(-angle), {dx: -pf1.x, dy: -pf1.y}]), |
| 134 |
scale = m.multiplyPoint(shiftAndRotate, pf2).x, |
| 135 |
|
| 136 |
|
| 137 |
points = [ |
| 138 |
getPoint(tl.x, tl.y, matrix, project, shiftAndRotate, scale), |
| 139 |
getPoint(rb.x, rb.y, matrix, project, shiftAndRotate, scale), |
| 140 |
getPoint(tl.x, rb.y, matrix, project, shiftAndRotate, scale), |
| 141 |
getPoint(rb.x, tl.y, matrix, project, shiftAndRotate, scale) |
| 142 |
].sort(sortPoints), |
| 143 |
from = points[0].o, |
| 144 |
to = points[3].o, |
| 145 |
stops = grad.rescale(gradient.colors, from, to), |
| 146 |
|
| 147 |
angle2 = Math.atan2(points[3].r.y - points[0].r.y, points[3].r.x - points[0].r.x); |
| 148 |
|
| 149 |
return { |
| 150 |
type: "linear", |
| 151 |
x1: points[0].p.x, y1: points[0].p.y, x2: points[3].p.x, y2: points[3].p.y, |
| 152 |
colors: stops, |
| 153 |
|
| 154 |
angle: angle |
| 155 |
}; |
| 156 |
}; |
| 157 |
|
| 158 |
return grad; |
| 159 |
}); |