Skip to main content
Home

latest
It is unknown whether this package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
It is unknown whether this package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
JSR Score
58%
Published
a year ago (0.1.0)

magic-color NPM version

Magic color creator.

Features

  • 🎨 Convert between RGB, HEX, HSL and HSB.
  • 🦄 Theme color generator.
  • etc.

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, createMagicColor } from 'magic-color'

const mc = new MagicColor('#ffffff', 'hex', '1')
// or
const mc = createMagicColor('#ffffff') // recommended

createMagicColor 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().value // [255, 255, 255]
mc.toHex().value // '#ffffff'
mc.toHsl().value // [0, 0, 100]
mc.toHsb().value // [0, 0, 100]

If you want to output a color string, you can use the toString method, and you can choose whether you need opacity.

mc.toRgb().toString() // 'rgb(255, 255, 255)'
// with opacity
mc.toRgb().toString(true) // 'rgba(255, 255, 255, 100%)'

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')

// Will output:
// {
//   "50": "#faf7ff",
//   "100": "#f5eeff",
//   "200": "#e6d5ff",
//   "300": "#d6bbff",
//   "400": "#b888ff",
//   "500": "#9955ff",
//   "600": "#8a4de6",
//   "700": "#5c3399",
//   "800": "#452673",
//   "900": "#2e1a4d",
//   "950": "#1f1133",
// }

And you can custom it with themeOptions.

export interface ThemeOptions {
  /**
   * Output color type
   *
   * @default same type as input
   */
  type?: ColorType

  /**
   * Custom render output color
   *
   * @param meta [name, color]
   * @returns [CustomedName, CustomedColor]
   */
  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, ''),
    ]
  },
})

// Will output:
// {
//   "--color-primary-50": "250 247 255",
//   "--color-primary-100": "245 238 255",
//   "--color-primary-200": "230 213 255",
//   "--color-primary-300": "214 187 255",
//   "--color-primary-400": "184 136 255",
//   "--color-primary-500": "153 85 255",
//   "--color-primary-600": "138 77 230",
//   "--color-primary-700": "92 51 153",
//   "--color-primary-800": "69 38 115",
//   "--color-primary-900": "46 26 77",
//   "--color-primary-950": "31 17 51",
// }

Credits

license

MIT

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@zyyv/magic-color

Import symbol

import * as magic_color from "@zyyv/magic-color";
or

Import directly with a jsr specifier

import * as magic_color from "jsr:@zyyv/magic-color";

Add Package

pnpm i jsr:@zyyv/magic-color
or (using pnpm 10.8 or older)
pnpm dlx jsr add @zyyv/magic-color

Import symbol

import * as magic_color from "@zyyv/magic-color";

Add Package

yarn add jsr:@zyyv/magic-color
or (using Yarn 4.8 or older)
yarn dlx jsr add @zyyv/magic-color

Import symbol

import * as magic_color from "@zyyv/magic-color";

Add Package

vlt install jsr:@zyyv/magic-color

Import symbol

import * as magic_color from "@zyyv/magic-color";

Add Package

npx jsr add @zyyv/magic-color

Import symbol

import * as magic_color from "@zyyv/magic-color";

Add Package

bunx jsr add @zyyv/magic-color

Import symbol

import * as magic_color from "@zyyv/magic-color";