Debian Codegraph @

dojox/color/NeutralColorModel.js

raw symbols SCIP
1 define(["dojo/_base/array", "dojo/_base/declare", "./SimpleColorModel"],
2 function(arr, declare, SimpleColorModel){
3
4 return declare("dojox.color.NeutralColorModel", SimpleColorModel, {
5 // summary:
6 // Base class for color models that return a color from a data value
7 // using an interpolation between two extremum colors around a neutral value.
8
9 _min: 0,
10 _max: 0,
11 _e: 0,
12
13 constructor: function(startColor, endColor){
14 // startColor: dojo/_base/Color
15 // The start color.
16 // endColor: dojo/_base/Color?
17 // The end color.
18 },
19
20 initialize: function(items, colorFunc){
21 // summary:
22 // Initialize the color model from a list of data items and using a function
23 // that returns the value used to compute the color for a given item.
24 // items: Object[]
25 // The data items.
26 // colorFunc: Function
27 // The function that returns the value used to compute the color for particular data item.
28 var values = [];
29 var sum = 0;
30 var min = 100000000;
31 var max = -min;
32 arr.forEach(items, function(item){
33 var value = colorFunc(item);
34 min = Math.min(min, value);
35 max = Math.max(max, value);
36 sum += value;
37 values.push(value);
38 });
39 values.sort(function(a,b){return a - b;});
40 var neutral = this.computeNeutral(min, max, sum, values);
41 this._min = min;
42 this._max = max;
43 if(this._min == this._max || neutral == this._min){
44 this._e = -1;
45 }else{
46 this._e = Math.log(.5) / Math.log((neutral - this._min) / (this._max - this._min));
47 }
48 },
49
50 computeNeutral: function(min, max, sum, values){
51 // summary:
52 // Return the neutral value. This can be for example the mean or average value.
53 // This function must be implemented by implementations.
54 // min: Number
55 // The minimal value.
56 // max: Number
57 // The maximum value.
58 // sum: Number
59 // The sum of all values.
60 // values: Number[]
61 // The sorted array of values used to compute colors.
62 },
63
64 getNormalizedValue: function(value){
65 // summary:
66 // Return the normalized (between 0 and 1) value for a given data value.
67 // This implementation uses an power function to map neutral value to 0.5
68 // and distribute other values around it.
69 // value: Number
70 // The data value
71 if(this._e < 0){
72 return 0;
73 }
74 value = (value - this._min) / (this._max - this._min);
75 return Math.pow(value, this._e);
76 }
77 });
78
79 });