PageRenderTime 5ms CodeModel.GetById 1ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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