UNPKG

color-theme-generator

Version:

Generates random color themes that are based in color theory.

130 lines (129 loc) 12 kB
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var _MultiHueColorThemeFactory_instances, _MultiHueColorThemeFactory_hues, _MultiHueColorThemeFactory_lightness, _MultiHueColorThemeFactory_calculateHueFunction, _MultiHueColorThemeFactory_setHues, _MultiHueColorThemeFactory_setLightness, _MultiHueColorThemeFactory_setNumberOfMainColors, _MultiHueColorThemeFactory_generateColors, _MultiHueColorThemeFactory_getMainColors, _MultiHueColorThemeFactory_generateMainColor, _MultiHueColorThemeFactory_addHueToHues, _MultiHueColorThemeFactory_getContrastColors, _MultiHueColorThemeFactory_shouldGenerateContrastColors, _MultiHueColorThemeFactory_generateContrastColors, _MultiHueColorThemeFactory_shouldGenerateMultipleContrastColor, _MultiHueColorThemeFactory_generateMultipleContrastColors, _MultiHueColorThemeFactory_generateDarkColor, _MultiHueColorThemeFactory_getRandomHue, _MultiHueColorThemeFactory_getFirstIndex, _MultiHueColorThemeFactory_getLastIndex, _MultiHueColorThemeFactory_getHueFromHues, _MultiHueColorThemeFactory_generateLightColor, _MultiHueColorThemeFactory_generateSingleContrastColor, _MultiHueColorThemeFactory_shouldGenerateDarkContrastColor, _MultiHueColorThemeFactory_mergeContrastColorsWithMainColors; import { Color } from './Color.js'; import { ColorThemeFactory } from './ColorThemeFactory.js'; import { ColorValues } from '../enums/ColorValues.js'; import { MaxMinObject } from './MaxMinObject.js'; export class MultiHueColorThemeFactory extends ColorThemeFactory { constructor(numberOfMainColors) { super(); _MultiHueColorThemeFactory_instances.add(this); _MultiHueColorThemeFactory_hues.set(this, void 0); _MultiHueColorThemeFactory_lightness.set(this, void 0); _MultiHueColorThemeFactory_calculateHueFunction.set(this, void 0); __classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_setHues).call(this); __classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_setLightness).call(this, new MaxMinObject(ColorValues.LightnessMax, ColorValues.LightnessMin)); __classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_setNumberOfMainColors).call(this, numberOfMainColors); } setCalculateHueFunction(calculateHue) { __classPrivateFieldSet(this, _MultiHueColorThemeFactory_calculateHueFunction, calculateHue, "f"); } /** * Generates a set of colors in a color theme. * * @param numberOfColors - The number of colors to generate. * @returns An array containing the colors as Color objects. */ getColors(numberOfColors) { this.setHue(new MaxMinObject(ColorValues.HueMax, ColorValues.HueMin)); return __classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_generateColors).call(this, numberOfColors); } } _MultiHueColorThemeFactory_hues = new WeakMap(), _MultiHueColorThemeFactory_lightness = new WeakMap(), _MultiHueColorThemeFactory_calculateHueFunction = new WeakMap(), _MultiHueColorThemeFactory_instances = new WeakSet(), _MultiHueColorThemeFactory_setHues = function _MultiHueColorThemeFactory_setHues() { __classPrivateFieldSet(this, _MultiHueColorThemeFactory_hues, [], "f"); }, _MultiHueColorThemeFactory_setLightness = function _MultiHueColorThemeFactory_setLightness(limits) { __classPrivateFieldSet(this, _MultiHueColorThemeFactory_lightness, this.generator.generateRandomNumber(limits), "f"); }, _MultiHueColorThemeFactory_setNumberOfMainColors = function _MultiHueColorThemeFactory_setNumberOfMainColors(value) { this.numberOfMainColors = value; }, _MultiHueColorThemeFactory_generateColors = function _MultiHueColorThemeFactory_generateColors(numberOfColors) { const mainColors = __classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_getMainColors).call(this); const contrastColors = __classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_getContrastColors).call(this, numberOfColors); if (contrastColors) { return __classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_mergeContrastColorsWithMainColors).call(this, contrastColors, mainColors); } return mainColors; }, _MultiHueColorThemeFactory_getMainColors = function _MultiHueColorThemeFactory_getMainColors() { const colors = []; for (let i = 0; i < this.numberOfMainColors; i++) { colors.push(__classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_generateMainColor).call(this, i)); } return colors; }, _MultiHueColorThemeFactory_generateMainColor = function _MultiHueColorThemeFactory_generateMainColor(loopCount) { const hue = __classPrivateFieldGet(this, _MultiHueColorThemeFactory_calculateHueFunction, "f").call(this, loopCount); __classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_addHueToHues).call(this, hue); const saturation = this.generator.adjustNumberWithin10(this.saturation); return new Color(hue, saturation, __classPrivateFieldGet(this, _MultiHueColorThemeFactory_lightness, "f")); }, _MultiHueColorThemeFactory_addHueToHues = function _MultiHueColorThemeFactory_addHueToHues(hue) { __classPrivateFieldGet(this, _MultiHueColorThemeFactory_hues, "f").push(hue); }, _MultiHueColorThemeFactory_getContrastColors = function _MultiHueColorThemeFactory_getContrastColors(numberOfColorsInTheme) { if (__classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_shouldGenerateContrastColors).call(this, numberOfColorsInTheme)) { return __classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_generateContrastColors).call(this, numberOfColorsInTheme); } return []; }, _MultiHueColorThemeFactory_shouldGenerateContrastColors = function _MultiHueColorThemeFactory_shouldGenerateContrastColors(numberOfColorsInTheme) { const numberOfColorsForContrastColor = this.numberOfMainColors + 1; if (numberOfColorsInTheme >= numberOfColorsForContrastColor) { return true; } return false; }, _MultiHueColorThemeFactory_generateContrastColors = function _MultiHueColorThemeFactory_generateContrastColors(numberOfColorsInTheme) { if (__classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_shouldGenerateMultipleContrastColor).call(this, numberOfColorsInTheme)) { return __classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_generateMultipleContrastColors).call(this); } else { return __classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_generateSingleContrastColor).call(this); } }, _MultiHueColorThemeFactory_shouldGenerateMultipleContrastColor = function _MultiHueColorThemeFactory_shouldGenerateMultipleContrastColor(numberOfColorsInTheme) { const numberOfColorsForMultipleContrastColors = this.numberOfMainColors + 2; if (numberOfColorsInTheme === numberOfColorsForMultipleContrastColors) { return true; } return false; }, _MultiHueColorThemeFactory_generateMultipleContrastColors = function _MultiHueColorThemeFactory_generateMultipleContrastColors() { return [__classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_generateDarkColor).call(this), __classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_generateLightColor).call(this)]; }, _MultiHueColorThemeFactory_generateDarkColor = function _MultiHueColorThemeFactory_generateDarkColor() { const calculatedSaturation = this.generator.adjustNumberWithin10(this.saturation); const color = new Color(__classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_getRandomHue).call(this), calculatedSaturation, this.minLightness); return color; }, _MultiHueColorThemeFactory_getRandomHue = function _MultiHueColorThemeFactory_getRandomHue() { const firstIndex = __classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_getFirstIndex).call(this); const lastIndex = __classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_getLastIndex).call(this, __classPrivateFieldGet(this, _MultiHueColorThemeFactory_hues, "f")); const randomIndex = this.generator.generateRandomNumber(new MaxMinObject(lastIndex, firstIndex)); return __classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_getHueFromHues).call(this, randomIndex); }, _MultiHueColorThemeFactory_getFirstIndex = function _MultiHueColorThemeFactory_getFirstIndex() { return 0; }, _MultiHueColorThemeFactory_getLastIndex = function _MultiHueColorThemeFactory_getLastIndex(refrence) { return refrence.length - 1; }, _MultiHueColorThemeFactory_getHueFromHues = function _MultiHueColorThemeFactory_getHueFromHues(index) { return __classPrivateFieldGet(this, _MultiHueColorThemeFactory_hues, "f")[index]; }, _MultiHueColorThemeFactory_generateLightColor = function _MultiHueColorThemeFactory_generateLightColor() { const calculatedSaturation = this.generator.adjustNumberWithin10(this.saturation); const color = new Color(__classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_getRandomHue).call(this), calculatedSaturation, this.maxLightness); return color; }, _MultiHueColorThemeFactory_generateSingleContrastColor = function _MultiHueColorThemeFactory_generateSingleContrastColor() { if (__classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_shouldGenerateDarkContrastColor).call(this, __classPrivateFieldGet(this, _MultiHueColorThemeFactory_lightness, "f"))) { return [__classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_generateDarkColor).call(this)]; } else { return [__classPrivateFieldGet(this, _MultiHueColorThemeFactory_instances, "m", _MultiHueColorThemeFactory_generateLightColor).call(this)]; } }, _MultiHueColorThemeFactory_shouldGenerateDarkContrastColor = function _MultiHueColorThemeFactory_shouldGenerateDarkContrastColor(lightnessOfMainColors) { const fullLightness = 100; const halftLightness = fullLightness / 2; if (lightnessOfMainColors > halftLightness) { return true; } return false; }, _MultiHueColorThemeFactory_mergeContrastColorsWithMainColors = function _MultiHueColorThemeFactory_mergeContrastColorsWithMainColors(contrastColors, mainColors) { return [...mainColors, ...contrastColors]; };