UNPKG

color-theme-generator

Version:

Generates random color themes that are based in color theory.

53 lines (52 loc) 3.73 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 _MonochromeThemeFactory_instances, _MonochromeThemeFactory_validateArgument, _MonochromeThemeFactory_generateColors, _MonochromeThemeFactory_generateColor, _MonochromeThemeFactory_calculateLightnessOfMainColor; import { ArgumentLimits } from '../enums/ArgumentLimits.js'; import { Color } from './Color.js'; import { ColorThemeData } from './ColorThemeData.js'; import { ColorThemeFactory } from './ColorThemeFactory.js'; import { ColorThemes } from '../enums/ColorThemes.js'; import { ValidationObject } from './ValidationObject.js'; import { ColorValues } from '../enums/ColorValues.js'; import { MaxMinObject } from './MaxMinObject.js'; export class MonochromeThemeFactory extends ColorThemeFactory { constructor() { super(...arguments); _MonochromeThemeFactory_instances.add(this); } /** * Generates a monochrome color theme. * * @param numberOfColors - The number of colors to include ranging from 2 to 5. * @returns An object containing data about the generated color theme. * @throws Error if the arguments does not pass the validation. */ getColorTheme(numberOfColors) { __classPrivateFieldGet(this, _MonochromeThemeFactory_instances, "m", _MonochromeThemeFactory_validateArgument).call(this, numberOfColors); const colors = __classPrivateFieldGet(this, _MonochromeThemeFactory_instances, "m", _MonochromeThemeFactory_generateColors).call(this, numberOfColors); const data = new ColorThemeData(ColorThemes.Monochrome, colors); return data; } } _MonochromeThemeFactory_instances = new WeakSet(), _MonochromeThemeFactory_validateArgument = function _MonochromeThemeFactory_validateArgument(numberOfColors) { const validationValues = new ValidationObject(ArgumentLimits.MonochromeMax, ArgumentLimits.MonochromeMin, numberOfColors); this.validator.validateNumberArgumentWithMaxAndMin(validationValues); }, _MonochromeThemeFactory_generateColors = function _MonochromeThemeFactory_generateColors(numberOfColors) { this.setHue(new MaxMinObject(ColorValues.HueMax, ColorValues.HueMin)); const colors = []; for (let i = 0; i < numberOfColors; i++) { colors.push(__classPrivateFieldGet(this, _MonochromeThemeFactory_instances, "m", _MonochromeThemeFactory_generateColor).call(this, numberOfColors, i)); } return colors; }, _MonochromeThemeFactory_generateColor = function _MonochromeThemeFactory_generateColor(numberOfColors, loopCount) { const saturation = this.generator.adjustNumberWithin10(this.saturation); const lightness = __classPrivateFieldGet(this, _MonochromeThemeFactory_instances, "m", _MonochromeThemeFactory_calculateLightnessOfMainColor).call(this, numberOfColors, loopCount); return new Color(this.hue, saturation, lightness); }, _MonochromeThemeFactory_calculateLightnessOfMainColor = function _MonochromeThemeFactory_calculateLightnessOfMainColor(numberOfColors, lightIncrementFactor) { const increments = numberOfColors - 1; // 1 since the number of increments is one less than number of colors. const lightnessIncrement = (this.maxLightness - this.minLightness) / increments; return this.minLightness + (lightnessIncrement * lightIncrementFactor); };