2
* Copyright (C) 2007-2008 Camptocamp
4
* This file is part of MapFish Client
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.
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.
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/>.
21
* An abstract representation of color.
23
mapfish.Color = OpenLayers.Class({
24
getColorRgb: function() {}
28
* Class: mapfish.ColorRgb
29
* Class for representing RGB colors.
31
mapfish.ColorRgb = OpenLayers.Class(mapfish.Color, {
37
* Constructor: mapfish.ColorRgb
44
initialize: function(red, green, blue) {
46
this.greenLevel = green;
47
this.blueLevel = blue;
52
* Returns true if the colors at the same.
55
* {<mapfish.ColorRgb>} color
57
equals: function(color) {
58
return color.redLevel == this.redLevel &&
59
color.greenLevel == this.greenLevel &&
60
color.blueLevel == this.blueLevel;
63
getColorRgb: function() {
67
getRgbArray: function() {
76
* Method: hex2rgbArray
77
* Converts a Hex color string to an Rbg array
80
* rgbHexString - {String} Hex color string (format: #rrggbb)
82
hex2rgbArray: function(rgbHexString) {
83
if (rgbHexString.charAt(0) == '#') {
84
rgbHexString = rgbHexString.substr(1);
87
parseInt(rgbHexString.substring(0,2),16),
88
parseInt(rgbHexString.substring(2,4),16),
89
parseInt(rgbHexString.substring(4,6),16)
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");
100
* APIMethod: setFromHex
101
* Sets the color from a color hex string
104
* rgbHexString - {String} Hex color string (format: #rrggbb)
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];
114
* APIMethod: setFromRgb
115
* Sets the color from a color rgb string
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;
126
* APIMethod: toHexString
127
* Converts the rgb color to hex string
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;
139
* Converts a color level to its hexadecimal value
142
* dec - {Integer} Decimal value to convert [0..255]
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);
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
156
// return hexadecimal equivalent
157
return hexCharacters.charAt(i) + hexCharacters.charAt(j);
160
CLASS_NAME: "mapfish.ColorRgb"
164
* APIMethod: getColorsArrayByRgbInterpolation
165
* Get an array of colors based on RGB interpolation.
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.
173
* {Array({<mapfish.Color>})} The resulting array of colors.
175
mapfish.ColorRgb.getColorsArrayByRgbInterpolation =
176
function(firstColor, lastColor, nbColors) {
178
var resultColors = [];
179
var colorA = firstColor.getColorRgb();
180
var colorB = lastColor.getColorRgb();
181
var colorAVal = colorA.getRgbArray();
182
var colorBVal = colorB.getRgbArray();
186
for (var i = 0; i < nbColors; i++) {
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]));