From ee25e634f55765bc3e628de1a3e454b9ad637f4d Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Wed, 30 Jun 2021 13:51:02 +0200 Subject: [PATCH] Add `ColorUtil.rgbaToHex()` --- ts/WoltLabSuite/Core/ColorUtil.ts | 38 +++++++++++++++++++ .../files/js/WoltLabSuite/Core/ColorUtil.js | 23 ++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/ts/WoltLabSuite/Core/ColorUtil.ts b/ts/WoltLabSuite/Core/ColorUtil.ts index cd6dea7f5f..a4c20ee3e3 100644 --- a/ts/WoltLabSuite/Core/ColorUtil.ts +++ b/ts/WoltLabSuite/Core/ColorUtil.ts @@ -189,12 +189,49 @@ export function rgbToHex(r: string | number, g?: number, b?: number): string { return rgbComponentToHex(r as number) + rgbComponentToHex(g) + rgbComponentToHex(b!); } +/** + * @since 5.5 + */ +function alphaToHex(alpha: number): string { + if (alpha < 0 || alpha > 1) { + throw new Error(`Invalid alpha value '${alpha}' given.`); + } + + return Math.round(alpha * 255) + .toString(16) + .padStart(2, "0") + .toUpperCase(); +} + +/** + * Converts a RGBA value into a HEX value. + * + * @since 5.5 + */ +export function rgbaToHex(rgba: RGBA): string; +export function rgbaToHex(r: number, g: number, b: number, a: number); +export function rgbaToHex(r: RGBA | number, g?: number, b?: number, a?: number): string { + if (g === undefined) { + const rgba = r as RGBA; + return rgbToHex(rgba.r, rgba.g, rgba.b) + alphaToHex(rgba.a); + } + + return rgbToHex(r as number, g, b!) + alphaToHex(a!); +} + export interface RGB { r: number; g: number; b: number; } +export interface RGBA { + r: number; + g: number; + b: number; + a: number; +} + export interface HSV { h: number; s: number; @@ -205,6 +242,7 @@ export interface HSV { window.__wcf_bc_colorUtil = { hexToRgb, hsvToRgb, + rgbaToHex, rgbToHex, rgbToHsv, }; diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/ColorUtil.js b/wcfsetup/install/files/js/WoltLabSuite/Core/ColorUtil.js index f52c402049..52d1b0c397 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/ColorUtil.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/ColorUtil.js @@ -10,7 +10,7 @@ define(["require", "exports"], function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); - exports.rgbToHex = exports.hexToRgb = exports.rgbToHsv = exports.hsvToRgb = void 0; + exports.rgbaToHex = exports.rgbToHex = exports.hexToRgb = exports.rgbToHsv = exports.hsvToRgb = void 0; /** * Converts a HSV color into RGB. * @@ -167,10 +167,31 @@ define(["require", "exports"], function (require, exports) { return rgbComponentToHex(r) + rgbComponentToHex(g) + rgbComponentToHex(b); } exports.rgbToHex = rgbToHex; + /** + * @since 5.5 + */ + function alphaToHex(alpha) { + if (alpha < 0 || alpha > 1) { + throw new Error(`Invalid alpha value '${alpha}' given.`); + } + return Math.round(alpha * 255) + .toString(16) + .padStart(2, "0") + .toUpperCase(); + } + function rgbaToHex(r, g, b, a) { + if (g === undefined) { + const rgba = r; + return rgbToHex(rgba.r, rgba.g, rgba.b) + alphaToHex(rgba.a); + } + return rgbToHex(r, g, b) + alphaToHex(a); + } + exports.rgbaToHex = rgbaToHex; // WCF.ColorPicker compatibility (color format conversion) window.__wcf_bc_colorUtil = { hexToRgb, hsvToRgb, + rgbaToHex, rgbToHex, rgbToHsv, }; -- 2.20.1