/src/com/google/maps/extras/gradients/GradientControl.as

http://gmaps-utility-library-flash.googlecode.com/ · ActionScript · 80 lines · 38 code · 8 blank · 34 comment · 3 complexity · 07275d02b8e0b184a251aa6cce0036c5 MD5 · raw file

  1. /**
  2. * Copyright 2009 Google Inc.
  3. * Licensed under the Apache License, Version 2.0:
  4. * http://www.apache.org/licenses/LICENSE-2.0
  5. */
  6. package com.google.maps.extras.gradients {
  7. import flash.utils.getQualifiedClassName;
  8. import com.google.maps.Color;
  9. import com.google.maps.extras.gradients.GradientUtil;
  10. /**
  11. * GradientControl contains a list of gradient-calcaluting rules
  12. * applied to a specific numerical range.
  13. *
  14. * @author Simon Ilyushchenko
  15. */
  16. public class GradientControl extends ValueColorGradient {
  17. // A list of ValueColorGradients storing specific
  18. // gradient rules.
  19. private var flatGradientList:Array = [];
  20. /**
  21. * Class constructor
  22. *
  23. * @param flatFradientList The list of gradients to use
  24. */
  25. public function GradientControl(flatGradientList:Array):void {
  26. super();
  27. // TODO: Make sure they are sorted???
  28. this.flatGradientList = flatGradientList;
  29. }
  30. // Return the number of gradients in this control
  31. protected override function numFlatGradients():int {
  32. return flatGradientList.length;
  33. }
  34. // Return the gradient at the specified index
  35. protected override function flatGradientAt(i:int):ValueColorGradient {
  36. return flatGradientList[i];
  37. }
  38. /**
  39. * Find the gradient into whose range the specified value falls
  40. * and use it to calculate the color.
  41. *
  42. * @param value A number to translate into a color.
  43. */
  44. public override function colorForValue(value:Number):Number {
  45. for (var i:int=0; i<numFlatGradients(); i++) {
  46. var gradient:ValueColorGradient = flatGradientAt(i);
  47. if (gradient.containsValue(value)) {
  48. return gradient.colorForValue(value);
  49. }
  50. }
  51. // The value is greater than last gradient's max
  52. // - delegate to last gradient.
  53. var lastGradient:ValueColorGradient = flatGradientAt(numFlatGradients()-1);
  54. return lastGradient.colorForValue(value);
  55. }
  56. /**
  57. * Class constructor
  58. *
  59. * @param flatFradientList The list of gradients to use
  60. */
  61. public override function toString():String {
  62. var parentString:String = super.toString();
  63. var result:Array = [parentString];
  64. result.push('');
  65. result.push('Flat gradients');
  66. for (var j:int=0; j<numFlatGradients(); j++) {
  67. result.push(flatGradientAt(j).toString());
  68. }
  69. return result.join("\n");
  70. }
  71. }
  72. }