[76] | 1 | /* |
---|
| 2 | * This file has been edited substantially from the Rico-released version by |
---|
| 3 | * the OpenLayers development team. |
---|
| 4 | * |
---|
| 5 | * This file is licensed under the Apache License, Version 2.0. |
---|
| 6 | */ |
---|
| 7 | OpenLayers.Rico.Color = OpenLayers.Class({ |
---|
| 8 | |
---|
| 9 | initialize: function(red, green, blue) { |
---|
| 10 | this.rgb = { r: red, g : green, b : blue }; |
---|
| 11 | }, |
---|
| 12 | |
---|
| 13 | setRed: function(r) { |
---|
| 14 | this.rgb.r = r; |
---|
| 15 | }, |
---|
| 16 | |
---|
| 17 | setGreen: function(g) { |
---|
| 18 | this.rgb.g = g; |
---|
| 19 | }, |
---|
| 20 | |
---|
| 21 | setBlue: function(b) { |
---|
| 22 | this.rgb.b = b; |
---|
| 23 | }, |
---|
| 24 | |
---|
| 25 | setHue: function(h) { |
---|
| 26 | |
---|
| 27 | // get an HSB model, and set the new hue... |
---|
| 28 | var hsb = this.asHSB(); |
---|
| 29 | hsb.h = h; |
---|
| 30 | |
---|
| 31 | // convert back to RGB... |
---|
| 32 | this.rgb = OpenLayers.Rico.Color.HSBtoRGB(hsb.h, hsb.s, hsb.b); |
---|
| 33 | }, |
---|
| 34 | |
---|
| 35 | setSaturation: function(s) { |
---|
| 36 | // get an HSB model, and set the new hue... |
---|
| 37 | var hsb = this.asHSB(); |
---|
| 38 | hsb.s = s; |
---|
| 39 | |
---|
| 40 | // convert back to RGB and set values... |
---|
| 41 | this.rgb = OpenLayers.Rico.Color.HSBtoRGB(hsb.h, hsb.s, hsb.b); |
---|
| 42 | }, |
---|
| 43 | |
---|
| 44 | setBrightness: function(b) { |
---|
| 45 | // get an HSB model, and set the new hue... |
---|
| 46 | var hsb = this.asHSB(); |
---|
| 47 | hsb.b = b; |
---|
| 48 | |
---|
| 49 | // convert back to RGB and set values... |
---|
| 50 | this.rgb = OpenLayers.Rico.Color.HSBtoRGB( hsb.h, hsb.s, hsb.b ); |
---|
| 51 | }, |
---|
| 52 | |
---|
| 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)); |
---|
| 56 | }, |
---|
| 57 | |
---|
| 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)); |
---|
| 61 | }, |
---|
| 62 | |
---|
| 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); |
---|
| 67 | }, |
---|
| 68 | |
---|
| 69 | isBright: function() { |
---|
| 70 | var hsb = this.asHSB(); |
---|
| 71 | return this.asHSB().b > 0.5; |
---|
| 72 | }, |
---|
| 73 | |
---|
| 74 | isDark: function() { |
---|
| 75 | return ! this.isBright(); |
---|
| 76 | }, |
---|
| 77 | |
---|
| 78 | asRGB: function() { |
---|
| 79 | return "rgb(" + this.rgb.r + "," + this.rgb.g + "," + this.rgb.b + ")"; |
---|
| 80 | }, |
---|
| 81 | |
---|
| 82 | asHex: function() { |
---|
| 83 | return "#" + this.rgb.r.toColorPart() + this.rgb.g.toColorPart() + this.rgb.b.toColorPart(); |
---|
| 84 | }, |
---|
| 85 | |
---|
| 86 | asHSB: function() { |
---|
| 87 | return OpenLayers.Rico.Color.RGBtoHSB(this.rgb.r, this.rgb.g, this.rgb.b); |
---|
| 88 | }, |
---|
| 89 | |
---|
| 90 | toString: function() { |
---|
| 91 | return this.asHex(); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | }); |
---|
| 95 | |
---|
| 96 | OpenLayers.Rico.Color.createFromHex = function(hexCode) { |
---|
| 97 | if(hexCode.length==4) { |
---|
| 98 | var shortHexCode = hexCode; |
---|
| 99 | var hexCode = '#'; |
---|
| 100 | for(var i=1;i<4;i++) { |
---|
| 101 | hexCode += (shortHexCode.charAt(i) + |
---|
| 102 | shortHexCode.charAt(i)); |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | if ( hexCode.indexOf('#') == 0 ) { |
---|
| 106 | hexCode = hexCode.substring(1); |
---|
| 107 | } |
---|
| 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) ); |
---|
| 112 | }; |
---|
| 113 | |
---|
| 114 | /** |
---|
| 115 | * Factory method for creating a color from the background of |
---|
| 116 | * an HTML element. |
---|
| 117 | */ |
---|
| 118 | OpenLayers.Rico.Color.createColorFromBackground = function(elem) { |
---|
| 119 | |
---|
| 120 | var actualColor = |
---|
| 121 | RicoUtil.getElementsComputedStyle(OpenLayers.Util.getElement(elem), |
---|
| 122 | "backgroundColor", |
---|
| 123 | "background-color"); |
---|
| 124 | |
---|
| 125 | if ( actualColor == "transparent" && elem.parentNode ) { |
---|
| 126 | return OpenLayers.Rico.Color.createColorFromBackground(elem.parentNode); |
---|
| 127 | } |
---|
| 128 | if ( actualColor == null ) { |
---|
| 129 | return new OpenLayers.Rico.Color(255,255,255); |
---|
| 130 | } |
---|
| 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] ) ); |
---|
| 137 | |
---|
| 138 | } |
---|
| 139 | else if ( actualColor.indexOf("#") == 0 ) { |
---|
| 140 | return OpenLayers.Rico.Color.createFromHex(actualColor); |
---|
| 141 | } |
---|
| 142 | else { |
---|
| 143 | return new OpenLayers.Rico.Color(255,255,255); |
---|
| 144 | } |
---|
| 145 | }; |
---|
| 146 | |
---|
| 147 | OpenLayers.Rico.Color.HSBtoRGB = function(hue, saturation, brightness) { |
---|
| 148 | |
---|
| 149 | var red = 0; |
---|
| 150 | var green = 0; |
---|
| 151 | var blue = 0; |
---|
| 152 | |
---|
| 153 | if (saturation == 0) { |
---|
| 154 | red = parseInt(brightness * 255.0 + 0.5); |
---|
| 155 | green = red; |
---|
| 156 | blue = red; |
---|
| 157 | } |
---|
| 158 | else { |
---|
| 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))); |
---|
| 164 | |
---|
| 165 | switch (parseInt(h)) { |
---|
| 166 | case 0: |
---|
| 167 | red = (brightness * 255.0 + 0.5); |
---|
| 168 | green = (t * 255.0 + 0.5); |
---|
| 169 | blue = (p * 255.0 + 0.5); |
---|
| 170 | break; |
---|
| 171 | case 1: |
---|
| 172 | red = (q * 255.0 + 0.5); |
---|
| 173 | green = (brightness * 255.0 + 0.5); |
---|
| 174 | blue = (p * 255.0 + 0.5); |
---|
| 175 | break; |
---|
| 176 | case 2: |
---|
| 177 | red = (p * 255.0 + 0.5); |
---|
| 178 | green = (brightness * 255.0 + 0.5); |
---|
| 179 | blue = (t * 255.0 + 0.5); |
---|
| 180 | break; |
---|
| 181 | case 3: |
---|
| 182 | red = (p * 255.0 + 0.5); |
---|
| 183 | green = (q * 255.0 + 0.5); |
---|
| 184 | blue = (brightness * 255.0 + 0.5); |
---|
| 185 | break; |
---|
| 186 | case 4: |
---|
| 187 | red = (t * 255.0 + 0.5); |
---|
| 188 | green = (p * 255.0 + 0.5); |
---|
| 189 | blue = (brightness * 255.0 + 0.5); |
---|
| 190 | break; |
---|
| 191 | case 5: |
---|
| 192 | red = (brightness * 255.0 + 0.5); |
---|
| 193 | green = (p * 255.0 + 0.5); |
---|
| 194 | blue = (q * 255.0 + 0.5); |
---|
| 195 | break; |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | return { r : parseInt(red), g : parseInt(green) , b : parseInt(blue) }; |
---|
| 200 | }; |
---|
| 201 | |
---|
| 202 | OpenLayers.Rico.Color.RGBtoHSB = function(r, g, b) { |
---|
| 203 | |
---|
| 204 | var hue; |
---|
| 205 | var saturation; |
---|
| 206 | var brightness; |
---|
| 207 | |
---|
| 208 | var cmax = (r > g) ? r : g; |
---|
| 209 | if (b > cmax) { |
---|
| 210 | cmax = b; |
---|
| 211 | } |
---|
| 212 | var cmin = (r < g) ? r : g; |
---|
| 213 | if (b < cmin) { |
---|
| 214 | cmin = b; |
---|
| 215 | } |
---|
| 216 | brightness = cmax / 255.0; |
---|
| 217 | if (cmax != 0) { |
---|
| 218 | saturation = (cmax - cmin)/cmax; |
---|
| 219 | } else { |
---|
| 220 | saturation = 0; |
---|
| 221 | } |
---|
| 222 | if (saturation == 0) { |
---|
| 223 | hue = 0; |
---|
| 224 | } else { |
---|
| 225 | var redc = (cmax - r)/(cmax - cmin); |
---|
| 226 | var greenc = (cmax - g)/(cmax - cmin); |
---|
| 227 | var bluec = (cmax - b)/(cmax - cmin); |
---|
| 228 | |
---|
| 229 | if (r == cmax) { |
---|
| 230 | hue = bluec - greenc; |
---|
| 231 | } else if (g == cmax) { |
---|
| 232 | hue = 2.0 + redc - bluec; |
---|
| 233 | } else { |
---|
| 234 | hue = 4.0 + greenc - redc; |
---|
| 235 | } |
---|
| 236 | hue = hue / 6.0; |
---|
| 237 | if (hue < 0) { |
---|
| 238 | hue = hue + 1.0; |
---|
| 239 | } |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | return { h : hue, s : saturation, b : brightness }; |
---|
| 243 | }; |
---|
| 244 | |
---|