mirror of
https://github.com/Vendicated/Vencord
synced 2024-09-13 04:29:24 -04:00
messageColors: rgba support/space instead of comma
This commit is contained in:
parent
c7998d1b0e
commit
a367429f2b
2 changed files with 26 additions and 5 deletions
|
@ -37,12 +37,27 @@ export const settings = definePluginSettings({
|
||||||
|
|
||||||
export const enum ColorType {
|
export const enum ColorType {
|
||||||
RGB,
|
RGB,
|
||||||
|
RGBA,
|
||||||
HEX,
|
HEX,
|
||||||
HSL
|
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 = [
|
export const regex = [
|
||||||
{ reg: /rgb\(\s*?\d+?\s*?,\s*?\d+?\s*?,\s*?\d+?\s*?\)/g, type: ColorType.RGB },
|
{ reg: /rgb\(\v\c\v\c\v\)/g, type: ColorType.RGB },
|
||||||
{ reg: /hsl\(\s*\d+\s*°?,\s*\d+%\s*,\s*\d+%\s*\)/g, type: ColorType.HSL },
|
{ 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 }
|
{ reg: /#(?:[0-9a-fA-F]{3}){1,2}/g, type: ColorType.HEX }
|
||||||
];
|
].map(v => { v.reg = replaceRegexp(v.reg.source); return v; });
|
||||||
|
|
|
@ -10,7 +10,7 @@ import { Devs } from "@utils/constants";
|
||||||
import definePlugin from "@utils/types";
|
import definePlugin from "@utils/types";
|
||||||
import { React } from "@webpack/common";
|
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 source = regex.map(r => r.reg.source).join("|");
|
||||||
const matchAllRegExp = new RegExp(`^(${source})`, "i");
|
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 => {
|
const isColorDark = (color: string, type: ColorType): boolean => {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
case ColorType.RGBA:
|
||||||
case ColorType.RGB: {
|
case ColorType.RGB: {
|
||||||
const match = color.match(/\d+/g)!;
|
const match = color.match(/\d+/g)!;
|
||||||
const lightness = calcRGBLightness(+match[0], +match[1], +match[2]);
|
const lightness = calcRGBLightness(+match[0], +match[1], +match[2]);
|
||||||
|
@ -154,16 +155,21 @@ const getColorType = (color: string): ColorType => {
|
||||||
color = color.toLowerCase().trim();
|
color = color.toLowerCase().trim();
|
||||||
if (color.startsWith("#")) return ColorType.HEX;
|
if (color.startsWith("#")) return ColorType.HEX;
|
||||||
if (color.startsWith("hsl")) return ColorType.HSL;
|
if (color.startsWith("hsl")) return ColorType.HSL;
|
||||||
|
if (color.startsWith("rgba")) return ColorType.RGBA;
|
||||||
if (color.startsWith("rgb")) return ColorType.RGB;
|
if (color.startsWith("rgb")) return ColorType.RGB;
|
||||||
|
|
||||||
throw new Error(`Can't resolve color type of ${color}`);
|
throw new Error(`Can't resolve color type of ${color}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
function parseColor(str: string, type: ColorType): string {
|
function parseColor(str: string, type: ColorType): string {
|
||||||
str = str.toLowerCase().trim();
|
str = str.toLowerCase().trim().replaceAll(/(\s|,)+/g, " ");
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case ColorType.RGB:
|
case ColorType.RGB:
|
||||||
return str;
|
return str;
|
||||||
|
case ColorType.RGBA:
|
||||||
|
if (!str.includes("/"))
|
||||||
|
return str.replaceAll(replaceRegexp(/\f(?=\s*?\))/.source), "/$&");
|
||||||
|
return str;
|
||||||
case ColorType.HEX:
|
case ColorType.HEX:
|
||||||
return str[0] === "#" ? str : `#${str}`;
|
return str[0] === "#" ? str : `#${str}`;
|
||||||
case ColorType.HSL:
|
case ColorType.HSL:
|
||||||
|
|
Loading…
Reference in a new issue