magic-color
🌈 Make the colors be magical.
Watch my Talk to learn more basic knowledge points.
Features
- 💫 Support
multi-color model
conversion. - 📦 Built-in color related components.
- 🚀 Provides utility toolset functions
- 🦄 Theme color generator and fully customizable.
- 🥳
esm
only & 0 dependencies.
Usage
pnpm add magic-color
A lot of color tool functions for you to use, providing easy conversion, generation, parsing, comparison, operation and other functions.
basic
import { MagicColor } from 'magic-color'
const mc = new MagicColor('#ffffff')
const mc = new MagicColor('#ffffff', 'hex')
const mc = new MagicColor('#ffffff', 'hex', 1)
MagicColor
will automatically infer the input color type and the opacity.
Now support color types: RGB
, HEX
, HSL
, HSB
, Keyword
.
You can convert between supported types.
mc.toRgb()
mc.toHex()
mc.toHsl()
mc.toHsb()
mc.to('your transformed type')
If you want to output a color string, you can use the toString
method, and you can choose whether you need opacity.
mc.toString()
mc.toString(true)
mc.toRgb().toString(true)
Refer to the type documentation for more information.
And more...
theme
Well, you can use it to create a theme color.
import { theme } from 'magic-color'
theme('#9955ff')
And you can custom it with themeOptions
.
export interface ThemeOptions {
type?: ColorType
render?: (meta: [string, string]) => [string, string]
}
import { theme } from 'magic-color'
theme('#9955ff', {
type: 'rgb',
render: (meta) => {
return [
`--color-primary-${meta[0]}`,
meta[1].replace(/rgb\((.*)\)/, '$1').replace(/,/g, ''),
]
},
})
Credits
license
MIT