| 1 |
// Copyright 2006 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
goog.provide('goog.colorTest'); |
| 15 |
goog.setTestOnly('goog.colorTest'); |
| 16 |
|
| 17 |
goog.require('goog.array'); |
| 18 |
goog.require('goog.color'); |
| 19 |
goog.require('goog.color.names'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
|
| 22 |
function testIsValidColor() { |
| 23 |
var goodColors = [ |
| 24 |
'#ffffff', '#ff7812', '#012345', '#Ff003D', '#3CA', '(255, 26, 75)', |
| 25 |
'RGB(2, 3, 4)', '(0,0,0)', 'white', 'blue' |
| 26 |
]; |
| 27 |
var badColors = [ |
| 28 |
'#xxxxxx', '8899000', 'not_color', '#1234567', 'fffffg', '(2555,0,0)', |
| 29 |
'(1,2,3,4)', 'rgb(1,20,)', 'RGB(20,20,20,)', 'omgwtfbbq' |
| 30 |
]; |
| 31 |
for (var i = 0; i < goodColors.length; i++) { |
| 32 |
assertTrue(goodColors[i], goog.color.isValidColor(goodColors[i])); |
| 33 |
} |
| 34 |
for (var i = 0; i < badColors.length; i++) { |
| 35 |
assertFalse(badColors[i], goog.color.isValidColor(badColors[i])); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
function testIsValidHexColor() { |
| 41 |
var goodHexColors = ['#ffffff', '#ff7812', '#012345', '#Ff003D', '#3CA']; |
| 42 |
var badHexColors = ['#xxxxxx', '889900', 'not_color', '#1234567', 'fffffg']; |
| 43 |
for (var i = 0; i < goodHexColors.length; i++) { |
| 44 |
assertTrue(goodHexColors[i], goog.color.isValidHexColor_(goodHexColors[i])); |
| 45 |
} |
| 46 |
for (var i = 0; i < badHexColors.length; i++) { |
| 47 |
assertFalse(badHexColors[i], goog.color.isValidHexColor_(badHexColors[i])); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
function testIsValidRgbColor() { |
| 53 |
var goodRgbColors = |
| 54 |
['(255, 26, 75)', 'RGB(2, 3, 4)', '(0,0,0)', 'rgb(255,255,255)']; |
| 55 |
var badRgbColors = |
| 56 |
['(2555,0,0)', '(1,2,3,4)', 'rgb(1,20,)', 'RGB(20,20,20,)']; |
| 57 |
for (var i = 0; i < goodRgbColors.length; i++) { |
| 58 |
assertEquals( |
| 59 |
goodRgbColors[i], goog.color.isValidRgbColor_(goodRgbColors[i]).length, |
| 60 |
3); |
| 61 |
} |
| 62 |
for (var i = 0; i < badRgbColors.length; i++) { |
| 63 |
assertEquals( |
| 64 |
badRgbColors[i], goog.color.isValidRgbColor_(badRgbColors[i]).length, |
| 65 |
0); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
function testParse() { |
| 71 |
var colors = |
| 72 |
['rgb(15, 250, 77)', '(127, 127, 127)', '#ffeedd', '123456', 'magenta']; |
| 73 |
var parsed = goog.array.map(colors, goog.color.parse); |
| 74 |
assertEquals('rgb', parsed[0].type); |
| 75 |
assertEquals(goog.color.rgbToHex(15, 250, 77), parsed[0].hex); |
| 76 |
assertEquals('rgb', parsed[1].type); |
| 77 |
assertEquals(goog.color.rgbToHex(127, 127, 127), parsed[1].hex); |
| 78 |
assertEquals('hex', parsed[2].type); |
| 79 |
assertEquals('#ffeedd', parsed[2].hex); |
| 80 |
assertEquals('hex', parsed[3].type); |
| 81 |
assertEquals('#123456', parsed[3].hex); |
| 82 |
assertEquals('named', parsed[4].type); |
| 83 |
assertEquals('#ff00ff', parsed[4].hex); |
| 84 |
|
| 85 |
var badColors = ['rgb(01, 1, 23)', '(256, 256, 256)', '#ffeeddaa']; |
| 86 |
for (var i = 0; i < badColors.length; i++) { |
| 87 |
var e = assertThrows(goog.partial(goog.color.parse, badColors[i])); |
| 88 |
assertContains('is not a valid color string', e.message); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
function testHexToRgb() { |
| 94 |
var testColors = [ |
| 95 |
['#B0FF2D', [176, 255, 45]], ['#b26e5f', [178, 110, 95]], |
| 96 |
['#66f', [102, 102, 255]] |
| 97 |
]; |
| 98 |
|
| 99 |
for (var i = 0; i < testColors.length; i++) { |
| 100 |
var r = goog.color.hexToRgb(testColors[i][0]); |
| 101 |
var t = testColors[i][1]; |
| 102 |
|
| 103 |
assertEquals('Red channel should match.', t[0], r[0]); |
| 104 |
assertEquals('Green channel should match.', t[1], r[1]); |
| 105 |
assertEquals('Blue channel should match.', t[2], r[2]); |
| 106 |
} |
| 107 |
|
| 108 |
var badColors = ['', '#g00', 'some words']; |
| 109 |
for (var i = 0; i < badColors.length; i++) { |
| 110 |
var e = assertThrows(goog.partial(goog.color.hexToRgb, badColors[i])); |
| 111 |
assertEquals("'" + badColors[i] + "' is not a valid hex color", e.message); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
function testHexToRgbStyle() { |
| 117 |
assertEquals('rgb(255,0,0)', goog.color.hexToRgbStyle(goog.color.names.red)); |
| 118 |
assertEquals('rgb(206,206,206)', goog.color.hexToRgbStyle('#cecece')); |
| 119 |
assertEquals('rgb(51,204,170)', goog.color.hexToRgbStyle('#3CA')); |
| 120 |
var badHexColors = ['#1234', null, undefined, '#.1234567890']; |
| 121 |
for (var i = 0; i < badHexColors.length; ++i) { |
| 122 |
var badHexColor = badHexColors[i]; |
| 123 |
var e = assertThrows(goog.partial(goog.color.hexToRgbStyle, badHexColor)); |
| 124 |
assertEquals("'" + badHexColor + "' is not a valid hex color", e.message); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
function testRgbToHex() { |
| 130 |
assertEquals(goog.color.names.red, goog.color.rgbToHex(255, 0, 0)); |
| 131 |
assertEquals('#af13ff', goog.color.rgbToHex(175, 19, 255)); |
| 132 |
var badRgb = [ |
| 133 |
[-1, -1, -1], [256, 0, 0], ['a', 'b', 'c'], [undefined, 5, 5], [1.2, 3, 4] |
| 134 |
]; |
| 135 |
for (var i = 0; i < badRgb.length; ++i) { |
| 136 |
var e = assertThrows(goog.partial(goog.color.rgbArrayToHex, badRgb[i])); |
| 137 |
assertContains('is not a valid RGB color', e.message); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
function testRgbToHsl() { |
| 143 |
var rgb = [255, 171, 32]; |
| 144 |
var hsl = goog.color.rgbArrayToHsl(rgb); |
| 145 |
assertEquals(37, hsl[0]); |
| 146 |
assertTrue(1.0 - hsl[1] < 0.01); |
| 147 |
assertTrue(hsl[2] - .5625 < 0.01); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
function testHslToRgb() { |
| 152 |
var hsl = [60, 0.5, 0.1]; |
| 153 |
var rgb = goog.color.hslArrayToRgb(hsl); |
| 154 |
assertEquals(38, rgb[0]); |
| 155 |
assertEquals(38, rgb[1]); |
| 156 |
assertEquals(13, rgb[2]); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
// Tests accuracy of HSL to RGB conversion |
| 161 |
function testHSLBidiToRGB() { |
| 162 |
var DELTA = 1; |
| 163 |
|
| 164 |
var color = [ |
| 165 |
[100, 56, 200], [255, 0, 0], [0, 0, 255], [0, 255, 0], [255, 255, 255], |
| 166 |
[0, 0, 0] |
| 167 |
]; |
| 168 |
|
| 169 |
for (var i = 0; i < color.length; i++) { |
| 170 |
colorConversionTestHelper( |
| 171 |
function(color) { |
| 172 |
return goog.color.rgbToHsl(color[0], color[1], color[2]); |
| 173 |
}, |
| 174 |
function(color) { |
| 175 |
return goog.color.hslToRgb(color[0], color[1], color[2]); |
| 176 |
}, |
| 177 |
color[i], DELTA); |
| 178 |
|
| 179 |
colorConversionTestHelper( |
| 180 |
function(color) { return goog.color.rgbArrayToHsl(color); }, |
| 181 |
function(color) { return goog.color.hslArrayToRgb(color); }, color[i], |
| 182 |
DELTA); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
// Tests HSV to RGB conversion |
| 188 |
function testHSVToRGB() { |
| 189 |
var DELTA = 1; |
| 190 |
|
| 191 |
var color = [ |
| 192 |
[100, 56, 200], [255, 0, 0], [0, 0, 255], [0, 255, 0], [255, 255, 255], |
| 193 |
[0, 0, 0] |
| 194 |
]; |
| 195 |
|
| 196 |
for (var i = 0; i < color.length; i++) { |
| 197 |
colorConversionTestHelper( |
| 198 |
function(color) { |
| 199 |
return goog.color.rgbToHsv(color[0], color[1], color[2]); |
| 200 |
}, |
| 201 |
function(color) { |
| 202 |
return goog.color.hsvToRgb(color[0], color[1], color[2]); |
| 203 |
}, |
| 204 |
color[i], DELTA); |
| 205 |
|
| 206 |
colorConversionTestHelper( |
| 207 |
function(color) { return goog.color.rgbArrayToHsv(color); }, |
| 208 |
function(color) { return goog.color.hsvArrayToRgb(color); }, color[i], |
| 209 |
DELTA); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
// Tests that HSV space is (0-360) for hue |
| 214 |
function testHSVSpecRangeIsCorrect() { |
| 215 |
var color = [0, 0, 255]; // Blue is in the middle of hue range |
| 216 |
|
| 217 |
var hsv = goog.color.rgbToHsv(color[0], color[1], color[2]); |
| 218 |
|
| 219 |
assertTrue("H in HSV space looks like it's not 0-360", hsv[0] > 1); |
| 220 |
} |
| 221 |
|
| 222 |
// Tests conversion between HSL and Hex |
| 223 |
function testHslToHex() { |
| 224 |
var DELTA = 1; |
| 225 |
|
| 226 |
var color = [[0, 0, 0], [20, 0.5, 0.5], [0, 0, 1], [255, .45, .76]]; |
| 227 |
|
| 228 |
for (var i = 0; i < color.length; i++) { |
| 229 |
colorConversionTestHelper( |
| 230 |
function(hsl) { return goog.color.hslToHex(hsl[0], hsl[1], hsl[2]); }, |
| 231 |
function(hex) { return goog.color.hexToHsl(hex); }, color[i], DELTA); |
| 232 |
|
| 233 |
colorConversionTestHelper( |
| 234 |
function(hsl) { return goog.color.hslArrayToHex(hsl); }, |
| 235 |
function(hex) { return goog.color.hexToHsl(hex); }, color[i], DELTA); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
// Tests conversion between HSV and Hex |
| 240 |
function testHsvToHex() { |
| 241 |
var DELTA = 1; |
| 242 |
|
| 243 |
var color = [[0, 0, 0], [.5, 0.5, 155], [0, 0, 255], [.7, .45, 21]]; |
| 244 |
|
| 245 |
for (var i = 0; i < color.length; i++) { |
| 246 |
colorConversionTestHelper( |
| 247 |
function(hsl) { return goog.color.hsvToHex(hsl[0], hsl[1], hsl[2]); }, |
| 248 |
function(hex) { return goog.color.hexToHsv(hex); }, color[i], DELTA); |
| 249 |
|
| 250 |
colorConversionTestHelper( |
| 251 |
function(hsl) { return goog.color.hsvArrayToHex(hsl); }, |
| 252 |
function(hex) { return goog.color.hexToHsv(hex); }, color[i], DELTA); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
/** |
| 258 |
* This helper method compares two RGB colors, checking that each color |
| 259 |
* component is the same. |
| 260 |
* @param {Array<number>} rgb1 Color represented by a 3-element array with |
| 261 |
* red, green, and blue values respectively, in the range [0, 255]. |
| 262 |
* @param {Array<number>} rgb2 Color represented by a 3-element array with |
| 263 |
* red, green, and blue values respectively, in the range [0, 255]. |
| 264 |
* @return {boolean} True if the colors are the same, false otherwise. |
| 265 |
*/ |
| 266 |
function rgbColorsAreEqual(rgb1, rgb2) { |
| 267 |
return (rgb1[0] == rgb2[0] && rgb1[1] == rgb2[1] && rgb1[2] == rgb2[2]); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
/** |
| 272 |
* This method runs unit tests against goog.color.blend(). Test cases include: |
| 273 |
* blending arbitrary colors with factors of 0 and 1, blending the same colors |
| 274 |
* using arbitrary factors, blending different colors of varying factors, |
| 275 |
* and blending colors using factors outside the expected range. |
| 276 |
*/ |
| 277 |
function testColorBlend() { |
| 278 |
// Define some RGB colors for our tests. |
| 279 |
var black = [0, 0, 0]; |
| 280 |
var blue = [0, 0, 255]; |
| 281 |
var gray = [128, 128, 128]; |
| 282 |
var green = [0, 255, 0]; |
| 283 |
var purple = [128, 0, 128]; |
| 284 |
var red = [255, 0, 0]; |
| 285 |
var yellow = [255, 255, 0]; |
| 286 |
var white = [255, 255, 255]; |
| 287 |
|
| 288 |
// Blend arbitrary colors, using 0 and 1 for factors. Using 0 should return |
| 289 |
// the first color, while using 1 should return the second color. |
| 290 |
var redWithNoGreen = goog.color.blend(red, green, 1); |
| 291 |
assertTrue('red + 0 * green = red', rgbColorsAreEqual(red, redWithNoGreen)); |
| 292 |
var whiteWithAllBlue = goog.color.blend(white, blue, 0); |
| 293 |
assertTrue( |
| 294 |
'white + 1 * blue = blue', rgbColorsAreEqual(blue, whiteWithAllBlue)); |
| 295 |
|
| 296 |
// Blend the same colors using arbitrary factors. This should return the |
| 297 |
// same colors. |
| 298 |
var greenWithGreen = goog.color.blend(green, green, .25); |
| 299 |
assertTrue( |
| 300 |
'green + .25 * green = green', rgbColorsAreEqual(green, greenWithGreen)); |
| 301 |
|
| 302 |
// Blend different colors using varying factors. |
| 303 |
var blackWithWhite = goog.color.blend(black, white, .5); |
| 304 |
assertTrue( |
| 305 |
'black + .5 * white = gray', rgbColorsAreEqual(gray, blackWithWhite)); |
| 306 |
var redAndBlue = goog.color.blend(red, blue, .5); |
| 307 |
assertTrue('red + .5 * blue = purple', rgbColorsAreEqual(purple, redAndBlue)); |
| 308 |
var lightGreen = goog.color.blend(green, white, .75); |
| 309 |
assertTrue( |
| 310 |
'green + .25 * white = a lighter shade of white', |
| 311 |
lightGreen[0] > 0 && lightGreen[1] == 255 && lightGreen[2] > 0); |
| 312 |
|
| 313 |
// Blend arbitrary colors using factors outside the expected range. |
| 314 |
var noGreenAllPurple = goog.color.blend(green, purple, -0.5); |
| 315 |
assertTrue( |
| 316 |
'green * -0.5 + purple = purple.', |
| 317 |
rgbColorsAreEqual(purple, noGreenAllPurple)); |
| 318 |
var allBlueNoYellow = goog.color.blend(blue, yellow, 1.37); |
| 319 |
assertTrue( |
| 320 |
'blue * 1.37 + yellow = blue.', rgbColorsAreEqual(blue, allBlueNoYellow)); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
/** |
| 325 |
* This method runs unit tests against goog.color.darken(). Test cases |
| 326 |
* include darkening black with arbitrary factors, edge cases (using 0 and 1), |
| 327 |
* darkening colors using various factors, and darkening colors using factors |
| 328 |
* outside the expected range. |
| 329 |
*/ |
| 330 |
function testColorDarken() { |
| 331 |
// Define some RGB colors |
| 332 |
var black = [0, 0, 0]; |
| 333 |
var green = [0, 255, 0]; |
| 334 |
var darkGray = [68, 68, 68]; |
| 335 |
var olive = [128, 128, 0]; |
| 336 |
var purple = [128, 0, 128]; |
| 337 |
var white = [255, 255, 255]; |
| 338 |
|
| 339 |
// Darken black by an arbitrary factor, which should still return black. |
| 340 |
var darkBlack = goog.color.darken(black, .63); |
| 341 |
assertTrue( |
| 342 |
'black darkened by .63 is still black.', |
| 343 |
rgbColorsAreEqual(black, darkBlack)); |
| 344 |
|
| 345 |
// Call darken() with edge-case factors (0 and 1). |
| 346 |
var greenNotDarkened = goog.color.darken(green, 0); |
| 347 |
assertTrue( |
| 348 |
'green darkened by 0 is still green.', |
| 349 |
rgbColorsAreEqual(green, greenNotDarkened)); |
| 350 |
var whiteFullyDarkened = goog.color.darken(white, 1); |
| 351 |
assertTrue( |
| 352 |
'white darkened by 1 is black.', |
| 353 |
rgbColorsAreEqual(black, whiteFullyDarkened)); |
| 354 |
|
| 355 |
// Call darken() with various colors and factors. The result should be |
| 356 |
// a color with less luminance. |
| 357 |
var whiteHsl = goog.color.rgbToHsl(white[0], white[1], white[2]); |
| 358 |
var whiteDarkened = goog.color.darken(white, .43); |
| 359 |
var whiteDarkenedHsl = |
| 360 |
goog.color.rgbToHsl(whiteDarkened[0], whiteDarkened[1], whiteDarkened[2]); |
| 361 |
assertTrue( |
| 362 |
'White that\'s darkened has less luminance than white.', |
| 363 |
whiteDarkenedHsl[2] < whiteHsl[2]); |
| 364 |
var purpleHsl = goog.color.rgbToHsl(purple[0], purple[1], purple[2]); |
| 365 |
var purpleDarkened = goog.color.darken(purple, .1); |
| 366 |
var purpleDarkenedHsl = goog.color.rgbToHsl( |
| 367 |
purpleDarkened[0], purpleDarkened[1], purpleDarkened[2]); |
| 368 |
assertTrue( |
| 369 |
'Purple that\'s darkened has less luminance than purple.', |
| 370 |
purpleDarkenedHsl[2] < purpleHsl[2]); |
| 371 |
|
| 372 |
// Call darken() with factors outside the expected range. |
| 373 |
var darkGrayTurnedBlack = goog.color.darken(darkGray, 2.1); |
| 374 |
assertTrue( |
| 375 |
'Darkening dark gray by 2.1 returns black.', |
| 376 |
rgbColorsAreEqual(black, darkGrayTurnedBlack)); |
| 377 |
var whiteNotDarkened = goog.color.darken(white, -0.62); |
| 378 |
assertTrue( |
| 379 |
'Darkening white by -0.62 returns white.', |
| 380 |
rgbColorsAreEqual(white, whiteNotDarkened)); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
/** |
| 385 |
* This method runs unit tests against goog.color.lighten(). Test cases |
| 386 |
* include lightening white with arbitrary factors, edge cases (using 0 and 1), |
| 387 |
* lightening colors using various factors, and lightening colors using factors |
| 388 |
* outside the expected range. |
| 389 |
*/ |
| 390 |
function testColorLighten() { |
| 391 |
// Define some RGB colors |
| 392 |
var black = [0, 0, 0]; |
| 393 |
var brown = [165, 42, 42]; |
| 394 |
var navy = [0, 0, 128]; |
| 395 |
var orange = [255, 165, 0]; |
| 396 |
var white = [255, 255, 255]; |
| 397 |
|
| 398 |
// Lighten white by an arbitrary factor, which should still return white. |
| 399 |
var lightWhite = goog.color.lighten(white, .41); |
| 400 |
assertTrue( |
| 401 |
'white lightened by .41 is still white.', |
| 402 |
rgbColorsAreEqual(white, lightWhite)); |
| 403 |
|
| 404 |
// Call lighten() with edge-case factors(0 and 1). |
| 405 |
var navyNotLightened = goog.color.lighten(navy, 0); |
| 406 |
assertTrue( |
| 407 |
'navy lightened by 0 is still navy.', |
| 408 |
rgbColorsAreEqual(navy, navyNotLightened)); |
| 409 |
var orangeFullyLightened = goog.color.lighten(orange, 1); |
| 410 |
assertTrue( |
| 411 |
'orange lightened by 1 is white.', |
| 412 |
rgbColorsAreEqual(white, orangeFullyLightened)); |
| 413 |
|
| 414 |
// Call lighten() with various colors and factors. The result should be |
| 415 |
// a color with greater luminance. |
| 416 |
var blackHsl = goog.color.rgbToHsl(black[0], black[1], black[2]); |
| 417 |
var blackLightened = goog.color.lighten(black, .33); |
| 418 |
var blackLightenedHsl = goog.color.rgbToHsl( |
| 419 |
blackLightened[0], blackLightened[1], blackLightened[2]); |
| 420 |
assertTrue( |
| 421 |
'Black that\'s lightened has more luminance than black.', |
| 422 |
blackLightenedHsl[2] >= blackHsl[2]); |
| 423 |
var orangeHsl = goog.color.rgbToHsl(orange[0], orange[1], orange[2]); |
| 424 |
var orangeLightened = goog.color.lighten(orange, .91); |
| 425 |
var orangeLightenedHsl = goog.color.rgbToHsl( |
| 426 |
orangeLightened[0], orangeLightened[1], orangeLightened[2]); |
| 427 |
assertTrue( |
| 428 |
'Orange that\'s lightened has more luminance than orange.', |
| 429 |
orangeLightenedHsl[2] >= orangeHsl[2]); |
| 430 |
|
| 431 |
// Call lighten() with factors outside the expected range. |
| 432 |
var navyTurnedWhite = goog.color.lighten(navy, 1.01); |
| 433 |
assertTrue( |
| 434 |
'Lightening navy by 1.01 returns white.', |
| 435 |
rgbColorsAreEqual(white, navyTurnedWhite)); |
| 436 |
var brownNotLightened = goog.color.lighten(brown, -0.0000001); |
| 437 |
assertTrue( |
| 438 |
'Lightening brown by -0.0000001 returns brown.', |
| 439 |
rgbColorsAreEqual(brown, brownNotLightened)); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
/** |
| 444 |
* This method runs unit tests against goog.color.hslDistance(). |
| 445 |
*/ |
| 446 |
function testHslDistance() { |
| 447 |
// Define some HSL colors |
| 448 |
var aliceBlueHsl = goog.color.rgbToHsl(240, 248, 255); |
| 449 |
var blackHsl = goog.color.rgbToHsl(0, 0, 0); |
| 450 |
var ghostWhiteHsl = goog.color.rgbToHsl(248, 248, 255); |
| 451 |
var navyHsl = goog.color.rgbToHsl(0, 0, 128); |
| 452 |
var redHsl = goog.color.rgbToHsl(255, 0, 0); |
| 453 |
var whiteHsl = goog.color.rgbToHsl(255, 255, 255); |
| 454 |
|
| 455 |
// The distance between the same colors should be 0. |
| 456 |
assertTrue( |
| 457 |
'There is no HSL distance between white and white.', |
| 458 |
goog.color.hslDistance(whiteHsl, whiteHsl) == 0); |
| 459 |
assertTrue( |
| 460 |
'There is no HSL distance between red and red.', |
| 461 |
goog.color.hslDistance(redHsl, redHsl) == 0); |
| 462 |
|
| 463 |
// The distance between various colors should be within certain thresholds. |
| 464 |
var hslDistance = goog.color.hslDistance(whiteHsl, ghostWhiteHsl); |
| 465 |
assertTrue( |
| 466 |
'The HSL distance between white and ghost white is > 0.', |
| 467 |
hslDistance > 0); |
| 468 |
assertTrue( |
| 469 |
'The HSL distance between white and ghost white is <= 0.02.', |
| 470 |
hslDistance <= 0.02); |
| 471 |
hslDistance = goog.color.hslDistance(whiteHsl, redHsl); |
| 472 |
assertTrue( |
| 473 |
'The HSL distance between white and red is > 0.02.', hslDistance > 0.02); |
| 474 |
hslDistance = goog.color.hslDistance(navyHsl, aliceBlueHsl); |
| 475 |
assertTrue( |
| 476 |
'The HSL distance between navy and alice blue is > 0.02.', |
| 477 |
hslDistance > 0.02); |
| 478 |
hslDistance = goog.color.hslDistance(blackHsl, whiteHsl); |
| 479 |
assertTrue( |
| 480 |
'The HSL distance between white and black is 1.', hslDistance == 1); |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
/** |
| 485 |
* This method runs unit tests against goog.color.yiqBrightness_(). |
| 486 |
*/ |
| 487 |
function testYiqBrightness() { |
| 488 |
var white = [255, 255, 255]; |
| 489 |
var black = [0, 0, 0]; |
| 490 |
var coral = [255, 127, 80]; |
| 491 |
var lightgreen = [144, 238, 144]; |
| 492 |
|
| 493 |
var whiteBrightness = goog.color.yiqBrightness_(white); |
| 494 |
var blackBrightness = goog.color.yiqBrightness_(black); |
| 495 |
var coralBrightness = goog.color.yiqBrightness_(coral); |
| 496 |
var lightgreenBrightness = goog.color.yiqBrightness_(lightgreen); |
| 497 |
|
| 498 |
// brightness should be a number |
| 499 |
assertTrue( |
| 500 |
'White brightness is a number.', typeof whiteBrightness == 'number'); |
| 501 |
assertTrue( |
| 502 |
'Coral brightness is a number.', typeof coralBrightness == 'number'); |
| 503 |
|
| 504 |
// brightness for known colors should match known values |
| 505 |
assertEquals('White brightness is 255', whiteBrightness, 255); |
| 506 |
assertEquals('Black brightness is 0', blackBrightness, 0); |
| 507 |
assertEquals('Coral brightness is 160', coralBrightness, 160); |
| 508 |
assertEquals('Lightgreen brightness is 199', lightgreenBrightness, 199); |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
/** |
| 513 |
* This method runs unit tests against goog.color.yiqBrightnessDiff_(). |
| 514 |
*/ |
| 515 |
function testYiqBrightnessDiff() { |
| 516 |
var colors = { |
| 517 |
'deeppink': [255, 20, 147], |
| 518 |
'indigo': [75, 0, 130], |
| 519 |
'saddlebrown': [139, 69, 19] |
| 520 |
}; |
| 521 |
|
| 522 |
var diffs = new Object(); |
| 523 |
for (name1 in colors) { |
| 524 |
for (name2 in colors) { |
| 525 |
diffs[name1 + '-' + name2] = |
| 526 |
goog.color.yiqBrightnessDiff_(colors[name1], colors[name2]); |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
for (pair in diffs) { |
| 531 |
// each brightness diff should be a number |
| 532 |
assertTrue(pair + ' diff is a number.', typeof diffs[pair] == 'number'); |
| 533 |
// each brightness diff should be greater than or equal to 0 |
| 534 |
assertTrue(pair + ' diff is greater than or equal to 0.', diffs[pair] >= 0); |
| 535 |
} |
| 536 |
|
| 537 |
// brightness diff for same-color pairs should be 0 |
| 538 |
assertEquals('deeppink-deeppink is 0.', diffs['deeppink-deeppink'], 0); |
| 539 |
assertEquals('indigo-indigo is 0.', diffs['indigo-indigo'], 0); |
| 540 |
|
| 541 |
// brightness diff for known pairs should match known values |
| 542 |
assertEquals('deeppink-indigo is 68.', diffs['deeppink-indigo'], 68); |
| 543 |
assertEquals( |
| 544 |
'saddlebrown-deeppink is 21.', diffs['saddlebrown-deeppink'], 21); |
| 545 |
|
| 546 |
// reversed pairs should have equal values |
| 547 |
assertEquals('indigo-saddlebrown is 47.', diffs['indigo-saddlebrown'], 47); |
| 548 |
assertEquals( |
| 549 |
'saddlebrown-indigo is also 47.', diffs['saddlebrown-indigo'], 47); |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
/** |
| 554 |
* This method runs unit tests against goog.color.colorDiff_(). |
| 555 |
*/ |
| 556 |
function testColorDiff() { |
| 557 |
var colors = { |
| 558 |
'mediumblue': [0, 0, 205], |
| 559 |
'oldlace': [253, 245, 230], |
| 560 |
'orchid': [218, 112, 214] |
| 561 |
}; |
| 562 |
|
| 563 |
var diffs = new Object(); |
| 564 |
for (name1 in colors) { |
| 565 |
for (name2 in colors) { |
| 566 |
diffs[name1 + '-' + name2] = |
| 567 |
goog.color.colorDiff_(colors[name1], colors[name2]); |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
for (pair in diffs) { |
| 572 |
// each color diff should be a number |
| 573 |
assertTrue(pair + ' diff is a number.', typeof diffs[pair] == 'number'); |
| 574 |
// each color diff should be greater than or equal to 0 |
| 575 |
assertTrue(pair + ' diff is greater than or equal to 0.', diffs[pair] >= 0); |
| 576 |
} |
| 577 |
|
| 578 |
// color diff for same-color pairs should be 0 |
| 579 |
assertEquals( |
| 580 |
'mediumblue-mediumblue is 0.', diffs['mediumblue-mediumblue'], 0); |
| 581 |
assertEquals('oldlace-oldlace is 0.', diffs['oldlace-oldlace'], 0); |
| 582 |
|
| 583 |
// color diff for known pairs should match known values |
| 584 |
assertEquals('mediumblue-oldlace is 523.', diffs['mediumblue-oldlace'], 523); |
| 585 |
assertEquals('oldlace-orchid is 184.', diffs['oldlace-orchid'], 184); |
| 586 |
|
| 587 |
// reversed pairs should have equal values |
| 588 |
assertEquals('orchid-mediumblue is 339.', diffs['orchid-mediumblue'], 339); |
| 589 |
assertEquals( |
| 590 |
'mediumblue-orchid is also 339.', diffs['mediumblue-orchid'], 339); |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
/** |
| 595 |
* This method runs unit tests against goog.color.highContrast(). |
| 596 |
*/ |
| 597 |
function testHighContrast() { |
| 598 |
white = [255, 255, 255]; |
| 599 |
black = [0, 0, 0]; |
| 600 |
lemonchiffron = [255, 250, 205]; |
| 601 |
sienna = [160, 82, 45]; |
| 602 |
|
| 603 |
var suggestion = |
| 604 |
goog.color.highContrast(black, [white, black, sienna, lemonchiffron]); |
| 605 |
|
| 606 |
// should return an array of three numbers |
| 607 |
assertTrue('Return value is an array.', typeof suggestion == 'object'); |
| 608 |
assertTrue('Return value is 3 long.', suggestion.length == 3); |
| 609 |
|
| 610 |
// known color combos should return a known (i.e. human-verified) suggestion |
| 611 |
assertArrayEquals( |
| 612 |
'White is best on sienna.', |
| 613 |
goog.color.highContrast(sienna, [white, black, sienna, lemonchiffron]), |
| 614 |
white); |
| 615 |
assertArrayEquals( |
| 616 |
'Black is best on lemonchiffron.', |
| 617 |
goog.color.highContrast(white, [white, black, sienna, lemonchiffron]), |
| 618 |
black); |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
/** |
| 623 |
* Helper function for color conversion functions between two colorspaces. |
| 624 |
* @param {Function} funcOne Function that converts from 1st colorspace to 2nd |
| 625 |
* @param {Function} funcTwo Function that converts from 2nd colorspace to 2nd |
| 626 |
* @param {Array<number>} color The color array passed to funcOne |
| 627 |
* @param {number} DELTA Margin of error for each element in color |
| 628 |
*/ |
| 629 |
function colorConversionTestHelper(funcOne, funcTwo, color, DELTA) { |
| 630 |
var temp = funcOne(color); |
| 631 |
|
| 632 |
if (!goog.color.isValidHexColor_(temp)) { |
| 633 |
assertTrue('First conversion had a NaN: ' + temp, !isNaN(temp[0])); |
| 634 |
assertTrue('First conversion had a NaN: ' + temp, !isNaN(temp[1])); |
| 635 |
assertTrue('First conversion had a NaN: ' + temp, !isNaN(temp[2])); |
| 636 |
} |
| 637 |
|
| 638 |
var back = funcTwo(temp); |
| 639 |
|
| 640 |
if (!goog.color.isValidHexColor_(temp)) { |
| 641 |
assertTrue('Second conversion had a NaN: ' + back, !isNaN(back[0])); |
| 642 |
assertTrue('Second conversion had a NaN: ' + back, !isNaN(back[1])); |
| 643 |
assertTrue('Second conversion had a NaN: ' + back, !isNaN(back[2])); |
| 644 |
} |
| 645 |
|
| 646 |
assertColorFuzzyEquals('Color was off', color, back, DELTA); |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
/** |
| 651 |
* Checks equivalence between two colors' respective values. Accepts +- delta |
| 652 |
* for each pair of values |
| 653 |
* @param {string} str |
| 654 |
* @param {Array<number>} expected |
| 655 |
* @param {Array<number>} actual |
| 656 |
* @param {number} delta Margin of error for each element in color array |
| 657 |
*/ |
| 658 |
function assertColorFuzzyEquals(str, expected, actual, delta) { |
| 659 |
assertTrue( |
| 660 |
str + ' Expected: ' + expected + ' and got: ' + actual + ' w/ delta: ' + |
| 661 |
delta, |
| 662 |
(Math.abs(expected[0] - actual[0]) <= delta) && |
| 663 |
(Math.abs(expected[1] - actual[1]) <= delta) && |
| 664 |
(Math.abs(expected[2] - actual[2]) <= delta)); |
| 665 |
} |