gradutils.js 2.6 KB

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