1 /*global one*/ 2 one.include('js:one/color.js'); 3 one.include('js:one/color-installColorSpace.js'); 4 one.include('js:one/color/RGB.js'); 5 6 /** 7 * @name one.color.CMYK 8 * @class 9 * <p>A color in the CMYK colorspace, with an optional alpha value.</p> 10 * <p>one.color.(RGB|HSL|HSV|CMYK) objects are designed to be 11 * immutable; all the conversion, set, and adjust methods return new 12 * objects.</p> 13 * <p>one.color.(RGB|HSL|HSV|CMYK) objects automatically get the set 14 * and adjust methods from all other installed colorspaces, so 15 * although you can use the explicit conversion methods ({@link one.color.CMYK#toRGB}, 16 * {@link one.color.CMYK#toHSL}...), the below 17 * will work just fine:</p><pre><code> 18 one.include('jslib:one/color/CMYK.js'); 19 one.include('jslib:one/color/RGB.js'); 20 one.include('jslib:one/color/HSL.js'); 21 22 new one.color.CMYK(.4, .2, .4, .9, .2). // CMYK with alpha 23 setBlue(-.2). // Implicit conversion to RGB (with alpha) 24 adjustHue(-.1). // Implicit conversion to HSL(/HSV) (with alpha) 25 toCSSWithAlpha(); // "rgba(20,13,0,0.2)" 26 </code></pre> 27 * @static 28 * 29 * @constructor 30 * Create a new one.color.CMYK object. Component values outside the 31 * supported range, [0..1], will be adjusted automatically. 32 * @param {Number} c The cyan component, range: [0..1] 33 * @param {Number} m The magenta component, range: [0..1] 34 * @param {Number} y The yellow component, range: [0..1] 35 * @param {Number} k The black component, range: [0..1] 36 * @param {Number} [a] The alpha value, range: [0..1], 37 * defaults to 1 38 */ 39 40 /** 41 * @name one.color.CMYK.prototype.c 42 * @property 43 * @type Number 44 * @description The cyan component, range: [0..1] 45 */ 46 47 /** 48 * @name one.color.CMYK.prototype.m 49 * @property 50 * @type Number 51 * @description The magenta component, range: [0..1] 52 */ 53 54 /** 55 * @name one.color.CMYK.prototype.y 56 * @property 57 * @type Number 58 * @description The yellow component, range: [0..1] 59 */ 60 61 /** 62 * @name one.color.CMYK.prototype.k 63 * @property 64 * @type Number 65 * @description The black component, range: [0..1] 66 */ 67 68 /** 69 * @name one.color.CMYK.prototype.a 70 * @property 71 * @type Number 72 * @description The alpha value, range: [0..1] 73 */ 74 75 /** 76 * @name one.color.CMYK.prototype.setCyan 77 * @function 78 * @param {Number} c The new cyan component, range: [0..1] 79 * @return {one.color.CMYK} New color object with the changed value. 80 */ 81 82 /** 83 * @name one.color.CMYK.prototype.setMagenta 84 * @function 85 * @param {Number} m The new magenta component, range: [0..1] 86 * @return {one.color.CMYK} New color object with the changed value. 87 */ 88 89 /** 90 * @name one.color.CMYK.prototype.setYellow 91 * @function 92 * @param {Number} y The new yellow component, range: [0..1] 93 * @return {one.color.CMYK} New color object with the changed value. 94 */ 95 96 /** 97 * @name one.color.CMYK.prototype.setBlack 98 * @function 99 * @param {Number} k The new black component, range: [0..1] 100 * @return {one.color.CMYK} New color object with the changed value. 101 */ 102 103 /** 104 * @name one.color.CMYK.prototype.setAlpha 105 * @function 106 * @param {Number} a The new alpha value, range: [0..1] 107 * @return {one.color.CMYK} New color object with the changed value. 108 */ 109 110 /** 111 * @name one.color.CMYK.prototype.adjustCyan 112 * @function 113 * @param {Number} c The value to add to the cyan component. If the resulting 114 * value falls outside the supported range, [0..1], it will be 115 * adjusted automatically. 116 * @return {one.color.CMYK} New color object with the changed value. 117 */ 118 119 /** 120 * @name one.color.CMYK.prototype.adjustMagenta 121 * @function 122 * @param {Number} m The value to add to the magenta component. If the 123 * resulting value falls outside the supported range, [0..1], it will 124 * be adjusted automatically. 125 * @return {one.color.CMYK} New color object with the changed value. 126 */ 127 128 /** 129 * @name one.color.CMYK.prototype.adjustYellow 130 * @function 131 * @param {Number} y The value to add to the yellow component. If the resulting 132 * value falls outside the supported range, [0..1], it will be 133 * adjusted automatically. 134 * @return {one.color.CMYK} New color object with the changed value. 135 */ 136 137 /** 138 * @name one.color.CMYK.prototype.adjustBlack 139 * @function 140 * @param {Number} k The value to add to the black component. If the resulting 141 * value falls outside the supported range, [0..1], it will be 142 * adjusted automatically. 143 * @return {one.color.CMYK} New color object with the changed value. 144 */ 145 146 /** 147 * @name one.color.CMYK.prototype.adjustAlpha 148 * @function 149 * @param {Number} a The value to add to the alpha value. If the resulting 150 * value falls outside the supported range, [0..1], it will be 151 * adjusted automatically. 152 * @return {one.color.CMYK} New color object with the changed value. 153 */ 154 155 /** 156 * @name one.color.CMYK.prototype.toJSON 157 * @description Convert the color to a JSON representation. 158 * @function 159 * @return {Array} 160 */ 161 162 /** 163 * @name one.color.CMYK.prototype.toRGB 164 * @description Convert the color to a {@link one.color.RGB} object. 165 * @function 166 * @return {one.color.RGB} 167 */ 168 169 /** 170 * @name one.color.CMYK.prototype.toHSV 171 * @description Convert the color to a {@link one.color.HSV} object. 172 * @function 173 * @requires one.color.HSV 174 * @return {one.color.HSV} 175 */ 176 177 /** 178 * @name one.color.CMYK.prototype.toHSL 179 * @description Convert the color to a {@link one.color.HSL} object. 180 * @function 181 * @requires one.color.HSL 182 * @return {one.color.HSL} 183 */ 184 185 /** 186 * @name one.color.CMYK.prototype.toCMYK 187 * @description Convert the color to a {@link one.color.CMYK} object, ie. return the object itself. 188 * @function 189 * @return {one.color.CMYK} 190 */ 191 192 /** 193 * @name one.color.CMYK.prototype.toHex 194 * @description Get the standard RGB hex representation of the color. 195 * @function 196 * @return {String} The hex string, e.g. "#f681df" 197 */ 198 199 /** 200 * @name one.color.CMYK.prototype.toCSS 201 * @description Get a valid CSS color representation of the color without an alpha value. 202 * @function 203 * @return {String} The CSS color string, e.g. "rgb(123, 2, 202)" 204 */ 205 206 /** 207 * @name one.color.CMYK.prototype.toCSSWithAlpha 208 * @description Get a valid CSS color representation of the color, including the alpha value. 209 * @function 210 * @return {String} The CSS color string, e.g. "rgba(123, 2, 202, 0.253)" 211 */ 212 213 one.color.installColorSpace('CMYK', ['Cyan', 'Magenta', 'Yellow', 'blacK', 'Alpha'], { 214 toRGB: function () { 215 return new one.color.RGB((1 - this.c * (1 - this.k) - this.k), 216 (1 - this.m * (1 - this.k) - this.k), 217 (1 - this.y * (1 - this.k) - this.k), 218 this.a); 219 }, 220 221 fromRGB: function () { // Becomes one.color.RGB.prototype.toCMYK 222 // Adapted from http://www.javascripter.net/faq/rgb2cmyk.htm 223 var c = 1 - this.r, 224 m = 1 - this.g, 225 y = 1 - this.b, 226 k = 1; 227 if (this.r || this.g || this.b) { 228 k = Math.min(c, Math.min(m, y)); 229 c = (c - k) / (1 - k); 230 m = (m - k) / (1 - k); 231 y = (y - k) / (1 - k); 232 } else { 233 k = 1; 234 } 235 return new one.color.CMYK(c, m, y, k, this.a); 236 } 237 }); 238