-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Closed
Description
Describe the problem
maybe this should be a discussion. if so, feel free to convert.
this Svelte attachment picks black or white text color to maximize contrast with node's background color:
export function contrast_color(
text_color_threshold: number = 0.7,
choices: [string, string] = [`black`, `white`], // one dark and one light color (in that order)
) {
return (node: HTMLElement | null) => {
const light_bg = luminance(get_bg_color(node)) > text_color_threshold
const text_color = light_bg ? choices[0] : choices[1] // white text for dark backgrounds, black for light
if (node) node.style.color = text_color
return text_color
}
}in some cases, i'd like to call it outside a Svelte component and have it return the text color it would apply to the node:
const text_color = contrast_color(0.6)(null)unfortunately, returning the color string from the attachment raises a type error when applied to a Svelte component:
Type
stringis not assignable to typevoid | (() => void)
Describe the proposed solution
maybe the allowed attachment return type could be broadened to enable this?
Importance
nice to have
Metadata
Metadata
Assignees
Labels
No labels