1
0
Fork 0
polaris-task-force/src/components/frontend/markers/iconStyle.ts

54 lines
2 KiB
TypeScript

import type React from "react";
/**
* Style that renders an Arma marker texture exactly like the game: the marker
* color MULTIPLIES the texture (white NATO/military glyphs take the color,
* full-color flags stay as-is under "Default"/white), and the texture's own
* alpha masks the result. Keys are CSS property names (kebab-case).
*/
export function armaIconCss(iconUrl: string, hex: string): Record<string, string> {
// Single quotes: this also lands inside double-quoted style attributes in
// divIcon HTML, where a double quote would end the attribute.
const url = `url('${iconUrl}')`;
return {
"background-image": url,
"background-color": hex,
"background-blend-mode": "multiply",
"background-size": "100% 100%",
"-webkit-mask-image": url,
"mask-image": url,
"-webkit-mask-size": "100% 100%",
"mask-size": "100% 100%",
};
}
/** Inline style string form of armaIconCss (for divIcon HTML). */
export function armaIconStyle(iconUrl: string, hex: string): string {
return Object.entries(armaIconCss(iconUrl, hex))
.map(([prop, value]) => `${prop}:${value}`)
.join(";");
}
/** React style object form of armaIconCss. */
export function armaIconReactStyle(iconUrl: string, hex: string): React.CSSProperties {
const style: Record<string, string> = {};
for (const [prop, value] of Object.entries(armaIconCss(iconUrl, hex))) {
const camel = prop
.replace(/^-webkit-/, "Webkit-")
.replace(/-([a-z])/g, (_, c: string) => c.toUpperCase());
style[camel] = value;
}
return style as React.CSSProperties;
}
/**
* Halo class for marker text: Arma's default black text vanishes on dark
* basemaps, so dark colors get a light halo and light colors a dark one.
*/
export function textHaloClass(hex: string): string {
const r = parseInt(hex.slice(1, 3), 16);
const g = parseInt(hex.slice(3, 5), 16);
const b = parseInt(hex.slice(5, 7), 16);
const luminance = (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255;
return luminance < 0.35 ? "arma-marker-halo-light" : "arma-marker-halo-dark";
}