2 * This file has been edited substantially from the Rico-released version by
3 * the OpenLayers development team.
5 * This file is licensed under the Apache License, Version 2.0.
7 OpenLayers.Rico.Color = OpenLayers.Class({
9 initialize: function(red, green, blue) {
10 this.rgb = { r: red, g : green, b : blue };
17 setGreen: function(g) {
21 setBlue: function(b) {
27 // get an HSB model, and set the new hue...
28 var hsb = this.asHSB();
31 // convert back to RGB...
32 this.rgb = OpenLayers.Rico.Color.HSBtoRGB(hsb.h, hsb.s, hsb.b);
35 setSaturation: function(s) {
36 // get an HSB model, and set the new hue...
37 var hsb = this.asHSB();
40 // convert back to RGB and set values...
41 this.rgb = OpenLayers.Rico.Color.HSBtoRGB(hsb.h, hsb.s, hsb.b);
44 setBrightness: function(b) {
45 // get an HSB model, and set the new hue...
46 var hsb = this.asHSB();
49 // convert back to RGB and set values...
50 this.rgb = OpenLayers.Rico.Color.HSBtoRGB( hsb.h, hsb.s, hsb.b );
53 darken: function(percent) {
54 var hsb = this.asHSB();
55 this.rgb = OpenLayers.Rico.Color.HSBtoRGB(hsb.h, hsb.s, Math.max(hsb.b - percent,0));
58 brighten: function(percent) {
59 var hsb = this.asHSB();
60 this.rgb = OpenLayers.Rico.Color.HSBtoRGB(hsb.h, hsb.s, Math.min(hsb.b + percent,1));
63 blend: function(other) {
64 this.rgb.r = Math.floor((this.rgb.r + other.rgb.r)/2);
65 this.rgb.g = Math.floor((this.rgb.g + other.rgb.g)/2);
66 this.rgb.b = Math.floor((this.rgb.b + other.rgb.b)/2);
69 isBright: function() {
70 var hsb = this.asHSB();
71 return this.asHSB().b > 0.5;
75 return ! this.isBright();
79 return "rgb(" + this.rgb.r + "," + this.rgb.g + "," + this.rgb.b + ")";
83 return "#" + this.rgb.r.toColorPart() + this.rgb.g.toColorPart() + this.rgb.b.toColorPart();
87 return OpenLayers.Rico.Color.RGBtoHSB(this.rgb.r, this.rgb.g, this.rgb.b);
90 toString: function() {
96 OpenLayers.Rico.Color.createFromHex = function(hexCode) {
97 if(hexCode.length==4) {
98 var shortHexCode = hexCode;
100 for(var i=1;i<4;i++) {
101 hexCode += (shortHexCode.charAt(i) +
102 shortHexCode.charAt(i));
105 if ( hexCode.indexOf('#') == 0 ) {
106 hexCode = hexCode.substring(1);
108 var red = hexCode.substring(0,2);
109 var green = hexCode.substring(2,4);
110 var blue = hexCode.substring(4,6);
111 return new OpenLayers.Rico.Color( parseInt(red,16), parseInt(green,16), parseInt(blue,16) );
115 * Factory method for creating a color from the background of
118 OpenLayers.Rico.Color.createColorFromBackground = function(elem) {
121 RicoUtil.getElementsComputedStyle(OpenLayers.Util.getElement(elem),
125 if ( actualColor == "transparent" && elem.parentNode ) {
126 return OpenLayers.Rico.Color.createColorFromBackground(elem.parentNode);
128 if ( actualColor == null ) {
129 return new OpenLayers.Rico.Color(255,255,255);
131 if ( actualColor.indexOf("rgb(") == 0 ) {
132 var colors = actualColor.substring(4, actualColor.length - 1 );
133 var colorArray = colors.split(",");
134 return new OpenLayers.Rico.Color( parseInt( colorArray[0] ),
135 parseInt( colorArray[1] ),
136 parseInt( colorArray[2] ) );
139 else if ( actualColor.indexOf("#") == 0 ) {
140 return OpenLayers.Rico.Color.createFromHex(actualColor);
143 return new OpenLayers.Rico.Color(255,255,255);
147 OpenLayers.Rico.Color.HSBtoRGB = function(hue, saturation, brightness) {
153 if (saturation == 0) {
154 red = parseInt(brightness * 255.0 + 0.5);
159 var h = (hue - Math.floor(hue)) * 6.0;
160 var f = h - Math.floor(h);
161 var p = brightness * (1.0 - saturation);
162 var q = brightness * (1.0 - saturation * f);
163 var t = brightness * (1.0 - (saturation * (1.0 - f)));
165 switch (parseInt(h)) {
167 red = (brightness * 255.0 + 0.5);
168 green = (t * 255.0 + 0.5);
169 blue = (p * 255.0 + 0.5);
172 red = (q * 255.0 + 0.5);
173 green = (brightness * 255.0 + 0.5);
174 blue = (p * 255.0 + 0.5);
177 red = (p * 255.0 + 0.5);
178 green = (brightness * 255.0 + 0.5);
179 blue = (t * 255.0 + 0.5);
182 red = (p * 255.0 + 0.5);
183 green = (q * 255.0 + 0.5);
184 blue = (brightness * 255.0 + 0.5);
187 red = (t * 255.0 + 0.5);
188 green = (p * 255.0 + 0.5);
189 blue = (brightness * 255.0 + 0.5);
192 red = (brightness * 255.0 + 0.5);
193 green = (p * 255.0 + 0.5);
194 blue = (q * 255.0 + 0.5);
199 return { r : parseInt(red), g : parseInt(green) , b : parseInt(blue) };
202 OpenLayers.Rico.Color.RGBtoHSB = function(r, g, b) {
208 var cmax = (r > g) ? r : g;
212 var cmin = (r < g) ? r : g;
216 brightness = cmax / 255.0;
218 saturation = (cmax - cmin)/cmax;
222 if (saturation == 0) {
225 var redc = (cmax - r)/(cmax - cmin);
226 var greenc = (cmax - g)/(cmax - cmin);
227 var bluec = (cmax - b)/(cmax - cmin);
230 hue = bluec - greenc;
231 } else if (g == cmax) {
232 hue = 2.0 + redc - bluec;
234 hue = 4.0 + greenc - redc;
242 return { h : hue, s : saturation, b : brightness };