~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/mapfish/core/Color.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007-2008  Camptocamp
 
3
 *
 
4
 * This file is part of MapFish Client
 
5
 *
 
6
 * MapFish Client is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * MapFish Client is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with MapFish Client.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
/**
 
21
 * An abstract representation of color.
 
22
 */
 
23
mapfish.Color = OpenLayers.Class({
 
24
    getColorRgb: function() {}
 
25
});
 
26
 
 
27
/**
 
28
 * Class: mapfish.ColorRgb
 
29
 * Class for representing RGB colors.
 
30
 */
 
31
mapfish.ColorRgb = OpenLayers.Class(mapfish.Color, {
 
32
    redLevel: null,
 
33
    greenLevel: null,
 
34
    blueLevel: null,
 
35
 
 
36
    /**
 
37
     * Constructor: mapfish.ColorRgb
 
38
     *
 
39
     * Parameters:
 
40
     * red - {Integer}
 
41
     * green - {Integer}
 
42
     * blue - {Integer}
 
43
     */
 
44
    initialize: function(red, green, blue) {
 
45
        this.redLevel = red;
 
46
        this.greenLevel = green;
 
47
        this.blueLevel = blue;
 
48
    },
 
49
 
 
50
    /**
 
51
     * APIMethod: equals
 
52
     *      Returns true if the colors at the same.
 
53
     *
 
54
     * Parameters:
 
55
     * {<mapfish.ColorRgb>} color
 
56
     */
 
57
    equals: function(color) {
 
58
        return color.redLevel == this.redLevel &&
 
59
               color.greenLevel == this.greenLevel &&
 
60
               color.blueLevel == this.blueLevel;
 
61
    },
 
62
    
 
63
    getColorRgb: function() {
 
64
        return this;
 
65
    },
 
66
    
 
67
    getRgbArray: function() {
 
68
        return [
 
69
            this.redLevel,
 
70
            this.greenLevel,
 
71
            this.blueLevel
 
72
        ];
 
73
    },
 
74
    
 
75
    /**
 
76
     * Method: hex2rgbArray
 
77
     * Converts a Hex color string to an Rbg array 
 
78
     *
 
79
     * Parameters:
 
80
     * rgbHexString - {String} Hex color string (format: #rrggbb)
 
81
     */
 
82
    hex2rgbArray: function(rgbHexString) {
 
83
        if (rgbHexString.charAt(0) == '#') {
 
84
            rgbHexString = rgbHexString.substr(1);
 
85
        }
 
86
        var rgbArray = [
 
87
            parseInt(rgbHexString.substring(0,2),16),
 
88
            parseInt(rgbHexString.substring(2,4),16),
 
89
            parseInt(rgbHexString.substring(4,6),16)
 
90
        ];
 
91
        for (var i = 0; i < rgbArray.length; i++) {
 
92
            if (rgbArray[i] < 0 || rgbArray[i] > 255 ) {
 
93
                OpenLayers.Console.error("Invalid rgb hex color string: rgbHexString");
 
94
            }
 
95
        }        
 
96
        return rgbArray;
 
97
    },
 
98
    
 
99
    /**
 
100
     * APIMethod: setFromHex
 
101
     * Sets the color from a color hex string 
 
102
     *
 
103
     * Parameters:
 
104
     * rgbHexString - {String} Hex color string (format: #rrggbb)
 
105
     */
 
106
    setFromHex: function(rgbHexString) {        
 
107
        var rgbArray = this.hex2rgbArray(rgbHexString);
 
108
        this.redLevel = rgbArray[0];
 
109
        this.greenLevel = rgbArray[1];
 
110
        this.blueLevel = rgbArray[2];
 
111
    },
 
112
    
 
113
    /**
 
114
     * APIMethod: setFromRgb
 
115
     * Sets the color from a color rgb string
 
116
     *
 
117
     */
 
118
    setFromRgb: function(rgbString) {
 
119
        var color = dojo.colorFromString(rgbString);
 
120
        this.redLevel = color.r;
 
121
        this.greenLevel = color.g;
 
122
        this.blueLevel = color.b;
 
123
    },
 
124
    
 
125
    /**
 
126
     * APIMethod: toHexString
 
127
     * Converts the rgb color to hex string
 
128
     *
 
129
     */
 
130
    toHexString: function() {
 
131
        var r = this.toHex(this.redLevel);
 
132
        var g = this.toHex(this.greenLevel);
 
133
        var b = this.toHex(this.blueLevel);
 
134
        return '#' + r + g + b;
 
135
    },
 
136
    
 
137
    /**
 
138
     * Method: toHex
 
139
     * Converts a color level to its hexadecimal value
 
140
     *
 
141
     * Parameters:
 
142
     * dec - {Integer} Decimal value to convert [0..255]
 
143
     */
 
144
    toHex: function(dec) {
 
145
        // create list of hex characters
 
146
        var hexCharacters = "0123456789ABCDEF";
 
147
        // if number is out of range return limit
 
148
        if (dec < 0 || dec > 255 ) {
 
149
            var msg = "Invalid decimal value for color level";
 
150
            OpenLayers.Console.error(msg);
 
151
        }
 
152
        // decimal equivalent of first hex character in converted number
 
153
        var i = Math.floor(dec / 16);
 
154
        // decimal equivalent of second hex character in converted number
 
155
        var j = dec % 16;
 
156
        // return hexadecimal equivalent
 
157
        return hexCharacters.charAt(i) + hexCharacters.charAt(j);
 
158
    },
 
159
    
 
160
    CLASS_NAME: "mapfish.ColorRgb"
 
161
});
 
162
 
 
163
/**
 
164
 * APIMethod: getColorsArrayByRgbInterpolation
 
165
 *      Get an array of colors based on RGB interpolation.
 
166
 *
 
167
 * Parameters:
 
168
 * firstColor - {<mapfish.Color>} The first color in the range.
 
169
 * lastColor - {<mapfish.Color>} The last color in the range.
 
170
 * nbColors - {Integer} The number of colors in the range.
 
171
 *
 
172
 * Returns
 
173
 * {Array({<mapfish.Color>})} The resulting array of colors.
 
174
 */
 
175
mapfish.ColorRgb.getColorsArrayByRgbInterpolation =
 
176
    function(firstColor, lastColor, nbColors) {
 
177
 
 
178
    var resultColors = [];
 
179
    var colorA = firstColor.getColorRgb();
 
180
    var colorB = lastColor.getColorRgb();
 
181
    var colorAVal = colorA.getRgbArray();
 
182
    var colorBVal = colorB.getRgbArray();
 
183
    if (nbColors == 1) {
 
184
        return [colorA];
 
185
    }
 
186
    for (var i = 0; i < nbColors; i++) {
 
187
        var rgbTriplet = [];
 
188
        rgbTriplet[0] = colorAVal[0] + 
 
189
            i * (colorBVal[0] - colorAVal[0]) / (nbColors - 1);
 
190
        rgbTriplet[1] = colorAVal[1] + 
 
191
            i * (colorBVal[1] - colorAVal[1]) / (nbColors - 1);
 
192
        rgbTriplet[2] = colorAVal[2] + 
 
193
            i * (colorBVal[2] - colorAVal[2]) / (nbColors - 1);
 
194
        resultColors[i] = new mapfish.ColorRgb(parseInt(rgbTriplet[0]), 
 
195
            parseInt(rgbTriplet[1]), parseInt(rgbTriplet[2]));
 
196
    }
 
197
    return resultColors;
 
198
};