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

http://gmaps-utility-library-flash.googlecode.com/ · ActionScript · 131 lines · 78 code · 13 blank · 40 comment · 2 complexity · 618ea5e89d40e9b25d984b7cfc886160 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.errors.IllegalOperationError;
  8. import com.google.maps.Color;
  9. /**
  10. * A Range is an abstract class that stores three numerical ranges
  11. * representing colors, absolute values and percentages.
  12. *
  13. * @author Simon Ilyushchenko
  14. */
  15. public class Range {
  16. // Use this value if the start of a range is undefined.
  17. protected static var MIN_PERCENT:Number = 0;
  18. // Use this value if the end of a range is undefined.
  19. protected static var MAX_PERCENT:Number = 100;
  20. // Start of a range
  21. public var minValue:Number = NaN;
  22. // End of a range
  23. public var maxValue:Number = NaN;
  24. // Start of a color range
  25. public var minColor:Number = NaN;
  26. // Start of a color range
  27. public var maxColor:Number = NaN;
  28. // Access to min/maxPercent will be overridden in subclasses,
  29. // hence they are not public.
  30. protected var _minPercent:Number = NaN; /** Backing for minPercent. */
  31. protected var _maxPercent:Number = NaN; /** Backing for maxPercent. */
  32. /**
  33. * Class constructor
  34. *
  35. * @param self The instance of the child being constructed
  36. *
  37. * @throws flash.errors.IllegalOperationError If this abstract class is
  38. * instantiated directly.
  39. */
  40. public function Range(self:Range) {
  41. //only a subclass can pass a valid reference to self
  42. if (self != this) {
  43. throw new IllegalOperationError(
  44. "Abstract class did not receive reference to self. " +
  45. "MyAbstractType cannot be instantiated directly.");
  46. }
  47. }
  48. /**
  49. * Start of a percentage range.
  50. */
  51. public function get minPercent():Number {
  52. return _minPercent;
  53. }
  54. public function set minPercent(someValue:Number):void {
  55. _minPercent = someValue;
  56. }
  57. /**
  58. * End of a percentage range.
  59. */
  60. public function get maxPercent():Number {
  61. return _maxPercent;
  62. }
  63. public function set maxPercent(someValue:Number):void {
  64. _maxPercent = someValue;
  65. }
  66. /**
  67. * String representation of this object
  68. */
  69. public function toString():String {
  70. return ([
  71. "Range: ",
  72. "value: " + minValue.toString() + " - " + maxValue.toString(),
  73. "percent: " + minPercent.toString() + " - " + maxPercent.toString(),
  74. "color: " + rgbColorDictString()
  75. ]).join(", ");
  76. }
  77. /**
  78. * String representation of the color ranges of this object
  79. */
  80. public function rgbColorDictString():String {
  81. var rgbColorDict:Object = getRgbColorDict();
  82. var result:Array = [];
  83. result.push(
  84. "Start: " + rgbColorDict['r']['min'].toString() + "," +
  85. rgbColorDict['g']['min'].toString() + ", " +
  86. rgbColorDict['b']['min'].toString());
  87. result.push(
  88. "End: " + rgbColorDict['r']['max'].toString() + "," +
  89. rgbColorDict['g']['max'].toString() + ", " +
  90. rgbColorDict['b']['max'].toString());
  91. return result.join(', ');
  92. }
  93. function getValueDict():Object {
  94. return {
  95. 'min': minValue,
  96. 'max': maxValue
  97. }
  98. }
  99. function getRgbColorDict():Object {
  100. var result:Object = {};
  101. var minColorObj:Color = new Color(minColor);
  102. var maxColorObj:Color = new Color(maxColor);
  103. result['r'] = {
  104. 'min': minColorObj.r,
  105. 'max': maxColorObj.r
  106. }
  107. result['g'] = {
  108. 'min': minColorObj.g,
  109. 'max': maxColorObj.g
  110. }
  111. result['b'] = {
  112. 'min': minColorObj.b,
  113. 'max': maxColorObj.b
  114. }
  115. return result;
  116. }
  117. }
  118. }