messageColors: rgba support/space instead of comma

This commit is contained in:
simidzu2ay 2024-07-12 22:09:22 +02:00
parent c7998d1b0e
commit a367429f2b
2 changed files with 26 additions and 5 deletions

View file

@ -37,12 +37,27 @@ export const settings = definePluginSettings({
export const enum ColorType {
RGB,
RGBA,
HEX,
HSL
}
// It's sooo hard to read regex without this, it makes it at least somewhat bearable
export const replaceRegexp = (reg: string) => {
const n = new RegExp(reg
// \c - 'comma'
// \v - 'value'
// \f - 'float'
.replaceAll("\\f", "[+-]?([0-9]*[.])?[0-9]+")
.replaceAll("\\c", "(?:,|\\s)")
.replaceAll("\\v", "\\s*?\\d+?\\s*?"), "g");
return n;
};
export const regex = [
{ reg: /rgb\(\s*?\d+?\s*?,\s*?\d+?\s*?,\s*?\d+?\s*?\)/g, type: ColorType.RGB },
{ reg: /hsl\(\s*\d+\s*°?,\s*\d+%\s*,\s*\d+%\s*\)/g, type: ColorType.HSL },
{ reg: /rgb\(\v\c\v\c\v\)/g, type: ColorType.RGB },
{ reg: /rgba\(\v\c\v\c\v(\c|\/?)\s*\f\)/g, type: ColorType.RGBA },
{ reg: /hsl\(\v°?\c\s*?\d+%?\s*?\c\s*?\d+%?\s*?\)/g, type: ColorType.HSL },
{ reg: /#(?:[0-9a-fA-F]{3}){1,2}/g, type: ColorType.HEX }
];
].map(v => { v.reg = replaceRegexp(v.reg.source); return v; });

View file

@ -10,7 +10,7 @@ import { Devs } from "@utils/constants";
import definePlugin from "@utils/types";
import { React } from "@webpack/common";
import { ColorType, regex, RenderType, settings } from "./constants";
import { ColorType, regex, RenderType, replaceRegexp, settings } from "./constants";
const source = regex.map(r => r.reg.source).join("|");
const matchAllRegExp = new RegExp(`^(${source})`, "i");
@ -129,6 +129,7 @@ const calcRGBLightness = (r: number, g: number, b: number) => {
};
const isColorDark = (color: string, type: ColorType): boolean => {
switch (type) {
case ColorType.RGBA:
case ColorType.RGB: {
const match = color.match(/\d+/g)!;
const lightness = calcRGBLightness(+match[0], +match[1], +match[2]);
@ -154,16 +155,21 @@ const getColorType = (color: string): ColorType => {
color = color.toLowerCase().trim();
if (color.startsWith("#")) return ColorType.HEX;
if (color.startsWith("hsl")) return ColorType.HSL;
if (color.startsWith("rgba")) return ColorType.RGBA;
if (color.startsWith("rgb")) return ColorType.RGB;
throw new Error(`Can't resolve color type of ${color}`);
};
function parseColor(str: string, type: ColorType): string {
str = str.toLowerCase().trim();
str = str.toLowerCase().trim().replaceAll(/(\s|,)+/g, " ");
switch (type) {
case ColorType.RGB:
return str;
case ColorType.RGBA:
if (!str.includes("/"))
return str.replaceAll(replaceRegexp(/\f(?=\s*?\))/.source), "/$&");
return str;
case ColorType.HEX:
return str[0] === "#" ? str : `#${str}`;
case ColorType.HSL: