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

http://gmaps-utility-library-flash.googlecode.com/ · ActionScript · 204 lines · 130 code · 21 blank · 53 comment · 30 complexity · 4e299bce78fd1c7f7cc7fd3736d57d82 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 com.google.maps.Color;
  8. import flash.utils.getQualifiedClassName;
  9. /**
  10. * GradientRule is a basic building block for gradient configurations.
  11. * One GradientRule object corresponds to a gradient transition
  12. * between two color values. Together with other GradientRule objects,
  13. * it sholud be contained in GradientRuleList to form a full gradient config.
  14. *
  15. * @author Simon Ilyushchenko
  16. */
  17. public class GradientRule extends Range {
  18. public function GradientRule() {
  19. super(this);
  20. }
  21. /**
  22. * Create a new instance of this class from an XML config
  23. *
  24. * @param xml An XML configuration specifying gradient config
  25. */
  26. public static function fromXML(xml:XML):GradientRule {
  27. var result:GradientRule = new GradientRule();
  28. if (xml.minPercent != undefined) {
  29. result.minPercent = xml.minPercent;
  30. }
  31. if (xml.maxPercent != undefined) {
  32. result.maxPercent = xml.maxPercent;
  33. }
  34. if (xml.minValue != undefined) {
  35. result.minValue = xml.minValue;
  36. }
  37. if (xml.maxValue != undefined) {
  38. result.maxValue = xml.maxValue;
  39. }
  40. if (xml.minColor != undefined) {
  41. result.minColor = xml.minColor;
  42. }
  43. if (xml.maxColor != undefined) {
  44. result.maxColor = xml.maxColor;
  45. }
  46. return result;
  47. }
  48. /**
  49. * Start of a percentage range. Values are forced to be greater than
  50. * MIN_PERCENT.
  51. */
  52. public override function set minPercent(value:Number):void {
  53. if (value < MIN_PERCENT) {
  54. value = MIN_PERCENT;
  55. }
  56. _minPercent = value;
  57. }
  58. /**
  59. * End of a percentage range. Values are forced to be less than
  60. * MAX_PERCENT.
  61. */
  62. public override function set maxPercent(value:Number):void {
  63. if (value > MAX_PERCENT) {
  64. value = MAX_PERCENT;
  65. }
  66. _maxPercent = value;
  67. }
  68. /**
  69. * Create a new instance of this class from an XML config
  70. *
  71. * @param dataValues An array of numbers
  72. *
  73. * @return A new GradientControl objects with this object's
  74. * gradient config applied to the specified values
  75. */
  76. public function applyGradientToValueList(dataValues:Array):GradientControl {
  77. var arrayMinMax:Array = GradientUtil.arrayMinMax(dataValues);
  78. if (arrayMinMax.length == 0) {
  79. return null;
  80. }
  81. minValue = arrayMinMax[0];
  82. maxValue = arrayMinMax[1];
  83. // TODO: should these be called twice?
  84. // Try reading values from children
  85. fillValueColorFromChildren();
  86. // If still unfilled, try reading values from the siblings
  87. fillValueColorFromSiblings();
  88. // If still unfilled, try reading values from the parent
  89. fillValueColorFromParent(this);
  90. var gradients:Array = createValueColorGradients(this);
  91. return new GradientControl(gradients);
  92. }
  93. // Nothing to do - these are leaf objects.
  94. function fillValueColorFromChildren():void {
  95. }
  96. function fillValueColorFromSiblings():void {
  97. }
  98. function fillValueColorFromParent(parent:GradientRule):void {
  99. }
  100. // If min or max absolute values are not specified,
  101. // calculate them based on the percentage values
  102. // and on the min/max values taken from the parent.
  103. function createValueColorGradients(parent:Range): Array {
  104. var percentRangeDict:Object = {
  105. 'min': 0,
  106. 'max': 100
  107. }
  108. var flatGradient:ValueColorGradient = createFlatGradient();
  109. var dstDict:Object = {
  110. 'min': parent.minValue,
  111. 'max': parent.maxValue
  112. }
  113. if (isNaN(flatGradient.minValue)) {
  114. flatGradient.minValue = GradientUtil.linearScale(
  115. percentRangeDict, dstDict, minPercent);
  116. }
  117. if (isNaN(flatGradient.maxValue)) {
  118. flatGradient.maxValue = GradientUtil.linearScale(
  119. percentRangeDict, dstDict, maxPercent);
  120. }
  121. return [flatGradient];
  122. }
  123. // Create a gradient with absolute values for use in a GradientControl.
  124. protected function createFlatGradient(): ValueColorGradient {
  125. var result:ValueColorGradient = new ValueColorGradient();
  126. result.minValue = this.minValue;
  127. result.maxValue = this.maxValue;
  128. result.minColor = this.minColor;
  129. result.maxColor = this.maxColor;
  130. return result;
  131. }
  132. // Duplicate the min values of another GradientRule object
  133. function fillMin(other:GradientRule):void {
  134. if (isNaN(minValue)) {
  135. minValue = other.minValue;
  136. }
  137. if (isNaN(minColor)) {
  138. minColor = other.minColor;
  139. }
  140. // Do not transfer percentage between levels
  141. }
  142. // Duplicate the max values of another GradientRule object
  143. function fillMax(other:GradientRule):void {
  144. if (isNaN(maxValue)) {
  145. maxValue = other.maxValue;
  146. }
  147. if (isNaN(maxColor)) {
  148. maxColor = other.maxColor;
  149. }
  150. // Do not transfer percentage between levels
  151. }
  152. // Duplicate the max values of another GradientRule object
  153. // as the min values for this object
  154. function fillFromPrev(
  155. prev:GradientRule, fillPercent:Boolean=true):void {
  156. if (isNaN(minPercent) && fillPercent) {
  157. //minValue won't be overriden by this
  158. minPercent = prev.maxPercent;
  159. }
  160. if (isNaN(minValue)) {
  161. minValue = prev.maxValue;
  162. }
  163. if (isNaN(minColor)) {
  164. minColor = prev.maxColor;
  165. }
  166. }
  167. // Duplicate the min values of another GradientRule object
  168. // as the max values for this object
  169. function fillFromNext(
  170. next:GradientRule, fillPercent:Boolean=true):void {
  171. if (isNaN(maxPercent) && fillPercent) {
  172. //maxValue won't be overriden by this
  173. maxPercent = next.minPercent;
  174. }
  175. if (isNaN(maxValue)) {
  176. maxValue = next.minValue;
  177. }
  178. if (isNaN(maxColor)) {
  179. maxColor = next.minColor;
  180. }
  181. }
  182. }
  183. }