admin.js
3 weeks ago
admin.min.js
3 weeks ago
chart.js
2 years ago
chart.min.js
1 year ago
customize-controls.js
1 year ago
customize-controls.min.js
1 year ago
customize-preview.js
1 year ago
customize-preview.min.js
1 year ago
deactivation-feedback.js
2 years ago
deactivation-feedback.min.js
2 years ago
editor.js
7 years ago
editor.min.js
5 years ago
everest-forms-survey-polls-quiz-builder.js
2 years ago
everest-forms-survey-polls-quiz-builder.min.js
2 years ago
evf-admin-email.js
2 years ago
evf-admin-email.min.js
1 year ago
evf-clipboard.js
7 years ago
evf-clipboard.min.js
7 years ago
evf-enhanced-select.js
3 years ago
evf-enhanced-select.min.js
2 years ago
evf-file-uploader.js
1 year ago
evf-file-uploader.min.js
1 year ago
evf-setup.js
1 year ago
evf-setup.min.js
1 year ago
extensions.js
2 years ago
extensions.min.js
2 years ago
form-builder.js
6 days ago
form-builder.min.js
6 days ago
form-template-controller.js
3 years ago
form-template-controller.min.js
3 years ago
printThis.min.js
2 years ago
progressbar.js
2 years ago
progressbar.min.js
1 year ago
randomColor.js
2 years ago
randomColor.min.js
2 years ago
settings.js
3 weeks ago
settings.min.js
3 weeks ago
shortcode-form-embed.js
2 years ago
shortcode-form-embed.min.js
2 years ago
tool-import-entries.js
2 years ago
tool-import-entries.min.js
2 years ago
tools.js
3 weeks ago
tools.min.js
3 weeks ago
upgrade.js
3 weeks ago
upgrade.min.js
3 weeks ago
randomColor.js
520 lines
| 1 | // randomColor by David Merfield under the CC0 license |
| 2 | // https://github.com/davidmerfield/randomColor/ |
| 3 | |
| 4 | ;(function(root, factory) { |
| 5 | |
| 6 | // Support CommonJS |
| 7 | if (typeof exports === 'object') { |
| 8 | var randomColor = factory(); |
| 9 | |
| 10 | // Support NodeJS & Component, which allow module.exports to be a function |
| 11 | if (typeof module === 'object' && module && module.exports) { |
| 12 | exports = module.exports = randomColor; |
| 13 | } |
| 14 | |
| 15 | // Support CommonJS 1.1.1 spec |
| 16 | exports.randomColor = randomColor; |
| 17 | |
| 18 | // Support AMD |
| 19 | } else if (typeof define === 'function' && define.amd) { |
| 20 | define([], factory); |
| 21 | |
| 22 | // Support vanilla script loading |
| 23 | } else { |
| 24 | root.randomColor = factory(); |
| 25 | } |
| 26 | |
| 27 | }(this, function() { |
| 28 | |
| 29 | // Seed to get repeatable colors |
| 30 | var seed = null; |
| 31 | |
| 32 | // Shared color dictionary |
| 33 | var colorDictionary = {}; |
| 34 | |
| 35 | // Populate the color dictionary |
| 36 | loadColorBounds(); |
| 37 | |
| 38 | // check if a range is taken |
| 39 | var colorRanges = []; |
| 40 | |
| 41 | var randomColor = function (options) { |
| 42 | |
| 43 | options = options || {}; |
| 44 | |
| 45 | // Check if there is a seed and ensure it's an |
| 46 | // integer. Otherwise, reset the seed value. |
| 47 | if (options.seed !== undefined && options.seed !== null && options.seed === parseInt(options.seed, 10)) { |
| 48 | seed = options.seed; |
| 49 | |
| 50 | // A string was passed as a seed |
| 51 | } else if (typeof options.seed === 'string') { |
| 52 | seed = stringToInteger(options.seed); |
| 53 | |
| 54 | // Something was passed as a seed but it wasn't an integer or string |
| 55 | } else if (options.seed !== undefined && options.seed !== null) { |
| 56 | throw new TypeError('The seed value must be an integer or string'); |
| 57 | |
| 58 | // No seed, reset the value outside. |
| 59 | } else { |
| 60 | seed = null; |
| 61 | } |
| 62 | |
| 63 | var H,S,B; |
| 64 | |
| 65 | // Check if we need to generate multiple colors |
| 66 | if (options.count !== null && options.count !== undefined) { |
| 67 | |
| 68 | var totalColors = options.count, |
| 69 | colors = []; |
| 70 | // Value false at index i means the range i is not taken yet. |
| 71 | for (var i = 0; i < options.count; i++) { |
| 72 | colorRanges.push(false) |
| 73 | } |
| 74 | options.count = null; |
| 75 | |
| 76 | while (totalColors > colors.length) { |
| 77 | |
| 78 | // Since we're generating multiple colors, |
| 79 | // incremement the seed. Otherwise we'd just |
| 80 | // generate the same color each time... |
| 81 | if (seed && options.seed) options.seed += 1; |
| 82 | |
| 83 | colors.push(randomColor(options)); |
| 84 | } |
| 85 | |
| 86 | options.count = totalColors; |
| 87 | |
| 88 | return colors; |
| 89 | } |
| 90 | |
| 91 | // First we pick a hue (H) |
| 92 | H = pickHue(options); |
| 93 | |
| 94 | // Then use H to determine saturation (S) |
| 95 | S = pickSaturation(H, options); |
| 96 | |
| 97 | // Then use S and H to determine brightness (B). |
| 98 | B = pickBrightness(H, S, options); |
| 99 | |
| 100 | // Then we return the HSB color in the desired format |
| 101 | return setFormat([H,S,B], options); |
| 102 | }; |
| 103 | |
| 104 | function pickHue(options) { |
| 105 | if (colorRanges.length > 0) { |
| 106 | var hueRange = getRealHueRange(options.hue) |
| 107 | |
| 108 | var hue = randomWithin(hueRange) |
| 109 | |
| 110 | //Each of colorRanges.length ranges has a length equal approximatelly one step |
| 111 | var step = (hueRange[1] - hueRange[0]) / colorRanges.length |
| 112 | |
| 113 | var j = parseInt((hue - hueRange[0]) / step) |
| 114 | |
| 115 | //Check if the range j is taken |
| 116 | if (colorRanges[j] === true) { |
| 117 | j = (j + 2) % colorRanges.length |
| 118 | } |
| 119 | else { |
| 120 | colorRanges[j] = true |
| 121 | } |
| 122 | |
| 123 | var min = (hueRange[0] + j * step) % 359, |
| 124 | max = (hueRange[0] + (j + 1) * step) % 359; |
| 125 | |
| 126 | hueRange = [min, max] |
| 127 | |
| 128 | hue = randomWithin(hueRange) |
| 129 | |
| 130 | if (hue < 0) {hue = 360 + hue;} |
| 131 | return hue |
| 132 | } |
| 133 | else { |
| 134 | var hueRange = getHueRange(options.hue) |
| 135 | |
| 136 | hue = randomWithin(hueRange); |
| 137 | // Instead of storing red as two seperate ranges, |
| 138 | // we group them, using negative numbers |
| 139 | if (hue < 0) { |
| 140 | hue = 360 + hue; |
| 141 | } |
| 142 | |
| 143 | return hue; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | function pickSaturation (hue, options) { |
| 148 | |
| 149 | if (options.hue === 'monochrome') { |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | if (options.luminosity === 'random') { |
| 154 | return randomWithin([0,100]); |
| 155 | } |
| 156 | |
| 157 | var saturationRange = getSaturationRange(hue); |
| 158 | |
| 159 | var sMin = saturationRange[0], |
| 160 | sMax = saturationRange[1]; |
| 161 | |
| 162 | switch (options.luminosity) { |
| 163 | |
| 164 | case 'bright': |
| 165 | sMin = 55; |
| 166 | break; |
| 167 | |
| 168 | case 'dark': |
| 169 | sMin = sMax - 10; |
| 170 | break; |
| 171 | |
| 172 | case 'light': |
| 173 | sMax = 55; |
| 174 | break; |
| 175 | } |
| 176 | |
| 177 | return randomWithin([sMin, sMax]); |
| 178 | |
| 179 | } |
| 180 | |
| 181 | function pickBrightness (H, S, options) { |
| 182 | |
| 183 | var bMin = getMinimumBrightness(H, S), |
| 184 | bMax = 100; |
| 185 | |
| 186 | switch (options.luminosity) { |
| 187 | |
| 188 | case 'dark': |
| 189 | bMax = bMin + 20; |
| 190 | break; |
| 191 | |
| 192 | case 'light': |
| 193 | bMin = (bMax + bMin)/2; |
| 194 | break; |
| 195 | |
| 196 | case 'random': |
| 197 | bMin = 0; |
| 198 | bMax = 100; |
| 199 | break; |
| 200 | } |
| 201 | |
| 202 | return randomWithin([bMin, bMax]); |
| 203 | } |
| 204 | |
| 205 | function setFormat (hsv, options) { |
| 206 | |
| 207 | switch (options.format) { |
| 208 | |
| 209 | case 'hsvArray': |
| 210 | return hsv; |
| 211 | |
| 212 | case 'hslArray': |
| 213 | return HSVtoHSL(hsv); |
| 214 | |
| 215 | case 'hsl': |
| 216 | var hsl = HSVtoHSL(hsv); |
| 217 | return 'hsl('+hsl[0]+', '+hsl[1]+'%, '+hsl[2]+'%)'; |
| 218 | |
| 219 | case 'hsla': |
| 220 | var hslColor = HSVtoHSL(hsv); |
| 221 | var alpha = options.alpha || Math.random(); |
| 222 | return 'hsla('+hslColor[0]+', '+hslColor[1]+'%, '+hslColor[2]+'%, ' + alpha + ')'; |
| 223 | |
| 224 | case 'rgbArray': |
| 225 | return HSVtoRGB(hsv); |
| 226 | |
| 227 | case 'rgb': |
| 228 | var rgb = HSVtoRGB(hsv); |
| 229 | return 'rgb(' + rgb.join(', ') + ')'; |
| 230 | |
| 231 | case 'rgba': |
| 232 | var rgbColor = HSVtoRGB(hsv); |
| 233 | var alpha = options.alpha || Math.random(); |
| 234 | return 'rgba(' + rgbColor.join(', ') + ', ' + alpha + ')'; |
| 235 | |
| 236 | default: |
| 237 | return HSVtoHex(hsv); |
| 238 | } |
| 239 | |
| 240 | } |
| 241 | |
| 242 | function getMinimumBrightness(H, S) { |
| 243 | |
| 244 | var lowerBounds = getColorInfo(H).lowerBounds; |
| 245 | |
| 246 | for (var i = 0; i < lowerBounds.length - 1; i++) { |
| 247 | |
| 248 | var s1 = lowerBounds[i][0], |
| 249 | v1 = lowerBounds[i][1]; |
| 250 | |
| 251 | var s2 = lowerBounds[i+1][0], |
| 252 | v2 = lowerBounds[i+1][1]; |
| 253 | |
| 254 | if (S >= s1 && S <= s2) { |
| 255 | |
| 256 | var m = (v2 - v1)/(s2 - s1), |
| 257 | b = v1 - m*s1; |
| 258 | |
| 259 | return m*S + b; |
| 260 | } |
| 261 | |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | function getHueRange (colorInput) { |
| 268 | |
| 269 | if (typeof parseInt(colorInput) === 'number') { |
| 270 | |
| 271 | var number = parseInt(colorInput); |
| 272 | |
| 273 | if (number < 360 && number > 0) { |
| 274 | return [number, number]; |
| 275 | } |
| 276 | |
| 277 | } |
| 278 | |
| 279 | if (typeof colorInput === 'string') { |
| 280 | |
| 281 | if (colorDictionary[colorInput]) { |
| 282 | var color = colorDictionary[colorInput]; |
| 283 | if (color.hueRange) {return color.hueRange;} |
| 284 | } else if (colorInput.match(/^#?([0-9A-F]{3}|[0-9A-F]{6})$/i)) { |
| 285 | var hue = HexToHSB(colorInput)[0]; |
| 286 | return [ hue, hue ]; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return [0,360]; |
| 291 | |
| 292 | } |
| 293 | |
| 294 | function getSaturationRange (hue) { |
| 295 | return getColorInfo(hue).saturationRange; |
| 296 | } |
| 297 | |
| 298 | function getColorInfo (hue) { |
| 299 | |
| 300 | // Maps red colors to make picking hue easier |
| 301 | if (hue >= 334 && hue <= 360) { |
| 302 | hue-= 360; |
| 303 | } |
| 304 | |
| 305 | for (var colorName in colorDictionary) { |
| 306 | var color = colorDictionary[colorName]; |
| 307 | if (color.hueRange && |
| 308 | hue >= color.hueRange[0] && |
| 309 | hue <= color.hueRange[1]) { |
| 310 | return colorDictionary[colorName]; |
| 311 | } |
| 312 | } return 'Color not found'; |
| 313 | } |
| 314 | |
| 315 | function randomWithin (range) { |
| 316 | if (seed === null) { |
| 317 | //generate random evenly destinct number from : https://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/ |
| 318 | var golden_ratio = 0.618033988749895 |
| 319 | var r=Math.random() |
| 320 | r += golden_ratio |
| 321 | r %= 1 |
| 322 | return Math.floor(range[0] + r*(range[1] + 1 - range[0])); |
| 323 | } else { |
| 324 | //Seeded random algorithm from http://indiegamr.com/generate-repeatable-random-numbers-in-js/ |
| 325 | var max = range[1] || 1; |
| 326 | var min = range[0] || 0; |
| 327 | seed = (seed * 9301 + 49297) % 233280; |
| 328 | var rnd = seed / 233280.0; |
| 329 | return Math.floor(min + rnd * (max - min)); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | function HSVtoHex (hsv){ |
| 334 | |
| 335 | var rgb = HSVtoRGB(hsv); |
| 336 | |
| 337 | function componentToHex(c) { |
| 338 | var hex = c.toString(16); |
| 339 | return hex.length == 1 ? '0' + hex : hex; |
| 340 | } |
| 341 | |
| 342 | var hex = '#' + componentToHex(rgb[0]) + componentToHex(rgb[1]) + componentToHex(rgb[2]); |
| 343 | |
| 344 | return hex; |
| 345 | |
| 346 | } |
| 347 | |
| 348 | function defineColor (name, hueRange, lowerBounds) { |
| 349 | |
| 350 | var sMin = lowerBounds[0][0], |
| 351 | sMax = lowerBounds[lowerBounds.length - 1][0], |
| 352 | |
| 353 | bMin = lowerBounds[lowerBounds.length - 1][1], |
| 354 | bMax = lowerBounds[0][1]; |
| 355 | |
| 356 | colorDictionary[name] = { |
| 357 | hueRange: hueRange, |
| 358 | lowerBounds: lowerBounds, |
| 359 | saturationRange: [sMin, sMax], |
| 360 | brightnessRange: [bMin, bMax] |
| 361 | }; |
| 362 | |
| 363 | } |
| 364 | |
| 365 | function loadColorBounds () { |
| 366 | |
| 367 | defineColor( |
| 368 | 'monochrome', |
| 369 | null, |
| 370 | [[0,0],[100,0]] |
| 371 | ); |
| 372 | |
| 373 | defineColor( |
| 374 | 'red', |
| 375 | [-26,18], |
| 376 | [[20,100],[30,92],[40,89],[50,85],[60,78],[70,70],[80,60],[90,55],[100,50]] |
| 377 | ); |
| 378 | |
| 379 | defineColor( |
| 380 | 'orange', |
| 381 | [19,46], |
| 382 | [[20,100],[30,93],[40,88],[50,86],[60,85],[70,70],[100,70]] |
| 383 | ); |
| 384 | |
| 385 | defineColor( |
| 386 | 'yellow', |
| 387 | [47,62], |
| 388 | [[25,100],[40,94],[50,89],[60,86],[70,84],[80,82],[90,80],[100,75]] |
| 389 | ); |
| 390 | |
| 391 | defineColor( |
| 392 | 'green', |
| 393 | [63,178], |
| 394 | [[30,100],[40,90],[50,85],[60,81],[70,74],[80,64],[90,50],[100,40]] |
| 395 | ); |
| 396 | |
| 397 | defineColor( |
| 398 | 'blue', |
| 399 | [179, 257], |
| 400 | [[20,100],[30,86],[40,80],[50,74],[60,60],[70,52],[80,44],[90,39],[100,35]] |
| 401 | ); |
| 402 | |
| 403 | defineColor( |
| 404 | 'purple', |
| 405 | [258, 282], |
| 406 | [[20,100],[30,87],[40,79],[50,70],[60,65],[70,59],[80,52],[90,45],[100,42]] |
| 407 | ); |
| 408 | |
| 409 | defineColor( |
| 410 | 'pink', |
| 411 | [283, 334], |
| 412 | [[20,100],[30,90],[40,86],[60,84],[80,80],[90,75],[100,73]] |
| 413 | ); |
| 414 | |
| 415 | } |
| 416 | |
| 417 | function HSVtoRGB (hsv) { |
| 418 | |
| 419 | // this doesn't work for the values of 0 and 360 |
| 420 | // here's the hacky fix |
| 421 | var h = hsv[0]; |
| 422 | if (h === 0) {h = 1;} |
| 423 | if (h === 360) {h = 359;} |
| 424 | |
| 425 | // Rebase the h,s,v values |
| 426 | h = h/360; |
| 427 | var s = hsv[1]/100, |
| 428 | v = hsv[2]/100; |
| 429 | |
| 430 | var h_i = Math.floor(h*6), |
| 431 | f = h * 6 - h_i, |
| 432 | p = v * (1 - s), |
| 433 | q = v * (1 - f*s), |
| 434 | t = v * (1 - (1 - f)*s), |
| 435 | r = 256, |
| 436 | g = 256, |
| 437 | b = 256; |
| 438 | |
| 439 | switch(h_i) { |
| 440 | case 0: r = v; g = t; b = p; break; |
| 441 | case 1: r = q; g = v; b = p; break; |
| 442 | case 2: r = p; g = v; b = t; break; |
| 443 | case 3: r = p; g = q; b = v; break; |
| 444 | case 4: r = t; g = p; b = v; break; |
| 445 | case 5: r = v; g = p; b = q; break; |
| 446 | } |
| 447 | |
| 448 | var result = [Math.floor(r*255), Math.floor(g*255), Math.floor(b*255)]; |
| 449 | return result; |
| 450 | } |
| 451 | |
| 452 | function HexToHSB (hex) { |
| 453 | hex = hex.replace(/^#/, ''); |
| 454 | hex = hex.length === 3 ? hex.replace(/(.)/g, '$1$1') : hex; |
| 455 | |
| 456 | var red = parseInt(hex.substr(0, 2), 16) / 255, |
| 457 | green = parseInt(hex.substr(2, 2), 16) / 255, |
| 458 | blue = parseInt(hex.substr(4, 2), 16) / 255; |
| 459 | |
| 460 | var cMax = Math.max(red, green, blue), |
| 461 | delta = cMax - Math.min(red, green, blue), |
| 462 | saturation = cMax ? (delta / cMax) : 0; |
| 463 | |
| 464 | switch (cMax) { |
| 465 | case red: return [ 60 * (((green - blue) / delta) % 6) || 0, saturation, cMax ]; |
| 466 | case green: return [ 60 * (((blue - red) / delta) + 2) || 0, saturation, cMax ]; |
| 467 | case blue: return [ 60 * (((red - green) / delta) + 4) || 0, saturation, cMax ]; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | function HSVtoHSL (hsv) { |
| 472 | var h = hsv[0], |
| 473 | s = hsv[1]/100, |
| 474 | v = hsv[2]/100, |
| 475 | k = (2-s)*v; |
| 476 | |
| 477 | return [ |
| 478 | h, |
| 479 | Math.round(s*v / (k<1 ? k : 2-k) * 10000) / 100, |
| 480 | k/2 * 100 |
| 481 | ]; |
| 482 | } |
| 483 | |
| 484 | function stringToInteger (string) { |
| 485 | var total = 0 |
| 486 | for (var i = 0; i !== string.length; i++) { |
| 487 | if (total >= Number.MAX_SAFE_INTEGER) break; |
| 488 | total += string.charCodeAt(i) |
| 489 | } |
| 490 | return total |
| 491 | } |
| 492 | |
| 493 | // get The range of given hue when options.count!=0 |
| 494 | function getRealHueRange(colorHue) |
| 495 | { if (!isNaN(colorHue)) { |
| 496 | var number = parseInt(colorHue); |
| 497 | |
| 498 | if (number < 360 && number > 0) { |
| 499 | return getColorInfo(colorHue).hueRange |
| 500 | } |
| 501 | } |
| 502 | else if (typeof colorHue === 'string') { |
| 503 | |
| 504 | if (colorDictionary[colorHue]) { |
| 505 | var color = colorDictionary[colorHue]; |
| 506 | |
| 507 | if (color.hueRange) { |
| 508 | return color.hueRange |
| 509 | } |
| 510 | } else if (colorHue.match(/^#?([0-9A-F]{3}|[0-9A-F]{6})$/i)) { |
| 511 | var hue = HexToHSB(colorHue)[0] |
| 512 | return getColorInfo(hue).hueRange |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | return [0,360] |
| 517 | } |
| 518 | return randomColor; |
| 519 | })); |
| 520 |