PageRenderTime 27ms CodeModel.GetById 13ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

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