UNPKG

@nextcloud/vue

Version:
1 lines 6.93 kB
{"version":3,"file":"colors-Go3zmZRD.mjs","sources":["../../src/utils/colors.ts"],"sourcesContent":["/**\n * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n\nimport { t } from '../l10n.ts'\n\nexport class Color {\n\t/**\n\t * @param r - The red value\n\t * @param g - The green value\n\t * @param b - The blue value\n\t * @param name - The name of the color\n\t */\n\tconstructor(\n\t\tpublic readonly r: number,\n\t\tpublic readonly g: number,\n\t\tpublic readonly b: number,\n\t\tpublic readonly name?: string,\n\t) {\n\t\tthis.r = Math.min(r, 255)\n\t\tthis.g = Math.min(g, 255)\n\t\tthis.b = Math.min(b, 255)\n\t\tthis.name = name\n\t}\n\n\t/**\n\t * The hexadecimal color string.\n\t */\n\tget color(): string {\n\t\tconst toHex = (int: number) => `00${int.toString(16)}`.slice(-2)\n\t\treturn `#${toHex(this.r)}${toHex(this.g)}${toHex(this.b)}`\n\t}\n}\n\n/**\n * Calculate the increment needed to blend color1 into color2 within specified number of steps.\n *\n * @param steps - The number of steps\n * @param color1 - The color to start with\n * @param color2 - The color to end at\n * @return The increment needed per color channel\n */\nfunction calculateStepIncrement(steps: number, color1: Color, color2: Color): { r: number, g: number, b: number } {\n\treturn {\n\t\tr: (color2.r - color1.r) / steps,\n\t\tg: (color2.g - color1.g) / steps,\n\t\tb: (color2.b - color1.b) / steps,\n\t}\n}\n\n/**\n * Create a color palette from two colors\n *\n * @param steps - The number of steps the palette should have\n * @param color1 - The first color to mix\n * @param color2 - The second color to mix\n * @return The created palette array (mixed `color1` and `color2` with `steps` steps)\n */\nfunction mixPalette(steps: number, color1: Color, color2: Color): Color[] {\n\tconst palette: Color[] = []\n\tpalette.push(color1)\n\tconst increment = calculateStepIncrement(steps, color1, color2)\n\tfor (let i = 1; i < steps; i++) {\n\t\tconst r = Math.floor(color1.r + increment.r * i)\n\t\tconst g = Math.floor(color1.g + increment.g * i)\n\t\tconst b = Math.floor(color1.b + increment.b * i)\n\t\tpalette.push(new Color(r, g, b))\n\t}\n\treturn palette\n}\n\n// This are our default base colors we use for mixing palettes\nconst COLOR_RED = new Color(182, 70, 157, t('Purple'))\nconst COLOR_YELLOW = new Color(221, 203, 85, t('Gold'))\nconst COLOR_BLUE = new Color(0, 130, 201, t('Nextcloud blue'))\n// Special \"none\"-colors\nexport const COLOR_BLACK = new Color(0, 0, 0, t('Black'))\nexport const COLOR_WHITE = new Color(255, 255, 255, t('White'))\n\n/**\n * Like GenColor(4) but with labels\n */\nexport const defaultPalette = [\n\t/* eslint-disable @stylistic/function-call-argument-newline -- Allow to put translators comments */\n\tCOLOR_RED,\n\tnew Color(\n\t\t191, 103, 139,\n\t\tt('Rosy brown'), // TRANSLATORS: A color name for RGB(191, 103, 139)\n\t),\n\tnew Color(\n\t\t201, 136, 121,\n\t\tt('Feldspar'), // TRANSLATORS: A color name for RGB(201, 136, 121)\n\t),\n\tnew Color(\n\t\t211, 169, 103,\n\t\tt('Whiskey'), // TRANSLATORS: A color name for RGB(211, 169, 103)\n\t),\n\tCOLOR_YELLOW,\n\tnew Color(\n\t\t165, 184, 114,\n\t\tt('Olivine'), // TRANSLATORS: A color name for RGB(165, 184, 114)\n\t),\n\tnew Color(\n\t\t110, 166, 143,\n\t\tt('Acapulco'), // TRANSLATORS: A color name for RGB(110, 166, 143)\n\t),\n\tnew Color(\n\t\t55, 148, 172,\n\t\tt('Boston Blue'), // TRANSLATORS: A color name for RGB(55, 148, 172)\n\t),\n\tCOLOR_BLUE,\n\tnew Color(\n\t\t45, 115, 190,\n\t\tt('Mariner'), // TRANSLATORS: A color name for RGB(45, 115, 190)\n\t),\n\tnew Color(\n\t\t91, 100, 179,\n\t\tt('Blue Violet'), // TRANSLATORS: A color name for RGB(91, 100, 179)\n\t),\n\tnew Color(\n\t\t136, 85, 168,\n\t\tt('Deluge'), // TRANSLATORS: A color name for RGB(136, 85, 168)\n\t),\n\t/* eslint-enable @stylistic/function-call-argument-newline */\n]\n\n/**\n * Generate colors from the official nextcloud color\n * You can provide how many colors you want (multiplied by 3).\n *\n * For example if steps is set to 6, then 3 colors * 6 will result in 18 generated colors.\n *\n * @param steps - Number of steps to go from a color to another\n */\nexport function generatePalette(steps: number): Color[] {\n\tif (steps <= 0) {\n\t\tsteps = 6\n\t}\n\n\tif (steps === 4) {\n\t\treturn [...defaultPalette]\n\t}\n\n\tconst palette1 = mixPalette(steps, COLOR_RED, COLOR_YELLOW)\n\tconst palette2 = mixPalette(steps, COLOR_YELLOW, COLOR_BLUE)\n\tconst palette3 = mixPalette(steps, COLOR_BLUE, COLOR_RED)\n\n\treturn palette1.concat(palette2).concat(palette3)\n}\n"],"names":[],"mappings":";;AAOO,MAAM,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOlB,YACiB,GACA,GACA,GACA,MACf;AAJe,SAAA,IAAA;AACA,SAAA,IAAA;AACA,SAAA,IAAA;AACA,SAAA,OAAA;AAEhB,SAAK,IAAI,KAAK,IAAI,GAAG,GAAG;AACxB,SAAK,IAAI,KAAK,IAAI,GAAG,GAAG;AACxB,SAAK,IAAI,KAAK,IAAI,GAAG,GAAG;AACxB,SAAK,OAAO;AAAA,EACb;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,QAAgB;AACnB,UAAM,QAAQ,CAAC,QAAgB,KAAK,IAAI,SAAS,EAAE,CAAC,GAAG,MAAM,EAAE;AAC/D,WAAO,IAAI,MAAM,KAAK,CAAC,CAAC,GAAG,MAAM,KAAK,CAAC,CAAC,GAAG,MAAM,KAAK,CAAC,CAAC;AAAA,EACzD;AACD;AAUA,SAAS,uBAAuB,OAAe,QAAe,QAAoD;AACjH,SAAO;AAAA,IACN,IAAI,OAAO,IAAI,OAAO,KAAK;AAAA,IAC3B,IAAI,OAAO,IAAI,OAAO,KAAK;AAAA,IAC3B,IAAI,OAAO,IAAI,OAAO,KAAK;AAAA,EAAA;AAE7B;AAUA,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