Scale.js 985 B

123456789101112131415161718192021222324252627282930313233343536
  1. // TODO code-sharing between scales
  2. /**
  3. * @ignore
  4. * @class
  5. */
  6. pv.Scale = function() {};
  7. /**
  8. * @private Returns a function that interpolators from the start value to the
  9. * end value, given a parameter <i>t</i> in [0, 1].
  10. *
  11. * @param start the start value.
  12. * @param end the end value.
  13. */
  14. pv.Scale.interpolator = function(start, end) {
  15. if (typeof start == "number") {
  16. return function(t) {
  17. return t * (end - start) + start;
  18. };
  19. }
  20. /* For now, assume color. */
  21. start = pv.color(start).rgb();
  22. end = pv.color(end).rgb();
  23. return function(t) {
  24. var a = start.a * (1 - t) + end.a * t;
  25. if (a < 1e-5) a = 0; // avoid scientific notation
  26. return (start.a == 0) ? pv.rgb(end.r, end.g, end.b, a)
  27. : ((end.a == 0) ? pv.rgb(start.r, start.g, start.b, a)
  28. : pv.rgb(
  29. Math.round(start.r * (1 - t) + end.r * t),
  30. Math.round(start.g * (1 - t) + end.g * t),
  31. Math.round(start.b * (1 - t) + end.b * t), a));
  32. };
  33. };