1
Fork 0
mirror of https://github.com/Steffo99/bluelib.git synced 2024-12-23 12:04:22 +00:00
bluelib/src/utils/Colors.ts
2021-08-17 17:45:29 +02:00

41 lines
1.1 KiB
TypeScript

import Color from "color"
/**
* The classNames of the colors builtin in Bluelib.
*/
export enum BuiltinColor {
RED = "color-red",
ORANGE = "color-orange",
YELLOW = "color-yellow",
LIME = "color-lime",
CYAN = "color-cyan",
BLUE = "color-blue",
MAGENTA = "color-magenta",
}
/**
* Convert a {@link Color} to an object containing the `--bluelib-${name}-r`, `--bluelib-${name}-g` and `--bluelib-${name}-b` properties, to be passed in
* a `style` prop.
*
* Will return `None` if the `color` parameter is falsy.
*
* @param name - The property "name", to be filled in the template string described above.
* @param color - The color to convert.
*/
export function colorToBluelibStyle(name: string, color?: typeof Color): {[key: string]: number} {
const colorObj = Color(color)
if(!color) {
return {}
}
const result: {[key: string]: number} = {}
result[`--bluelib-${name}-r`] = colorObj.red()
result[`--bluelib-${name}-g`] = colorObj.green()
result[`--bluelib-${name}-b`] = colorObj.blue()
return result
}