Add color metadata to site embeds

This commit is contained in:
Erica Marigold 2024-07-06 22:50:49 +05:30
parent 9842aa4815
commit 8c07757c7a
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1
2 changed files with 66 additions and 0 deletions

View file

@ -16,6 +16,7 @@ import {
} from "../constants/constants"; } from "../constants/constants";
import { defaultFavicons } from "../constants/icon"; import { defaultFavicons } from "../constants/icon";
import type { Favicon } from "../types/config"; import type { Favicon } from "../types/config";
import { hslToHex } from "../utils/color-utils";
import { url } from "../utils/url-utils"; import { url } from "../utils/url-utils";
interface Props { interface Props {
@ -101,6 +102,8 @@ const siteLang = siteConfig.lang.replace("_", "-");
<meta name="viewport" content="width=device-width" /> <meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} /> <meta name="generator" content={Astro.generator} />
<meta name="theme-color" content={hslToHex(siteConfig.themeColor.hue, 100, 100)}>
{favicons.map(favicon => ( {favicons.map(favicon => (
<link rel="icon" <link rel="icon"
href={favicon.src.startsWith('/') ? url(favicon.src) : favicon.src} href={favicon.src.startsWith('/') ? url(favicon.src) : favicon.src}

63
src/utils/color-utils.ts Normal file
View file

@ -0,0 +1,63 @@
/**
* Converts an HSL color value to Hex. Adapted from
* https://stackoverflow.com/a/44134328.
*
* @param {number} h The hue
* @param {number} s The saturation
* @param {number} l The lightness
* @return {Array} The RGB representation
*/
export function hslToHex(h: number, s: number, l: number): string {
l /= 100;
const a = (s * Math.min(l, 1 - l)) / 100;
const f = (n: number) => {
const k = (n + h / 30) % 12;
const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
return Math.round(255 * color)
.toString(16)
.padStart(2, "0"); // convert to Hex and prefix "0" if needed
};
return `#${f(0)}${f(8)}${f(4)}`;
}
/**
* Converts an HSL color value to RGB. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSL_color_space.
* Assumes h, s, and l are contained in the set [0, 1] and
* returns r, g, and b in the set [0, 255].
*
* @param {number} h The hue
* @param {number} s The saturation
* @param {number} l The lightness
* @return {Array} The RGB representation
*/
export function hslToRgb(
h: number,
s: number,
l: number,
): [number, number, number] {
let r: number;
let g: number;
let b: number;
if (s === 0) {
r = g = b = l; // achromatic
} else {
const hue2rgb = (p: number, q: number, t: number) => {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1 / 6) return p + (q - p) * 6 * t;
if (t < 1 / 2) return q;
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
return p;
};
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
const p = 2 * l - q;
r = hue2rgb(p, q, h + 1 / 3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1 / 3);
}
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}