mirror of
https://github.com/signalapp/Signal-Desktop.git
synced 2026-04-25 19:08:04 +01:00
Import/export chat styles
This commit is contained in:
26
ts/util/hslToRGB.ts
Normal file
26
ts/util/hslToRGB.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
// Copyright 2024 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative
|
||||
export function hslToRGB(
|
||||
h: number,
|
||||
s: number,
|
||||
l: number
|
||||
): {
|
||||
r: number;
|
||||
g: number;
|
||||
b: number;
|
||||
} {
|
||||
const a = s * Math.min(l, 1 - l);
|
||||
|
||||
function f(n: number): number {
|
||||
const k = (n + h / 30) % 12;
|
||||
return l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
|
||||
}
|
||||
|
||||
return {
|
||||
r: Math.round(255 * f(0)),
|
||||
g: Math.round(255 * f(8)),
|
||||
b: Math.round(255 * f(4)),
|
||||
};
|
||||
}
|
||||
45
ts/util/rgbToHSL.ts
Normal file
45
ts/util/rgbToHSL.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
// Copyright 2024 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB
|
||||
export function rgbToHSL(
|
||||
r: number,
|
||||
g: number,
|
||||
b: number
|
||||
): {
|
||||
h: number;
|
||||
s: number;
|
||||
l: number;
|
||||
} {
|
||||
// Normalize to [0, 1]
|
||||
const rn = r / 255;
|
||||
const gn = g / 255;
|
||||
const bn = b / 255;
|
||||
|
||||
const xMax = Math.max(rn, gn, bn);
|
||||
const xMin = Math.min(rn, gn, bn);
|
||||
const v = xMax;
|
||||
const c = xMax - xMin;
|
||||
const l = v - c / 2;
|
||||
let h: number;
|
||||
|
||||
if (c === 0) {
|
||||
h = 0;
|
||||
} else if (v === rn) {
|
||||
h = 60 * (((gn - bn) / c) % 6);
|
||||
} else if (v === gn) {
|
||||
h = 60 * ((bn - rn) / c + 2);
|
||||
} else {
|
||||
// v === b
|
||||
h = 60 * ((rn - gn) / c + 4);
|
||||
}
|
||||
|
||||
let s: number;
|
||||
if (l === 0 || l === 1) {
|
||||
s = 0;
|
||||
} else {
|
||||
s = (v - l) / Math.min(l, 1 - l);
|
||||
}
|
||||
|
||||
return { h, s, l };
|
||||
}
|
||||
Reference in New Issue
Block a user