|
| 1 | +/* |
| 2 | + * This file is part of prose-app-web |
| 3 | + * |
| 4 | + * Copyright 2025, Prose Foundation |
| 5 | + */ |
| 6 | + |
| 7 | +/************************************************************************** |
| 8 | + * IMPORTS |
| 9 | + * ************************************************************************* */ |
| 10 | + |
| 11 | +// NPM |
| 12 | +import hexRgb from "hex-rgb"; |
| 13 | +import colorMix from "mix-css-color"; |
| 14 | + |
| 15 | +/************************************************************************** |
| 16 | + * TYPES |
| 17 | + * ************************************************************************* */ |
| 18 | + |
| 19 | +type ColorHex = string; |
| 20 | +type ColorVar = string; |
| 21 | + |
| 22 | +/************************************************************************** |
| 23 | + * CONSTANTS |
| 24 | + * ************************************************************************* */ |
| 25 | + |
| 26 | +const RATIO_TO_PERCENT = 100; // 100% |
| 27 | + |
| 28 | +const COLOR_MIX_COLOR_BLACK = "black"; |
| 29 | +const COLOR_MIX_CORRECTIVE_FACTOR = 1.75; |
| 30 | + |
| 31 | +/************************************************************************** |
| 32 | + * FILTERS |
| 33 | + ***************************************************************************/ |
| 34 | + |
| 35 | +class FilterColor { |
| 36 | + hexVar(hex: ColorHex): ColorVar { |
| 37 | + // Convert hexadecimal to RGB, and return CSS-compatible 'R, G, B' \ |
| 38 | + // format, equivalent to the return value of the 'hex-var()' global \ |
| 39 | + // SCSS function. |
| 40 | + return hexRgb(hex, { |
| 41 | + format: "array" |
| 42 | + }) |
| 43 | + .slice(0, 3) |
| 44 | + .join(", "); |
| 45 | + } |
| 46 | + |
| 47 | + darkenVar(hex: ColorHex, ratio: number): ColorVar { |
| 48 | + // Convert hexadecimal to darkened RGB, and return CSS-compatible \ |
| 49 | + // 'R, G, B' format, equivalent to the return value of the \ |
| 50 | + // 'darken-var()' global SCSS function. |
| 51 | + // Notice: apply a corrective factor to the percent so that it looks \ |
| 52 | + // almost like SCSS darken() function. |
| 53 | + const mixedColor = colorMix( |
| 54 | + COLOR_MIX_COLOR_BLACK, |
| 55 | + hex, |
| 56 | + ratio * RATIO_TO_PERCENT * COLOR_MIX_CORRECTIVE_FACTOR |
| 57 | + ); |
| 58 | + |
| 59 | + return this.hexVar(mixedColor.hex); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/************************************************************************** |
| 64 | + * EXPORTS |
| 65 | + ***************************************************************************/ |
| 66 | + |
| 67 | +export default new FilterColor(); |
0 commit comments