|
| 1 | +/** |
| 2 | +* A collection of methods useful for manipulating and comparing colors. |
| 3 | +* |
| 4 | +* @class ColorHarmony |
| 5 | +* @author Richard Davey <rich@photonstorm.com> |
| 6 | +* @copyright 2013 Photon Storm Ltd. |
| 7 | +* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License |
| 8 | +* @module Phaser |
| 9 | +*/ |
| 10 | + |
| 11 | +Phaser.Plugins.ColorHarmony.prototype = { |
| 12 | + |
| 13 | + /** |
| 14 | + * Returns a Complementary Color Harmony for the given color. |
| 15 | + * <p>A complementary hue is one directly opposite the color given on the color wheel</p> |
| 16 | + * <p>Value returned in 0xAARRGGBB format with Alpha set to 255.</p> |
| 17 | + * |
| 18 | + * @method getComplementHarmony |
| 19 | + * @param {Number} color The color to base the harmony on. |
| 20 | + * @return {Number} 0xAARRGGBB format color value. |
| 21 | + */ |
| 22 | + getComplementHarmony: function (color) { |
| 23 | + |
| 24 | + var hsv = Phaser.Color.RGBtoHSV(color); |
| 25 | + var opposite = Phaser.Color.game.math.wrapValue(hsv.hue, 180, 359); |
| 26 | + return Phaser.Color.HSVtoRGB(opposite, 1.0, 1.0); |
| 27 | + |
| 28 | + }, |
| 29 | + |
| 30 | + /** |
| 31 | + * Returns an Analogous Color Harmony for the given color. |
| 32 | + * <p>An Analogous harmony are hues adjacent to each other on the color wheel</p> |
| 33 | + * <p>Values returned in 0xAARRGGBB format with Alpha set to 255.</p> |
| 34 | + * |
| 35 | + * @method getAnalogousHarmony |
| 36 | + * @param {Number} color The color to base the harmony on. |
| 37 | + * @param {Number} threshold Control how adjacent the colors will be (default +- 30 degrees) |
| 38 | + * @return {Object} Object containing 3 properties: color1 (the original color), color2 (the warmer analogous color) and color3 (the colder analogous color) |
| 39 | + */ |
| 40 | + getAnalogousHarmony: function (color, threshold) { |
| 41 | + if (typeof threshold === "undefined") { threshold = 30; } |
| 42 | + var hsv = Phaser.Color.RGBtoHSV(color); |
| 43 | + if(threshold > 359 || threshold < 0) { |
| 44 | + throw Error("Color Warning: Invalid threshold given to getAnalogousHarmony()"); |
| 45 | + } |
| 46 | + var warmer = Phaser.Color.game.math.wrapValue(hsv.hue, 359 - threshold, 359); |
| 47 | + var colder = Phaser.Color.game.math.wrapValue(hsv.hue, threshold, 359); |
| 48 | + return { |
| 49 | + color1: color, |
| 50 | + color2: Phaser.Color.HSVtoRGB(warmer, 1.0, 1.0), |
| 51 | + color3: Phaser.Color.HSVtoRGB(colder, 1.0, 1.0), |
| 52 | + hue1: hsv.hue, |
| 53 | + hue2: warmer, |
| 54 | + hue3: colder |
| 55 | + }; |
| 56 | + }, |
| 57 | + |
| 58 | + /** |
| 59 | + * Returns an Split Complement Color Harmony for the given color. |
| 60 | + * <p>A Split Complement harmony are the two hues on either side of the color's Complement</p> |
| 61 | + * <p>Values returned in 0xAARRGGBB format with Alpha set to 255.</p> |
| 62 | + * |
| 63 | + * @method getSplitComplementHarmony |
| 64 | + * @param {Number} color The color to base the harmony on |
| 65 | + * @param {Number} threshold Control how adjacent the colors will be to the Complement (default +- 30 degrees) |
| 66 | + * @return {Object} An object containing 3 properties: color1 (the original color), color2 (the warmer analogous color) and color3 (the colder analogous color) |
| 67 | + */ |
| 68 | + getSplitComplementHarmony: function (color, threshold) { |
| 69 | + if (typeof threshold === "undefined") { threshold = 30; } |
| 70 | + var hsv = Phaser.Color.RGBtoHSV(color); |
| 71 | + if(threshold >= 359 || threshold <= 0) { |
| 72 | + throw Error("Phaser.Color Warning: Invalid threshold given to getSplitComplementHarmony()"); |
| 73 | + } |
| 74 | + var opposite = Phaser.Color.game.math.wrapValue(hsv.hue, 180, 359); |
| 75 | + var warmer = Phaser.Color.game.math.wrapValue(hsv.hue, opposite - threshold, 359); |
| 76 | + var colder = Phaser.Color.game.math.wrapValue(hsv.hue, opposite + threshold, 359); |
| 77 | + return { |
| 78 | + color1: color, |
| 79 | + color2: Phaser.Color.HSVtoRGB(warmer, hsv.saturation, hsv.value), |
| 80 | + color3: Phaser.Color.HSVtoRGB(colder, hsv.saturation, hsv.value), |
| 81 | + hue1: hsv.hue, |
| 82 | + hue2: warmer, |
| 83 | + hue3: colder |
| 84 | + }; |
| 85 | + }, |
| 86 | + |
| 87 | + /** |
| 88 | + * Returns a Triadic Color Harmony for the given color. |
| 89 | + * <p>A Triadic harmony are 3 hues equidistant from each other on the color wheel</p> |
| 90 | + * <p>Values returned in 0xAARRGGBB format with Alpha set to 255.</p> |
| 91 | + * |
| 92 | + * @method getTriadicHarmony |
| 93 | + * @param {Number} color The color to base the harmony on. |
| 94 | + * @return {Object} An Object containing 3 properties: color1 (the original color), color2 and color3 (the equidistant colors) |
| 95 | + */ |
| 96 | + getTriadicHarmony: function (color) { |
| 97 | + var hsv = Phaser.Color.RGBtoHSV(color); |
| 98 | + var triadic1 = Phaser.Color.game.math.wrapValue(hsv.hue, 120, 359); |
| 99 | + var triadic2 = Phaser.Color.game.math.wrapValue(triadic1, 120, 359); |
| 100 | + return { |
| 101 | + color1: color, |
| 102 | + color2: Phaser.Color.HSVtoRGB(triadic1, 1.0, 1.0), |
| 103 | + color3: Phaser.Color.HSVtoRGB(triadic2, 1.0, 1.0) |
| 104 | + }; |
| 105 | + }, |
| 106 | + |
| 107 | + /** |
| 108 | + * Get HSV color wheel values in an array which will be 360 elements in size. |
| 109 | + * |
| 110 | + * @method getHSVColorWheel |
| 111 | + * @param {Number} alpha Alpha value for each color of the color wheel, between 0 (transparent) and 255 (opaque) |
| 112 | + * @return {Array} An array containing 360 elements corresponding to the HSV color wheel. |
| 113 | + */ |
| 114 | + getHSVColorWheel: function (alpha) { |
| 115 | + |
| 116 | + alpha = alpha || 255; |
| 117 | + |
| 118 | + var colors = []; |
| 119 | + |
| 120 | + for (var c = 0; c <= 359; c++) |
| 121 | + { |
| 122 | + colors[c] = Phaser.Color.getWebRGB(Phaser.Color.HSVtoRGB(c, 1.0, 1.0, alpha)); |
| 123 | + } |
| 124 | + |
| 125 | + return colors; |
| 126 | + }, |
| 127 | + |
| 128 | + /** |
| 129 | + * Convert a HSV (hue, saturation, lightness) color space value to an RGB color |
| 130 | + * |
| 131 | + * @method HSVtoRGB |
| 132 | + * @param {Number} h Hue degree, between 0 and 359 |
| 133 | + * @param {Number} s Saturation, between 0.0 (grey) and 1.0 |
| 134 | + * @param {Number} v Value, between 0.0 (black) and 1.0 |
| 135 | + * @param {Number} alpha Alpha value to set per color (between 0 and 255) |
| 136 | + * @return {Number} 32-bit ARGB color value (0xAARRGGBB) |
| 137 | + */ |
| 138 | + HSVtoRGB: function (h, s, v, alpha) { |
| 139 | + if (typeof alpha === "undefined") { alpha = 255; } |
| 140 | + var result; |
| 141 | + if(s == 0.0) { |
| 142 | + result = Phaser.Color.getColor32(alpha, v * 255, v * 255, v * 255); |
| 143 | + } else { |
| 144 | + h = h / 60.0; |
| 145 | + var f = h - Math.floor(h); |
| 146 | + var p = v * (1.0 - s); |
| 147 | + var q = v * (1.0 - s * f); |
| 148 | + var t = v * (1.0 - s * (1.0 - f)); |
| 149 | + switch(Math.floor(h)) { |
| 150 | + case 0: |
| 151 | + result = Phaser.Color.getColor32(alpha, v * 255, t * 255, p * 255); |
| 152 | + break; |
| 153 | + case 1: |
| 154 | + result = Phaser.Color.getColor32(alpha, q * 255, v * 255, p * 255); |
| 155 | + break; |
| 156 | + case 2: |
| 157 | + result = Phaser.Color.getColor32(alpha, p * 255, v * 255, t * 255); |
| 158 | + break; |
| 159 | + case 3: |
| 160 | + result = Phaser.Color.getColor32(alpha, p * 255, q * 255, v * 255); |
| 161 | + break; |
| 162 | + case 4: |
| 163 | + result = Phaser.Color.getColor32(alpha, t * 255, p * 255, v * 255); |
| 164 | + break; |
| 165 | + case 5: |
| 166 | + result = Phaser.Color.getColor32(alpha, v * 255, p * 255, q * 255); |
| 167 | + break; |
| 168 | + default: |
| 169 | + throw new Error("Phaser.Color.HSVtoRGB : Unknown color"); |
| 170 | + } |
| 171 | + } |
| 172 | + return result; |
| 173 | + }, |
| 174 | + |
| 175 | + /** |
| 176 | + * Convert an RGB color value to an object containing the HSV color space values: Hue, Saturation and Lightness |
| 177 | + * |
| 178 | + * @method RGBtoHSV |
| 179 | + * @param {Number} color In format 0xRRGGBB |
| 180 | + * @return {Object} An Object with the properties hue (from 0 to 360), saturation (from 0 to 1.0) and lightness (from 0 to 1.0, also available under .value) |
| 181 | + */ |
| 182 | + RGBtoHSV: function (color) { |
| 183 | + var rgb = Phaser.Color.getRGB(color); |
| 184 | + var red = rgb.red / 255; |
| 185 | + var green = rgb.green / 255; |
| 186 | + var blue = rgb.blue / 255; |
| 187 | + var min = Math.min(red, green, blue); |
| 188 | + var max = Math.max(red, green, blue); |
| 189 | + var delta = max - min; |
| 190 | + var lightness = (max + min) / 2; |
| 191 | + var hue; |
| 192 | + var saturation; |
| 193 | + // Grey color, no chroma |
| 194 | + if(delta == 0) { |
| 195 | + hue = 0; |
| 196 | + saturation = 0; |
| 197 | + } else { |
| 198 | + if(lightness < 0.5) { |
| 199 | + saturation = delta / (max + min); |
| 200 | + } else { |
| 201 | + saturation = delta / (2 - max - min); |
| 202 | + } |
| 203 | + var delta_r = (((max - red) / 6) + (delta / 2)) / delta; |
| 204 | + var delta_g = (((max - green) / 6) + (delta / 2)) / delta; |
| 205 | + var delta_b = (((max - blue) / 6) + (delta / 2)) / delta; |
| 206 | + if(red == max) { |
| 207 | + hue = delta_b - delta_g; |
| 208 | + } else if(green == max) { |
| 209 | + hue = (1 / 3) + delta_r - delta_b; |
| 210 | + } else if(blue == max) { |
| 211 | + hue = (2 / 3) + delta_g - delta_r; |
| 212 | + } |
| 213 | + if(hue < 0) { |
| 214 | + hue += 1; |
| 215 | + } |
| 216 | + if(hue > 1) { |
| 217 | + hue -= 1; |
| 218 | + } |
| 219 | + } |
| 220 | + // Keep the value with 0 to 359 |
| 221 | + hue *= 360; |
| 222 | + hue = Math.round(hue); |
| 223 | + return { |
| 224 | + hue: hue, |
| 225 | + saturation: saturation, |
| 226 | + lightness: lightness, |
| 227 | + value: lightness |
| 228 | + }; |
| 229 | + } |
| 230 | + |
| 231 | +} |
0 commit comments