1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
// Copyright 2006 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Utilities related to alpha/transparent colors and alpha color
* conversion.
*/
goog.provide('goog.color.alpha');
goog.require('goog.color');
/**
* Parses an alpha color out of a string.
* @param {string} str Color in some format.
* @return {{hex: string, type: string}} 'hex' is a string containing
* a hex representation of the color, and 'type' is a string
* containing the type of color format passed in ('hex', 'rgb', 'named').
*/
goog.color.alpha.parse = function(str) {
var result = {};
str = String(str);
var maybeHex = goog.color.prependHashIfNecessaryHelper(str);
if (goog.color.alpha.isValidAlphaHexColor_(maybeHex)) {
result.hex = goog.color.alpha.normalizeAlphaHex_(maybeHex);
result.type = 'hex';
return result;
} else {
var rgba = goog.color.alpha.isValidRgbaColor_(str);
if (rgba.length) {
result.hex = goog.color.alpha.rgbaArrayToHex(rgba);
result.type = 'rgba';
return result;
} else {
var hsla = goog.color.alpha.isValidHslaColor_(str);
if (hsla.length) {
result.hex = goog.color.alpha.hslaArrayToHex(hsla);
result.type = 'hsla';
return result;
}
}
}
throw new Error(str + ' is not a valid color string');
};
/**
* Converts a hex representation of a color to RGBA.
* @param {string} hexColor Color to convert.
* @return {string} string of the form 'rgba(R,G,B,A)' which can be used in
* styles.
*/
goog.color.alpha.hexToRgbaStyle = function(hexColor) {
return goog.color.alpha.rgbaStyle_(goog.color.alpha.hexToRgba(hexColor));
};
/**
* Extracts a substring, from startIdx to endIdx, of the normalized (lowercase
* #rrggbbaa) form of a hex-with-alpha color.
* @param {string} colorWithAlpha The alpha hex color to get the hex color from.
* This may be four or eight digits.
* @param {number} startIdx The start index within the #rrggbbaa color.
* @param {number} endIdx The end index within the #rrggbbbaa color.
* @return {string} The requested startIdx-to-endIdx substring from the color.
* @private
*/
goog.color.alpha.extractColor_ = function(colorWithAlpha, startIdx, endIdx) {
if (goog.color.alpha.isValidAlphaHexColor_(colorWithAlpha)) {
var fullColor = goog.color.prependHashIfNecessaryHelper(colorWithAlpha);
var normalizedColor = goog.color.alpha.normalizeAlphaHex_(fullColor);
return normalizedColor.substring(startIdx, endIdx);
} else {
throw new Error(colorWithAlpha + ' is not a valid 8-hex color string');
}
};
/**
* Gets the hex color part of an alpha hex color. For example, both '#abcd' and
* '#AABBCC12' return '#aabbcc'.
* @param {string} colorWithAlpha The alpha hex color to get the hex color from.
* @return {string} The hex color where the alpha part has been stripped off.
*/
goog.color.alpha.extractHexColor = function(colorWithAlpha) {
return goog.color.alpha.extractColor_(colorWithAlpha, 0, 7);
};
/**
* Gets the alpha color part of an alpha hex color. For example, both '#123A'
* and '#123456aa' return 'aa'. The result is always two characters long.
* @param {string} colorWithAlpha The alpha hex color to get the hex color from.
* @return {string} The two-character alpha from the given color.
*/
goog.color.alpha.extractAlpha = function(colorWithAlpha) {
return goog.color.alpha.extractColor_(colorWithAlpha, 7, 9);
};
/**
* Regular expression for extracting the digits in a hex color quadruplet.
* @type {RegExp}
* @private
*/
goog.color.alpha.hexQuadrupletRe_ = /#(.)(.)(.)(.)/;
/**
* Normalize a hex representation of an alpha color.
* @param {string} hexColor an alpha hex color string.
* @return {string} hex color in the format '#rrggbbaa' with all lowercase
* literals.
* @private
*/
goog.color.alpha.normalizeAlphaHex_ = function(hexColor) {
if (!goog.color.alpha.isValidAlphaHexColor_(hexColor)) {
throw new Error('\'' + hexColor + '\' is not a valid alpha hex color');
}
if (hexColor.length == 5) { // of the form #RGBA
hexColor = hexColor.replace(
goog.color.alpha.hexQuadrupletRe_, '#$1$1$2$2$3$3$4$4');
}
return hexColor.toLowerCase();
};
/**
* Converts an 8-hex representation of a color to RGBA.
* @param {string} hexColor Color to convert.
* @return {!Array<number>} array containing [r, g, b, a].
* r, g, b are ints between 0
* and 255, and a is a value between 0 and 1.
*/
goog.color.alpha.hexToRgba = function(hexColor) {
// TODO(user): Enhance code sharing with goog.color, for example by
// adding a goog.color.genericHexToRgb method.
hexColor = goog.color.alpha.normalizeAlphaHex_(hexColor);
var r = parseInt(hexColor.substr(1, 2), 16);
var g = parseInt(hexColor.substr(3, 2), 16);
var b = parseInt(hexColor.substr(5, 2), 16);
var a = parseInt(hexColor.substr(7, 2), 16);
return [r, g, b, a / 255];
};
/**
* Converts a color from RGBA to hex representation.
* @param {number} r Amount of red, int between 0 and 255.
* @param {number} g Amount of green, int between 0 and 255.
* @param {number} b Amount of blue, int between 0 and 255.
* @param {number} a Amount of alpha, float between 0 and 1.
* @return {string} hex representation of the color.
*/
goog.color.alpha.rgbaToHex = function(r, g, b, a) {
var intAlpha = Math.floor(a * 255);
if (isNaN(intAlpha) || intAlpha < 0 || intAlpha > 255) {
// TODO(user): The CSS spec says the value should be clamped.
throw new Error(
'"(' + r + ',' + g + ',' + b + ',' + a +
'") is not a valid RGBA color');
}
var hexA = goog.color.prependZeroIfNecessaryHelper(intAlpha.toString(16));
return goog.color.rgbToHex(r, g, b) + hexA;
};
/**
* Converts a color from HSLA to hex representation.
* @param {number} h Amount of hue, int between 0 and 360.
* @param {number} s Amount of saturation, int between 0 and 100.
* @param {number} l Amount of lightness, int between 0 and 100.
* @param {number} a Amount of alpha, float between 0 and 1.
* @return {string} hex representation of the color.
*/
goog.color.alpha.hslaToHex = function(h, s, l, a) {
var intAlpha = Math.floor(a * 255);
if (isNaN(intAlpha) || intAlpha < 0 || intAlpha > 255) {
// TODO(user): The CSS spec says the value should be clamped.
throw new Error(
'"(' + h + ',' + s + ',' + l + ',' + a +
'") is not a valid HSLA color');
}
var hexA = goog.color.prependZeroIfNecessaryHelper(intAlpha.toString(16));
return goog.color.hslToHex(h, s / 100, l / 100) + hexA;
};
/**
* Converts a color from RGBA to hex representation.
* @param {Array<number>} rgba Array of [r, g, b, a], with r, g, b in [0, 255]
* and a in [0, 1].
* @return {string} hex representation of the color.
*/
goog.color.alpha.rgbaArrayToHex = function(rgba) {
return goog.color.alpha.rgbaToHex(rgba[0], rgba[1], rgba[2], rgba[3]);
};
/**
* Converts a color from RGBA to an RGBA style string.
* @param {number} r Value of red, in [0, 255].
* @param {number} g Value of green, in [0, 255].
* @param {number} b Value of blue, in [0, 255].
* @param {number} a Value of alpha, in [0, 1].
* @return {string} An 'rgba(r,g,b,a)' string ready for use in a CSS rule.
*/
goog.color.alpha.rgbaToRgbaStyle = function(r, g, b, a) {
if (isNaN(r) || r < 0 || r > 255 || isNaN(g) || g < 0 || g > 255 ||
isNaN(b) || b < 0 || b > 255 || isNaN(a) || a < 0 || a > 1) {
throw new Error(
'"(' + r + ',' + g + ',' + b + ',' + a +
')" is not a valid RGBA color');
}
return goog.color.alpha.rgbaStyle_([r, g, b, a]);
};
/**
* Converts a color from RGBA to an RGBA style string.
* @param {(Array<number>|Float32Array)} rgba Array of [r, g, b, a],
* with r, g, b in [0, 255] and a in [0, 1].
* @return {string} An 'rgba(r,g,b,a)' string ready for use in a CSS rule.
*/
goog.color.alpha.rgbaArrayToRgbaStyle = function(rgba) {
return goog.color.alpha.rgbaToRgbaStyle(rgba[0], rgba[1], rgba[2], rgba[3]);
};
/**
* Converts a color from HSLA to hex representation.
* @param {Array<number>} hsla Array of [h, s, l, a], where h is an integer in
* [0, 360], s and l are integers in [0, 100], and a is in [0, 1].
* @return {string} hex representation of the color, such as '#af457eff'.
*/
goog.color.alpha.hslaArrayToHex = function(hsla) {
return goog.color.alpha.hslaToHex(hsla[0], hsla[1], hsla[2], hsla[3]);
};
/**
* Converts a color from HSLA to an RGBA style string.
* @param {Array<number>} hsla Array of [h, s, l, a], where h is and integer in
* [0, 360], s and l are integers in [0, 100], and a is in [0, 1].
* @return {string} An 'rgba(r,g,b,a)' string ready for use in a CSS rule.
*/
goog.color.alpha.hslaArrayToRgbaStyle = function(hsla) {
return goog.color.alpha.hslaToRgbaStyle(hsla[0], hsla[1], hsla[2], hsla[3]);
};
/**
* Converts a color from HSLA to an RGBA style string.
* @param {number} h Amount of hue, int between 0 and 360.
* @param {number} s Amount of saturation, int between 0 and 100.
* @param {number} l Amount of lightness, int between 0 and 100.
* @param {number} a Amount of alpha, float between 0 and 1.
* @return {string} An 'rgba(r,g,b,a)' string ready for use in a CSS rule.
* styles.
*/
goog.color.alpha.hslaToRgbaStyle = function(h, s, l, a) {
return goog.color.alpha.rgbaStyle_(goog.color.alpha.hslaToRgba(h, s, l, a));
};
/**
* Converts a color from HSLA color space to RGBA color space.
* @param {number} h Amount of hue, int between 0 and 360.
* @param {number} s Amount of saturation, int between 0 and 100.
* @param {number} l Amount of lightness, int between 0 and 100.
* @param {number} a Amount of alpha, float between 0 and 1.
* @return {!Array<number>} [r, g, b, a] values for the color, where r, g, b
* are integers in [0, 255] and a is a float in [0, 1].
*/
goog.color.alpha.hslaToRgba = function(h, s, l, a) {
return goog.color.hslToRgb(h, s / 100, l / 100).concat(a);
};
/**
* Converts a color from RGBA color space to HSLA color space.
* Modified from {@link http://en.wikipedia.org/wiki/HLS_color_space}.
* @param {number} r Value of red, in [0, 255].
* @param {number} g Value of green, in [0, 255].
* @param {number} b Value of blue, in [0, 255].
* @param {number} a Value of alpha, in [0, 255].
* @return {!Array<number>} [h, s, l, a] values for the color, with h an int in
* [0, 360] and s, l and a in [0, 1].
*/
goog.color.alpha.rgbaToHsla = function(r, g, b, a) {
return goog.color.rgbToHsl(r, g, b).concat(a);
};
/**
* Converts a color from RGBA color space to HSLA color space.
* @param {Array<number>} rgba [r, g, b, a] values for the color, each in
* [0, 255].
* @return {!Array<number>} [h, s, l, a] values for the color, with h in
* [0, 360] and s, l and a in [0, 1].
*/
goog.color.alpha.rgbaArrayToHsla = function(rgba) {
return goog.color.alpha.rgbaToHsla(rgba[0], rgba[1], rgba[2], rgba[3]);
};
/**
* Helper for isValidAlphaHexColor_.
* @type {RegExp}
* @private
*/
goog.color.alpha.validAlphaHexColorRe_ = /^#(?:[0-9a-f]{4}){1,2}$/i;
/**
* Checks if a string is a valid alpha hex color. We expect strings of the
* format #RRGGBBAA (ex: #1b3d5f5b) or #RGBA (ex: #3CAF == #33CCAAFF).
* @param {string} str String to check.
* @return {boolean} Whether the string is a valid alpha hex color.
* @private
*/
// TODO(user): Support percentages when goog.color also supports them.
goog.color.alpha.isValidAlphaHexColor_ = function(str) {
return goog.color.alpha.validAlphaHexColorRe_.test(str);
};
/**
* Helper for isNormalizedAlphaHexColor_.
* @type {RegExp}
* @private
*/
goog.color.alpha.normalizedAlphaHexColorRe_ = /^#[0-9a-f]{8}$/;
/**
* Checks if a string is a normalized alpha hex color.
* We expect strings of the format #RRGGBBAA (ex: #1b3d5f5b)
* using only lowercase letters.
* @param {string} str String to check.
* @return {boolean} Whether the string is a normalized hex color.
* @private
*/
goog.color.alpha.isNormalizedAlphaHexColor_ = function(str) {
return goog.color.alpha.normalizedAlphaHexColorRe_.test(str);
};
/**
* Regular expression for matching and capturing RGBA style strings. Helper for
* isValidRgbaColor_.
* @type {RegExp}
* @private
*/
goog.color.alpha.rgbaColorRe_ =
/^(?:rgba)?\((0|[1-9]\d{0,2}),\s?(0|[1-9]\d{0,2}),\s?(0|[1-9]\d{0,2}),\s?(0|1|0\.\d{0,10})\)$/i;
/**
* Regular expression for matching and capturing HSLA style strings. Helper for
* isValidHslaColor_.
* @type {RegExp}
* @private
*/
goog.color.alpha.hslaColorRe_ =
/^(?:hsla)\((0|[1-9]\d{0,2}),\s?(0|[1-9]\d{0,2})\%,\s?(0|[1-9]\d{0,2})\%,\s?(0|1|0\.\d{0,10})\)$/i;
/**
* Checks if a string is a valid rgba color. We expect strings of the format
* '(r, g, b, a)', or 'rgba(r, g, b, a)', where r, g, b are ints in [0, 255]
* and a is a float in [0, 1].
* @param {string} str String to check.
* @return {!Array<number>} the integers [r, g, b, a] for valid colors or the
* empty array for invalid colors.
* @private
*/
goog.color.alpha.isValidRgbaColor_ = function(str) {
// Each component is separate (rather than using a repeater) so we can
// capture the match. Also, we explicitly set each component to be either 0,
// or start with a non-zero, to prevent octal numbers from slipping through.
var regExpResultArray = str.match(goog.color.alpha.rgbaColorRe_);
if (regExpResultArray) {
var r = Number(regExpResultArray[1]);
var g = Number(regExpResultArray[2]);
var b = Number(regExpResultArray[3]);
var a = Number(regExpResultArray[4]);
if (r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255 &&
a >= 0 && a <= 1) {
return [r, g, b, a];
}
}
return [];
};
/**
* Checks if a string is a valid hsla color. We expect strings of the format
* 'hsla(h, s, l, a)', where s in an int in [0, 360], s and l are percentages
* between 0 and 100 such as '50%' or '70%', and a is a float in [0, 1].
* @param {string} str String to check.
* @return {!Array<number>} the integers [h, s, l, a] for valid colors or the
* empty array for invalid colors.
* @private
*/
goog.color.alpha.isValidHslaColor_ = function(str) {
// Each component is separate (rather than using a repeater) so we can
// capture the match. Also, we explicitly set each component to be either 0,
// or start with a non-zero, to prevent octal numbers from slipping through.
var regExpResultArray = str.match(goog.color.alpha.hslaColorRe_);
if (regExpResultArray) {
var h = Number(regExpResultArray[1]);
var s = Number(regExpResultArray[2]);
var l = Number(regExpResultArray[3]);
var a = Number(regExpResultArray[4]);
if (h >= 0 && h <= 360 && s >= 0 && s <= 100 && l >= 0 && l <= 100 &&
a >= 0 && a <= 1) {
return [h, s, l, a];
}
}
return [];
};
/**
* Takes an array of [r, g, b, a] and converts it into a string appropriate for
* CSS styles. The alpha channel value is rounded to 3 decimal places to make
* sure the produced string is not too long.
* @param {Array<number>} rgba [r, g, b, a] with r, g, b in [0, 255] and a
* in [0, 1].
* @return {string} string of the form 'rgba(r,g,b,a)'.
* @private
*/
goog.color.alpha.rgbaStyle_ = function(rgba) {
var roundedRgba = rgba.slice(0);
roundedRgba[3] = Math.round(rgba[3] * 1000) / 1000;
return 'rgba(' + roundedRgba.join(',') + ')';
};
/**
* Converts from h,s,v,a values to a hex string
* @param {number} h Hue, in [0, 1].
* @param {number} s Saturation, in [0, 1].
* @param {number} v Value, in [0, 255].
* @param {number} a Alpha, in [0, 1].
* @return {string} hex representation of the color.
*/
goog.color.alpha.hsvaToHex = function(h, s, v, a) {
var alpha = Math.floor(a * 255);
return goog.color.hsvArrayToHex([h, s, v]) +
goog.color.prependZeroIfNecessaryHelper(alpha.toString(16));
};
/**
* Converts from an HSVA array to a hex string
* @param {Array<number>} hsva Array of [h, s, v, a] in
* [[0, 1], [0, 1], [0, 255], [0, 1]].
* @return {string} hex representation of the color.
*/
goog.color.alpha.hsvaArrayToHex = function(hsva) {
return goog.color.alpha.hsvaToHex(hsva[0], hsva[1], hsva[2], hsva[3]);
};
|