2020-10-08 15:19:28 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2020-10-08 15:19:28 +00:00
|
|
|
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2020-10-08 15:19:28 +00:00
|
|
|
*/
|
|
|
|
|
2022-04-20 14:49:12 +00:00
|
|
|
import { split } from "lodash";
|
|
|
|
|
2020-10-08 15:19:28 +00:00
|
|
|
export function textToHtmlRainbow(str: string): string {
|
|
|
|
const frequency = (2 * Math.PI) / str.length;
|
|
|
|
|
2022-04-20 14:49:12 +00:00
|
|
|
return split(str, "")
|
2020-10-08 15:19:28 +00:00
|
|
|
.map((c, i) => {
|
|
|
|
if (c === " ") {
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
const [a, b] = generateAB(i * frequency, 1);
|
|
|
|
const [red, green, blue] = labToRGB(75, a, b);
|
|
|
|
return (
|
2024-03-19 23:34:11 +00:00
|
|
|
'<span data-mx-color="#' +
|
2020-10-08 15:19:28 +00:00
|
|
|
red.toString(16).padStart(2, "0") +
|
|
|
|
green.toString(16).padStart(2, "0") +
|
|
|
|
blue.toString(16).padStart(2, "0") +
|
|
|
|
'">' +
|
|
|
|
c +
|
2024-03-19 23:34:11 +00:00
|
|
|
"</span>"
|
2020-10-08 15:19:28 +00:00
|
|
|
);
|
|
|
|
})
|
|
|
|
.join("");
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateAB(hue: number, chroma: number): [number, number] {
|
|
|
|
const a = chroma * 127 * Math.cos(hue);
|
|
|
|
const b = chroma * 127 * Math.sin(hue);
|
|
|
|
|
|
|
|
return [a, b];
|
|
|
|
}
|
|
|
|
|
|
|
|
function labToRGB(l: number, a: number, b: number): [number, number, number] {
|
2020-10-10 22:37:00 +00:00
|
|
|
// https://en.wikipedia.org/wiki/CIELAB_color_space#Reverse_transformation
|
|
|
|
// https://en.wikipedia.org/wiki/SRGB#The_forward_transformation_(CIE_XYZ_to_sRGB)
|
|
|
|
|
2020-10-08 15:19:28 +00:00
|
|
|
// Convert CIELAB to CIEXYZ (D65)
|
2020-10-08 17:55:52 +00:00
|
|
|
let y = (l + 16) / 116;
|
2020-10-08 15:19:28 +00:00
|
|
|
const x = adjustXYZ(y + a / 500) * 0.9505;
|
|
|
|
const z = adjustXYZ(y - b / 200) * 1.089;
|
|
|
|
|
|
|
|
y = adjustXYZ(y);
|
|
|
|
|
|
|
|
// Linear transformation from CIEXYZ to RGB
|
|
|
|
const red = 3.24096994 * x - 1.53738318 * y - 0.49861076 * z;
|
|
|
|
const green = -0.96924364 * x + 1.8759675 * y + 0.04155506 * z;
|
|
|
|
const blue = 0.05563008 * x - 0.20397696 * y + 1.05697151 * z;
|
|
|
|
|
|
|
|
return [adjustRGB(red), adjustRGB(green), adjustRGB(blue)];
|
|
|
|
}
|
|
|
|
|
|
|
|
function adjustXYZ(v: number): number {
|
|
|
|
if (v > 0.2069) {
|
|
|
|
return Math.pow(v, 3);
|
|
|
|
}
|
|
|
|
return 0.1284 * v - 0.01771;
|
|
|
|
}
|
|
|
|
|
|
|
|
function gammaCorrection(v: number): number {
|
|
|
|
// Non-linear transformation to sRGB
|
|
|
|
if (v <= 0.0031308) {
|
|
|
|
return 12.92 * v;
|
|
|
|
}
|
|
|
|
return 1.055 * Math.pow(v, 1 / 2.4) - 0.055;
|
|
|
|
}
|
|
|
|
|
|
|
|
function adjustRGB(v: number): number {
|
|
|
|
const corrected = gammaCorrection(v);
|
|
|
|
|
|
|
|
// Limits number between 0 and 1
|
|
|
|
const limited = Math.min(Math.max(corrected, 0), 1);
|
|
|
|
|
|
|
|
return Math.round(limited * 255);
|
|
|
|
}
|